#!/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.  More documentation to come.
#
# - Ameir Abdeldayem
#


#-----------------------------------------------------
#	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 "mkd \"${directory}\""; done`
DELETE=`for file in $DFILES; do echo "delete \"${file}\""; done`
ATTACH=`for file in $FILES; do echo "put \"${file}\""; done`

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

# Send updates to first server
	ftp -nv <<EOF
	open $FTPHOST
	user $FTPUSER $FTPPASS
	binary
	cd $FTPDIR
	$MKDIR
	$DELETE
	$ATTACH
	quit
EOF

# Send updates to second server if it is configured
if [ -n "$FTPHOST1" ]; then
	ftp -nv <<EOF
	open $FTPHOST1
	user $FTPUSER1 $FTPPASS1
	binary
	cd $FTPDIR1
	$MKDIR
	$DELETE
	$ATTACH
	quit
EOF
fi

# 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 [ $? == 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."
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"
