#!/bin/bash
#
function usage () 
{
	echo -e "\nUsage:\n\t$(basename $0) [-adfhnt] -i\tInstall\n\t$(basename $0) [-adfhnt][-u]\tUpdate\n";
}
#
#	setup env
#
pushd $(dirname $0)/.. >> /dev/null
WORKDIR=$(pwd)
popd >> /dev/null
#echo -e "WORKDIR: $WORKDIR\n"
HOST="netusg20.ac.pestinger.net"
USER="root"
REMDIR=""
DDIR0="/etc/apache2"
DDIR1="/var/www"
DDIR2="$REMDIR/phpwcms-svn"
DDIR3="/var/lib/phpwcms/filearchive"
OPTS0="-va"
OPTS1="-u"
OPTS2="-C"
TESTING=0
CMD=""
#
#	process cmd line
#
while [ "$1" != "" ]; do
    case $1 in
        -a | --all )	
        	OPTS2="";
        	;;
        -d | --dest )	
        	shift
		    HOST="$1"
        	;;
        -f | --force )  
            OPTS1="--delete --force"
            ;;
        -h | --help )   
        	usage
            exit 0
            ;;
        -i | --install )	
        	CMD="install"
        	;;
        -n | --dry-run )	
        	OPTS0=${OPTS0}n
        	;;
        -t | --test )	
        	TESTING=1
        	;;
        -u | --update )	
        	CMD="update"
        	;;
        * ) 
        	usage
            exit 1
    esac
    shift
done
#
#	setup env cont.
#
REMURI="$USER@$HOST:$REMDIR"
TARGET0="${REMURI}${DDIR0}"
TARGET1="${REMURI}${DDIR1}"
TARGET2="${REMURI}${DDIR2}"
TARGET3="${REMURI}${DDIR3}"
IMPORTOPTS="$OPTS0 $OPTS1 $OPTS2"
INSTALLOPTS="$OPTS0 $OPTS1 $OPTS2 -K --no-o --no-g"
#
#	setup target environment
#
##ssh $REMURI 'bash -s' <<EndOfScript
##for d in $REMDIR $DDIR2 $DDIR3 $DDIR4; do
##	[ -e \$d ] || mkdir -p \$d
##done
##[ -e $REMDIR/phpwcms ] || ln -s $DDIR2 $REMDIR/phpwcms 
##chmod 777 $DDIR3
##[ -e $REMDIR/filearchive ] || ln -s $DDIR3 $REMDIR/filearchive 
##EndOfScript
#
#	process command
#
case $CMD in
	install )
		echo -e "Installing ($INSTALLOPTS) ...\n" 
		rsync $INSTALLOPTS $WORKDIR/rootfs/etc/asterisk/ $REMURI/etc/asterisk
##		rsync $INSTALLOPTS $WORKDIR/content/ $TARGET1
##		rsync $INSTALLOPTS $WORKDIR/phpwcms/phpwcms-svn/ $TARGET2
##		rsync $INSTALLOPTS $WORKDIR/phpwcms/filearchive/ $TARGET3
		;;
	update )
		echo -e "Update workspace ($IMPORTOPTS) ...\n"
		rsync $IMPORTOPTS $REMURI/etc/ $WORKDIR/rootfs/etc
##		rsync $IMPORTOPTS --safe-links $TARGET1/ $WORKDIR/content
##		rsync $IMPORTOPTS $TARGET2/ $WORKDIR/phpwcms/phpwcms-svn
##		rsync $IMPORTOPTS $TARGET3/ $WORKDIR/phpwcms/filearchive
		;;
	* )
		usage
		exit 1
esac
