#!/bin/sh
# for sync - $1 is the destination ip, where to sync to
#            $2 is the source ip, where is comes from
#            $3 can be the word "config" which tells this is sync to
#	     $4 peer name
#            $6 password
DATE=`date`

source_ip=$2
action=$3
SYNC_CMD=/ca/bin/ssh
SYNC_OPT="-q -p 65519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PasswordAuthentication=yes -o Sync=$6 -o CheckHostIp=no"

getFeature()
{
	awk ' 
	      BEGIN { feature_line = 0 }
	      /Features/ { feature_line = NR+1; fdata = $0 }
	      !/:/ {
			if (feature_line != 0) {
				if (feature_line == NR) {
					f2data = $0;
					feature_line = 0;
				}
			}
	      } 

	      END {
			print fdata;
			print f2data;
	      } 
	    ' $1
}

/ca/sync/munch -l 'show version' -d $1 -f 'init' > /tmp/my.version
myFeature=`getFeature '/tmp/my.version'`

myv=`cat /tmp/my.version | grep "^Array.*build" | cut -f1 -d"-"`
if [ "$myFeature" = "Licensed Features :" ]
then
	echo "Error: Missing license key on this machine"
	if [ ! -f /ca/sync/bipass ]
	then
		exit;
	fi
fi	

myGSLBFeature=`echo $myFeature | grep "GSLB"`
if [ "x$myGSLBFeature" = "x" ]
then
        echo "Error: Missing GSLB Feature on this machine"
        echo $myFeature
        exit 1
fi

skip=0
for i in $1 
do   
	#cut out the host name, input looks like "10.2.2.2 sam" 
	#dst_ip=`echo $i | sed '/[a-zA-d]/d'`
	#skip the peername, handle the peerip
	if [ $skip -eq 1 ]
	then
		skip=0
		hname=$i
		continue
	fi
	skip=1
	dst_ip=`echo $i`
	if [ "x$dst_ip" = "x" ]
	then
		echo " "
		#keep the hostname portion
		hname=$i
	else
		if [ $i = $source_ip ]
		then
			echo " " #don't bother to do anything for itself
		else
			if [ -n "`echo $dst_ip|grep :`" ]
			then
				PING=ping6
				ping_opt=""
			else
				PING=ping
				ping_opt="-t 3" 
			fi
			STATUS=`$PING -S $source_ip -n -c 1 $ping_opt $dst_ip | grep "100.*% packet loss"`
			echo "$DATE % $dst_ip % start % sync to" >> /ca/sync/log/history.log 

			if [ "x$STATUS" = "x" ]
			then
				echo " "
				echo "checking version and licenses...."

				$SYNC_CMD $SYNC_OPT $dst_ip "/ca/sync/munch -l 'show version' -d $2 -f 'init' " > /tmp/ca.version.tmp

				if [ $? -ne 0 ]
				then
					echo "Error: Wrong synconfig configurations or the challenge codes on the peers are mismatched."
					exit 1
				fi

				cat /tmp/ca.version.tmp | sed 's/[[:space:]]*REDUNDANT//' > /tmp/ca.version
				rm /tmp/ca.version.tmp

				otv=`cat /tmp/ca.version | grep "^Array.*build" | cut -f1 -d"-"`
				if [ "$otv" = "$myv" ]
				then
					echo "$myv  $otv"
				else
					if [ -n "$otv" ]
					then
						echo "Version mismatches, synconf is aborted"
						echo "This> $myv"
						#print peer name instead of dest ip address
						#echo "$dst_ip> $otv"
						echo "$4> $otv"
						echo "$DATE % $dst_ip % error % version mismatches % $otv" >> /ca/sync/log/history.log 
						echo " "
						if [ ! -f /ca/sync/bipass ]
						then
							exit 1
						fi
					fi
				fi

				key=`cat /tmp/ca.version | grep "License Key" | cut -f2 -d":"`
				if [ "x$key" = "x" ]
				then
					echo "Error: Missing license key on $dst_ip $hname"
					echo "$DATE % $dst_ip % error % missing license key" >> /ca/sync/log/history.log 
				else	
					feature=`getFeature /tmp/ca.version`
					if [ "$feature" = "Licensed Features :" ]
					then
						echo "Licensed undefined on $dst_ip"
						exit 1
					fi
					
					dstGSLBFeature=`echo $feature |grep "GSLB"`
                                        if [ "x$dstGSLBFeature" = "x" ]
                                        then
                                                echo "Error: Missing GSLB Feature on $dst_ip"
                                                echo $feature
                                                exit 1
                                        fi

                                        if [ ! -d /etc/usercfg/etc/namedb ]
                                        then
                                             echo "Error: Missing namedb files on this machine"
	                                     exit;
                                        fi
                                        
                                        cd /etc/usercfg/etc/

                                        tar -czf /ca/sync/namedb.tar.gz namedb/ 2> /dev/null
 
                                        if [ ! -f /ca/sync/namedb.tar.gz ]
                                        then
                                             echo "Error: Missing namedb tar files on this machine"
	                                     exit;
                                        fi	

					echo "sync config for $dst_ip starts..."
									
					$SYNC_CMD $SYNC_OPT $dst_ip "/ca/sync/sync_sdns $source_ip $action \"\" $6 > /ca/sync/log/$source_ip.log" 
					echo " "
					rm -f /ca/sync/namedb.tar.gz 2> /dev/null
				fi 
			else
				echo "Error: destination $dst_ip is unreachable"    
				echo "$DATE % $dst_ip % error % destination unreachable" >> /ca/sync/log/history.log 
			fi
		fi
	fi
done
