#!/bin/sh

# Sync webservers with snapshots - internetlynx.com.au
# Copyright 2006 clonepanel.com / internetlynx.com.au
# Version 0.30
# post_process.sh -h for built-in documentation

set -u
# Be strict about variable declaration

#avoid use of $PATH - limit script to commands specified
unset PATH

# Standard include files:
. $PROGRAM_DIR/includes

# Built-in documentation:
function showdocs {
	$CAT <<-EODOC
		Handle operations to follow on after sync with another account in clonepanel system.
			
		Command line options: 
		-h		Print these instructions and exit
		-t WEB|MAIL	Type of documents to sync (web or e-mail)
		-d PUSH|PULL	Direction of sync
		-c mysql40	(optional) Use this compatibility mode for mysql dump
		
		Exit (error) codes:
		$DOC_REQUEST		Documentation request (not an error)
		tba
		

EODOC
	exit $DOC_REQUEST
}


# Initialise own input variables:
stype=''
pushpull=''
force='n'
compat=''
# compatibility mode not currently used since no database dumps required in postprocessing

# Start real script

# Check for help request and input variables in the command line options:
while getopts ":ht:d:c:f" opt
do
	case $opt in
		h)	showdocs
			;;
		t)	stype=$OPTARG
			;;
		d)	pushpull=$OPTARG
			;;
		f)	force='y'
			;;
		c)	compat=$OPTARG
			;;
		*)	$ECHO "Unknown option. Use -h for instructions"; exit $E_UNKNOWN_OPT
			;;
	esac
done
shift $(($OPTIND - 1))

OPTIND=1

if [ ! $pushpull -o ! $stype ]; then
	cecho -c $error "Username and sync type must be specified"
	exit $E_BLANK_INPUTS
fi


my_status=$($CAT $REMOTEHOST_ROLES_FILE |$GREP ${stype}_.*=$MY_NAME |$CUT -d= -f1) ;
#get status of this host from roles file (eg WEB_SLAVE from WEB_SLAVE=MYHOST)

case "$stype.$pushpull" in
	WEB.PULL)
# I must be MASTER web server - postprocessing after rsync FROM me TO remote
		if [ $my_status != 'WEB_MASTER' ] ; then
			if [ $force == 'y' ] ; then
				$ECHO "$($DATE) Warning - status is $my_status - require WEB_MASTER. Continuing because forced (-f)." >>$REMOTE_ERROR_LOG ;
				cecho -c $warning "$($DATE) Warning - status is $my_status - require WEB_MASTER. Continuing because forced (-f)." ;
			else
				$ECHO "$($DATE) Error - status is $my_status - require WEB_MASTER" >>$REMOTE_ERROR_LOG ;
				cecho -c $error "$($DATE) Error - status is $my_status - require WEB_MASTER" ;
				exit 1
			fi
		fi
		cecho -c $info "Nothing to do on web master after sync" ;
		;;
	WEB.PUSH)
# I must be SLAVE web server - postprocessing after rsync FROM remote TO me
		if [ $my_status != 'WEB_SLAVE' ] ; then
			if [ $force == 'y' ] ; then
				$ECHO "$($DATE) Warning - status is $my_status - require WEB_SLAVE. Continuing because forced (-f)." >>$REMOTE_ERROR_LOG ;
				cecho -c $warning "$($DATE) Warning - status is $my_status - require WEB_SLAVE. Continuing because forced (-f)." ;
			else
				$ECHO "$($DATE) Error - status is $my_status - require WEB_SLAVE" >>$REMOTE_ERROR_LOG ;
				cecho -c $error "$($DATE) Error - status is $my_status - require WEB_SLAVE" ;
				exit 1
			fi
		fi
# Reload databases using new dump files just brought in
                for adb in `$LS $REMOTEHOST_DATABASE_DIR`
                do
                        . $REMOTEHOST_DATABASE_DIR/$adb

			if [ "$DB_HOST_OVERRIDE" ] ;  then op_host="-h$DB_HOST_OVERRIDE"
			elif [ "$db_host" ];	then op_host="-h$db_host"
						else op_host=""
			fi
			if [ "$db_table" ];	then db_file="${db_name}-$db_table.sql"
						else db_file="${db_name}.sql"
                        fi
                        if [ "$db_name" ] ;       then
				$MYSQL -u$db_user -p$db_password $op_host $db_name < $REMOTEHOST_DATABASE_STORE/$db_file
				cecho -c $info "Restored database $db_file"
                        fi
                done

		cecho -c $ok "Database restore complete on $my_status" ;
		;;
	MAIL.PULL)
# I must be MAIL_SLAVE web server - postprocessing after rsync FROM me TO remote
		if [ $my_status != 'MAIL_SLAVE' ] ; then
			$ECHO "$($DATE) Error - status is $my_status - require MAIL_SLAVE" >>$REMOTE_ERROR_LOG ;
			cecho -c $error "$($DATE) Error - status is $my_status - require MAIL_SLAVE" ;
			exit 1
		fi
# Clear clone directory (avoid transferring duplicate messages next time)
		if [ -d "$REMOTEHOST_CLONE_MAIL_DIR" ] ; then
			$RM -rf $REMOTEHOST_CLONE_MAIL_DIR/* ;
		fi ;
		cecho -c $info "Transferred messages cleared on $my_status" ;
		;;
	MAIL.PUSH)
# I must be MAIL_MASTER web server - postprocessing after rsync FROM remote TO me
		if [ $my_status != 'MAIL_MASTER' ] ; then
			$ECHO "$($DATE) Error - status is $my_status - require MAIL_MASTER" >>$REMOTE_ERROR_LOG ;
			cecho -c $error "$($DATE) Error - status is $my_status - require MAIL_MASTER" ;
			exit 1
		fi
# Move all messages from clone directory to appropriate mailbox in main mail dir
		$MAIL_MOVE_SCRIPT $REMOTEHOST_CLONE_MAIL_DIR $REMOTEHOST_MAIL_DIR
		cecho -c $ok "Messages from slave added to mailboxes on $my_status"
		;;
	*)
		cecho -c $error "$($DATE) Error - Unknown type"
		exit 1
		;;
esac	


