#!/bin/bash

# Download Git tags missing to SVN /tags folder
# Relies on Fetch utility: https://github.com/gruntwork-io/fetch

GITHUB_REPO="http://github.com/salaros/wp-purge-pagespeed-button"
GIT_TAGS_SYNCHED=0

# Iterate through Git tags
for git_tag in $(git -C ./trunk/ tag)
do
    # Check if the tag is missing in SVN /tags folder
    if [ ! -d ./tags/${git_tag} ]; then
        printf "\nGit tag ${git_tag} doesn't not exist in SVN tags folder!"

        # Download the missing Git tag from GitHub repo and extract it to /tags folder
        fetch --repo="${GITHUB_REPO}" --tag="${git_tag}" --source-path="/" ./tags/${git_tag}

        # Increment Git tag counter variable
        GIT_TAGS_SYNCHED=$(expr $GIT_TAGS_SYNCHED + 1)
    fi
done

# Check if at least one Git tag has been downloaded
# and print messages accordingly to the tag counter
if [ $GIT_TAGS_SYNCHED -eq 0 ]; then
    printf "No Git tags to synchronize. Nothing to do, exiting!"
else
    printf "\nSynchronized ${GIT_TAGS_SYNCHED} Git tags to SVN, you can run 'svn update' now!"
fi
