#!/bin/bash

ENV=${1:-dev}

echo "Starting deployment for $ENV environment"

# main config, set off of plugin slug
CURRENTDIR=`pwd`

# git config
GITPATH="$CURRENTDIR/" # this file should be in the base of your git repository

# svn config
SVNPATH="$CURRENTDIR/wpdeploy" # path to a temp SVN repo. No trailing slash required and don't add trunk.
SVNURL="http://plugins.svn.wordpress.org/smsglobal-sms-plugin/" # Remote SVN repo on WordPress.org, with no trailing slash
SVNUSER="SMSGlobal" # your svn username
#SVNPASSWORD="$SVNPASSWORD" # SVNPASSWORD comes from the environment variable unless you specify it here

rm -rf $SVNPATH

# Removing existing SVN directory
rm -rf $SVNPATH

NEWVERSION=`grep "^Stable tag" $GITPATH/readme.txt | awk -F' ' '{print $3}'`
echo "Version: $NEWVERSION"

cd $GITPATH

if [ "$ENV" = "prod" ]
then
    git commit -am "Updated to version $NEWVERSION"
    echo "Tagging new version in git $NEWVERSION"
    git tag -a "$NEWVERSION" -m "Tagging version $NEWVERSION"

    echo "Pushing latest commit to origin, with tags"
    git push origin master
    git push origin master --tags
fi

echo
echo "Creating local copy of SVN repo ..."

svn co $SVNURL $SVNPATH

# Export git -> SVN
echo "Exporting the HEAD of master from git to the trunk of SVN"
git checkout-index -a -f --prefix=$SVNPATH/trunk/

echo "Ignoring github specific files and deployment script"
svn propset svn:ignore "deploy.sh
README.md
.git
tests
wordpress-tests
.gitignore
.gitmodules" "$SVNPATH/trunk/"

# If submodule exist, recursively check out their indexes
if [ -f ".gitmodules" ]
then
    echo "Exporting the HEAD of each submodule from git to the trunk of SVN"
    git submodule init
    git submodule update
    git submodule foreach --recursive 'git checkout-index -a --prefix=$SVNPATH/trunk/$path/'
fi

echo "Changing directory to SVN and committing to trunk"
cd $SVNPATH/trunk/
# Add all new files that are not set to be ignored
svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
svn mv assets ..
cd $SVNPATH
svn commit --username=$SVNUSER --password=$SVNPASSWORD -m "New plugin stable version 2.0.1"

#move assets to root

if [ "$ENV" = "prod" ]
then
    echo "Creating new SVN tag & committing it"
    cd $SVNPATH
    if [ ! -e tags/$NEWVERSION/ ];
    then
        svn copy trunk/ tags/$NEWVERSION/
        cd $SVNPATH/tags/$NEWVERSION
        svn commit --username=$SVNUSER -m "Tagging version $NEWVERSION"
    else
        echo "Tag exists for the version $NEWVERSION"
        echo "Change stable version in Readme.txt file for release"
    fi
fi

echo "Removing temporary directory $SVNPATH"
rm -rf $SVNPATH

echo "Complete"

