#!/bin/sh

# Sync webservers with snapshots - internetlynx.com.au
# Copyright 2006 clonepanel.com / internetlynx.com.au
# Version 0.30
# pre_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 be done before 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
		
EODOC
	exit $DOC_REQUEST
}


# Initialise own input variables:
stype=''
pushpull=''
force='n'
compat=''

# 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)


if [ "$compat" ]; then
	compat="--compatible=$compat"
	# mysql dump compatibility mode
fi


if [ ! -d $REMOTEHOST_DATABASE_STORE ]; then
	$MKDIR $REMOTEHOST_DATABASE_STORE
fi

case "$stype.$pushpull" in
	WEB.PULL)
# I must be WEB_MASTER server - preprocessing before 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
# Dump databases to new dump files ready for transfer
		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 
				$MYSQLDUMP $compat -u$db_user -p$db_password $op_host $db_name $db_table  > $REMOTEHOST_DATABASE_STORE/$db_file
                                cecho -c $info "Saved database $db_file"
 			fi
		done
		cecho -c $ok "Database dump complete on $my_status" ;
		;;
	WEB.PUSH)
# I must be WEB_SLAVE server - preprocessing before 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
		cecho -c $info "WEB PUSH - Nothing to do before sync" ;
		;;
	MAIL.PULL)
# I must be MAIL_SLAVE web server - preprocessing before 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
# Move all mailboxes to clone directory
		$MAIL_MOVE_SCRIPT $REMOTEHOST_MAIL_DIR $REMOTEHOST_CLONE_MAIL_DIR ;
		cecho -c $info "Mailboxes copied and emptied on $my_status" ;
		;;
	MAIL.PUSH)
# I must be MAIL_MASTER web server - preprocessing before 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
		cecho -c $info "MAIL PUSH - Nothing to do before sync" ;
		;;
		*)
		cecho -c $error "$($DATE) Error - Unknown type" ;
		exit 1 ;
		;;
esac	



