#!/bin/bash
# Given a svn-url file one directory up, export the latest git commits to the specified SVN repo.
# Create a git release tag from the version specified in the plugin file.
# Author: Weston Ruter (@westonruter)

set -e

cd "$( dirname $0 )"
dev_lib_dir=$(pwd)
cd ..

if [ ! -e svn-url ]; then
	echo "Error: Missing svn-url file" >&2
	exit 1
fi

force=
while getopts 'f' option; do
	case $option in
		f)
			force=1
			;;
	esac
done

current_branch=$(git rev-parse --abbrev-ref HEAD)
if [ $current_branch != 'master' ]; then
	echo "Please checkout the master branch"
	exit 1
fi

if [ -n "$(git status -s -uno)" ] && [ -z "$force" ]; then
	git status
	echo "Error: Git state has modified or staged files. Commit or reset, or supply -f" >&2
	exit 1
fi

git_root=$(pwd)

if [ -e readme.txt ]; then
	$dev_lib_dir/generate-markdown-readme
	git add readme.md
fi
if [ -n "$(git status --porcelain readme.md)" ]; then
	echo "Please commit (--amend?) the updated readme.md"
	exit 1
fi

git pull --ff-only origin master
git push origin master
svn_url=$(cat svn-url)
svn_tmp_repo_dir=/tmp/svn-$(basename $git_root)-$(md5sum <<< $git_root | cut -c1-32)
git_tmp_repo_dir=/tmp/git-$(basename $git_root)-$(md5sum <<< $git_root | cut -c1-32)
echo "Temp SVN dir: $svn_tmp_repo_dir"
echo "Temp Git dir: $git_tmp_repo_dir"

for php in *.php; do
	if grep -q 'Plugin Name:' $php && grep -q 'Version:' $php; then
		plugin_version=$(cat $php | grep 'Version:' | sed 's/.*Version: *//')
	fi
done

if [ -z "$plugin_version" ]; then
	echo "Unable to find plugin version"
	exit 1
fi

if ! grep -q "$plugin_version" readme.txt; then
	echo "Please update readme.txt to include $plugin_version in changelog"
	exit 1
fi

if git show-ref --tags --quiet --verify -- "refs/tags/$plugin_version"; then
	has_tag=1
fi

if [ -n "$has_tag" ] && [ -z "$force" ]; then
	echo "Plugin version $plugin_version already tagged. Please bump version and try again, or supply -f"
	exit 1
fi

# Clean up existing Git and SVN repos
if [ -e $svn_tmp_repo_dir ] && [ ! -e $svn_tmp_repo_dir/.svn ]; then
	rm -rf $svn_tmp_repo_dir
fi
if [ -e $git_tmp_repo_dir ] && [ ! -e $git_tmp_repo_dir/.git ]; then
	rm -rf $git_tmp_repo_dir
fi

# SVN: Checkout or update
if [ ! -e $svn_tmp_repo_dir ]; then
	svn checkout $svn_url $svn_tmp_repo_dir
	cd $svn_tmp_repo_dir
else
	cd $svn_tmp_repo_dir
	svn up
fi

# Git: Clone or pull
if [ ! -e $git_tmp_repo_dir ]; then
	git clone $git_root $git_tmp_repo_dir
	cd $git_tmp_repo_dir
else
	cd $git_tmp_repo_dir
	git pull
fi

# rsync all committed files from the Git repo to SVN
rsync -avz --delete --delete-excluded --exclude '/docs' --exclude '/assets' --exclude '/.git*' --exclude '/dev-lib' --exclude '/.jshint*' --exclude '/svn-url' --exclude '/.travis.yml' $git_tmp_repo_dir/ $svn_tmp_repo_dir/trunk/
if [ -e $git_tmp_repo_dir/assets/ ]; then
	mkdir -p $svn_tmp_repo_dir/assets/
	rsync -avz --delete $git_tmp_repo_dir/assets/ $svn_tmp_repo_dir/assets/
fi
cd $svn_tmp_repo_dir

if svn status | grep -s '^!'; then
    svn status | grep '^!' | awk '{print $2}' | xargs svn delete --force
fi
svn add --force trunk --auto-props --parents --depth infinity -q

mkdir -p tags
if [ -e tags/$plugin_version ]; then
	svn rm --force tags/$plugin_version
fi
cp -r trunk tags/$plugin_version
svn add --force tags/$plugin_version --auto-props --parents --depth infinity -q

svn_commit_file=/tmp/svn-commit-msg
git --git-dir $git_root/.git log -1 --format="Update to commit %h from $(git --git-dir $git_root/.git config --get remote.origin.url)" > $svn_commit_file

svn status
echo
echo "SVN Commit message:"
cat $svn_commit_file
echo

echo "Hit enter to proceed with SVN commit and Git tag"
read OK

if [ -z "$has_tag" ]; then
	cd $git_tmp_repo_dir
	echo "Tagging plugin version $plugin_version"
	git tag "$plugin_version" master
	git push origin "$plugin_version"
	cd $svn_tmp_repo_dir
else
	echo "Skipping plugin tag $plugin_version since already exists"
fi

svn commit -F $svn_commit_file
