#!/bin/sh

# Backup script for FileWave v5.x
# updated by Ken 8/22/2011

# if Leopard, we have to unload via launchDaemons - otherwise simply via systemstarter

OSVERSION=`sw_vers -productVersion | cut -f 2 -d .`
if [ $OSVERSION -ge 5 ]; then
	echo "unloading and disabling the launch daemon for fwxserver"
	# unload and disable it (so that it does not restart automatically)
	/bin/launchctl unload -w /System/Library/LaunchDaemons/com.filewave.fwxserver-admin.plist
	/bin/launchctl unload -w /System/Library/LaunchDaemons/com.filewave.fwxserver-server.plist
	/bin/launchctl unload -w /System/Library/LaunchDaemons/com.filewave.fwxserver-ldap.plist
	/bin/launchctl unload -w /System/Library/LaunchDaemons/com.filewave.fwxserver-apache.plist
else
	/sbin/SystemStarter stop fwxserver
fi

# nuke any existing exe
/usr/bin/killall fwxserver

# take down apache
/usr/local/filewave/apache/bin/apachectl stop

############################################################
## You need to edit these two paths. The first path will  ##
## store the database files, and the second stores server ##
## files. The database folder will be timestamped.        ##
############################################################

DBPATH=/test_backup_dir/fwbackup/fwxserver-DB$(date "+%Y.%m.%d-%H.%M.%S")
FW_FILES_PATH=/test_backup_dir/fwxserver/


#########Changes below here are not necessary########

/bin/mkdir -p $DBPATH
/bin/mkdir -p $FW_FILES_PATH
/bin/cp /fwxserver/DB/* $DBPATH
/usr/bin/ditto -cz $DBPATH $DBPATH.zip

/usr/bin/rsync -r /fwxserver/Data\ Folder $FW_FILES_PATH > /dev/null

/bin/sleep 1
/bin/rm -R $DBPATH
#might have to edit this path to rsync, use the command:
#"which rsync" to find the path to rsync on your computer


if [ $OSVERSION -ge 5 ]; then

	# load up the server again, and make sure (with -w) that its re-enabled
	chmod 644 /System/Library/LaunchDaemons/com.filewave.fwxserver-admin.plist
	chmod 644 /System/Library/LaunchDaemons/com.filewave.fwxserver-server.plist
	/bin/launchctl load -w /System/Library/LaunchDaemons/com.filewave.fwxserver-admin.plist
	/bin/launchctl load -w /System/Library/LaunchDaemons/com.filewave.fwxserver-server.plist
	/bin/launchctl load -w /System/Library/LaunchDaemons/com.filewave.fwxserver-ldap.plist
	/bin/launchctl load -w /System/Library/LaunchDaemons/com.filewave.fwxserver-apache.plist
else 
	/usr/sbin/chown -R root:wheel /System/Library/StartupItems/FWServer
	/usr/bin/find /System/Library/StartupItems/FWServer -type d -exec /bin/chmod 755 {} \; 
	/usr/bin/find /System/Library/StartupItems/FWServer -type f -exec /bin/chmod 644 {} \; 
	/bin/chmod 755 /System/Library/StartupItems
	/bin/chmod 755 /System/Library/StartupItems/FWServer
	/bin/chmod 644 /System/Library/StartupItems/FWServer/FWServer
	/bin/chmod +x /System/Library/StartupItems/FWServer/FWServer


	/sbin/SystemStarter start fwxserver
fi



exit 0
