#!/bin/sh
#transfering the config data from the requesting host 
# for sync - $1 is the source ip, where to sync from
#            $2 can be the word "config" which tells this is sync to
#$4     password

if [ -n "`echo $1|grep :`" ]
then
	format_ip1="[""$1""]"
	option="-6"
else
	format_ip1="$1"
	option=""
fi

RSYNC_CMD=/ca/sync/rsync
SYNC_CMD=/ca/bin/ssh
SYNC_OPT="-q -p 65519 -o CheckHostIP=no -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o Sync=$4"

checkAccess()
{
	ERROR=`grep "Access denied" /tmp/clear`
	cat /tmp/clear
	if [ -n "$ERROR" ]
	then
   		echo "Sync config failed: Access denied due to someone else in config mode" 1>&2
		echo "`date`: end: error : access : `cat /tmp/clear`" >> /ca/sync/log/$1.status
   		exit 1
	fi    
}

getGSLBCli()
{
     	awk ' 
            /^#smart DNS configuration/ { print $0 }
            /^sdns/                     { print $0 }
            /^#NoCheck IP Address/      { print $0 }
	    ' $1
}

rollback()
{
	rm -f /ca/sync/log/from
	if [ ! -d /ca/sync/$new_name ]
	then
		echo "Error: $1 does not exist, cannot rollback" 1>&2
		echo "`date`% error % $1 % `cat /tmp/clear`" >> /ca/sync/log/$1.status
		exit 1
	fi 

	echo "Restoring the original configuration...." 1>&2
	echo "`date` % start % $1 % rollback % $2 $3" >> "/ca/sync/log/$1.status"
	echo ">> Restoring files"
	$RSYNC_CMD -avz --delete /ca/sync/$new_name/backup/namedb/ /ca/sync/$new_name/namedb

	#strip out the exceptions
	getGSLBCli /ca/sync/$new_name/backup/ca_sdns.conf.org | sed -f /ca/sync/stripfile > /ca/sync/$new_name/ca_sdns.conf
         

	/ca/sync/munch -l 'clear sdns secondary' -d "$new_name" -f "config" > /tmp/clear
	echo ">> Show clear config output"
	cat /tmp/clear
	checkAccess
	CLI2="config file $new_name"
	/ca/sync/munch -l "$CLI2" -d "$new_name" -f "sdns"
	echo "`date` % end % $1 % completed % $2 $3" >> /ca/sync/log/$1.status
}

echo "`date` % start % $1 % $2 $3" > /ca/sync/log/$1.status

if [ $# -lt 1 ] 
then
	echo "too few parameter"
	exit 1
fi
	
#convert . to _ some places don't like filenames with .
new_name="SC_`echo $1 | sed 's/\./_/g'`"

if [ ! -d /ca/sync/$new_name ]
then
	mkdir /ca/sync/$new_name
fi

if [ ! -d /ca/sync/$new_name/backup ]
then
	mkdir /ca/sync/$new_name/backup
fi

/ca/sync/munch -x '/bin/rm -f /ca/conf/uconf/sync_sdns4321.cfg'
/ca/sync/munch -l 'write file sync_sdns4321' -d "$new_name" -f "config" > /tmp/clear
checkAccess
if [ ! -f /ca/conf/uconf/sync_sdns4321.cfg ]
then
	echo "Error: unable to save the CLI configuration for backup"
	echo "`date`% error % $1 % $2 $3" >> /ca/sync/log/$1.status
fi

#backup the old stuff first 
echo ">> Backing up files"

echo ">> Backing up bind9 configurations..."
$RSYNC_CMD -avz --delete /etc/usercfg/etc/namedb/ /ca/sync/$new_name/backup/namedb

echo ">> Backing up sdns CLI ..."
/ca/sync/munch -x '/bin/chmod 755 /ca/conf'
/ca/sync/munch -x '/bin/chmod 755 /ca/conf/uconf'
$RSYNC_CMD -avz /ca/conf/uconf/sync_sdns4321.cfg /ca/sync/$new_name/backup/ca_sdns.conf.org 

#retrieve from the originiator 
echo ">> Transfering files from $1"

echo ">> Transfering CLI from the running config..."
echo "Synchronizing CLI ..." 1>&2
$RSYNC_CMD -avzo --rsh="$SYNC_CMD $SYNC_OPT" --rsync-path=$RSYNC_CMD $format_ip1:/ca/conf/uconf/sync_sdns4321.cfg /ca/sync/$new_name/ca_sdns.conf.org  

echo ">> Transfering bind9 configurations ..."
echo "Synchronizing bind9 configurations..." 1>&2
$RSYNC_CMD -avz --rsh="$SYNC_CMD $SYNC_OPT" --rsync-path=$RSYNC_CMD --delete $format_ip1:/etc/usercfg/etc/namedb /ca/sync/$new_name/

#strip out the exceptions
sed -f /ca/sync/stripfile_sdns /ca/sync/$new_name/ca_sdns.conf.org > /ca/sync/$new_name/ca_sdns.conf

#backup sdns member local
CLIBAK=`/ca/sync/munch -l 'show sdns all' -f "config" | grep "sdns member local"`

echo "Clearing the running configuration..." 1>&2

CLI="clear sdns secondary"
echo "clearing -> $CLI" 

/ca/sync/munch -l "$CLI" -d "$new_name" -f "config" > /tmp/clear

CLI1="clear llb dns host"
echo "clearing -> $CLI1" 

/ca/sync/munch -l "$CLI1" -d "$new_name" -f "config" > /tmp/clear

checkAccess
echo ">> Show clear config output"
cat /tmp/clear


#/ca/sync/munch -x "tar -xzf /ca/sync/$new_name/namedb.tar.gz" 2 > /tmp/clear

#digest the new configuration with the root permission
CLI2="config file $new_name"
echo "seting -> $CLI2" 

echo "Applying the new settings ..." 1>&2
#move down loaded files to the rightful places,
#need to have root privilege for this

/ca/sync/munch -l "$CLI2" -d "$new_name" -f"sdns" 2> /tmp/clear 

if [ "$CLIBAK" != "" ]
then
/ca/sync/munch -l "$CLIBAK" -d "$new_name"  -f "config" >> /dev/null
fi

ERROR=`cat /tmp/clear | grep -i "error"`
echo ">> Show new configuration output"
cat /tmp/clear

if [ -n "$ERROR" ]
then
	echo ">> Error occured, performing automatic rollback" 1>&2
	cat /tmp/clear 1>&2
	rollback
     	echo "`date` % error % $1 % $1 $2 $3" >> /ca/sync/log/$1.status
	exit 1
fi

echo "sync config completed, writing to the current config" 1>&2
echo "write mem->"

/ca/sync/munch -l 'write mem' -d "$new_name" -f "config"   
/ca/sync/munch -x '/bin/rm -f /ca/conf/uconf/sync_sdns4321.cfg'
echo "removing /ca/conf/uconf/$new_name.cfg"
/ca/sync/munch -x "/bin/rm -f /ca/conf/uconf/$new_name.cfg"
echo "`date`% end % $1 % completed % $2 $3" >> /ca/sync/log/$1.status
