#!/bin/bash
. $4

#----------------------------------------------------
# This script will sync an SVN repo to a remote FTP 
# server.  It is designed to be used as a post-commit
# hook.
#
# - Ameir Abdeldayem
# http://www.ameir.net
# You are free to use this script as you please so long
# as you keep my name and URL in it.


#-----------------------------------------------------
#	End of config options
#-----------------------------------------------------

# check for correct number of arguments
if [ $# -ne 4 ];then
	echo "Error in $0 - Invalid Number of Arguments!"
	echo "Correct Usage: $0 \"\$REPOS\" \"\$REV\" \"path/between/repo/and/trunk\" \"/full/path/to/config/file\""
	exit
fi

# make sure we have home of all svn repos created
if ! [ -e $SVNHOME ]; then
        mkdir $SVNHOME
fi

# see if repo has a copy in tmp, else make it
if ! [ -e $HOME ]; then
	mkdir $HOME
fi

# print timestamp so we can see when commit was performed
echo -e "\t`date`"

# if lockfile does not exist, this is the first time to sync this repo
# do a full svn checkout and create a lockfile
if ! [ -e $HOME/lockfile ]; then

	echo "Downloading full repository to machine..."
	cd $HOME
	svn co $BASEURL/$EXTPATH/trunk --username=$USER --password=$PASSWORD
	touch $HOME/lockfile
	echo "Done with repository download!"

# update our local copy with the repo
else
	echo "Doing an svn update..."
        cd $HOME/trunk
	svn update --username=$USER --password=$PASSWORD > $HOME/svnupdate.txt
	cat $HOME/svnupdate.txt
	echo "Done with svn update! Local copy is now most recent!"
fi

#-------------------------------------------------
# done with svn updating, now for FTP sync
#-------------------------------------------------

cd $HOME/trunk

FILES=`find . -type f -mtime -1 | grep / | grep -v "\\.svn" | grep -v _logs | grep -vi Caverns.mdb`
DFILES=`cat $HOME/svnupdate.txt | grep "^D    " | sed -e 's/D    /.\//g'`
echo -e "Updated files:\n${FILES:-(none)} \n"
echo -e "Deleted files:\n${DFILES:-(none)} \n"

# make list of directory tree
# FOLDERS=`ls -R | grep / | sed -e 's/://g' -e 's/\.\///g'`

# find all folders modified in past day; remove dot from first line
FOLDERS=`find . -type d -mtime -1 | grep -v "\\.svn" | grep -v _logs | grep -v "^.$"`
echo -e "Updated folders:\n${FOLDERS:-(none)} \n"

# change IFS variable so we can parse files with spaces properly
ORIGIFS=$IFS
IFS=`echo -en "\n\b"`

MKDIR=`for directory in $FOLDERS; do echo "${directory}"; done`
DELETE=`for file in $DFILES; do echo "${file}"; done`
ATTACH=`for file in $FILES; do echo "${file}"; done`

IFS=$ORIGIFS
# ATTACH1=`echo $FILES | while read file; do echo -n -e put "${file}\n"; done`

# Send updates to first server
	php /usr/local/bin/ftpupdate.php "$FTPHOST" "$FTPPORT" "$FTPUSER" "$FTPPASS" "$FTPDIR" "$MKDIR" "$ATTACH" "$DELETE"
ERROR=$?
if [ -n "$FTPHOST1" ]; then
	php /usr/local/bin/ftpupdate.php "$FTPHOST1" "$FTPPORT1" "$FTPUSER1" "$FTPPASS1" "$FTPDIR1" "$MKDIR" "$ATTACH" "$DELETE"
fi
ERROR=$(($ERROR + $?))

# change timestamp of files so that any more commits today don't resend everything!
# touch -t 0701011200 $FILES

# touch files with old date only if FTP transfer succeeded
if [ $ERROR == 0 ]; then
	echo "No errors captured from FTP."
	ORIGIFS=$IFS
	IFS=`echo -en "\n\b"`
	for file in $FILES; do touch -t 0701011200 "${file}"; done
	for folder in $FOLDERS; do touch -t 0701011200 "${folder}"; done
	IFS=$ORIGIFS
else
	echo "An error occurred with the transfer.  The files have not been touched."
	echo "The FTP script encountered an error and did not complete.  See the svn2ftp logs for more info." | mutt -s "FTP sync failed! ($REPONAME)" it@hargroveinc.com
fi

# Debugging purposes only:
# for file in $FILES; do touch -t 0701011200 "${file}"; done
# echo "$REPONAME $REV" > $HOME/test.txt
# echo -e "Reponame: $REPONAME\nRepo: $REPO\nRevision: $REV" | mutt -s test2 email@address.com

echo -e "----------------------------------------\n\n"
