#!/bin/sh
# for sync - $1 is the destination ip, where to sync to
#            $2 is the source ip, where is comes from
#            $2 can be the word "from" which tells this is sync from instead of to
#	     $4 peer name       (bug 7245)
#            $6 passwd
#            $7 isFIPS   -- This argument tells this script whether this system is FIPS or not.  'YES'= FIPS system, 'NO'=non-FIPS system.
#            $8 syncAfterFips   -- This argument tells this script to run '/ca/sync/sync' only and get out.  The argument is empty when it is not needed.
#            $9 local ip address
DATE=`date`
SYNC_CMD=/ca/bin/ssh
SYNC_OPT="-q -b $9 -p 65519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PasswordAuthentication=yes -o Sync=$6 -o CheckHostIp=no"
#echo "remote:  destIp=$1, srcIp=$2, 3rd=$3, peername=$4, 5th=$5, 6th=password, fips=$7, syncAfterFips=$8."

getFeature()
{
        awk '
              BEGIN { feature_line = 0 }
              /:/ {
                        feature_line = 0;
              }
              /Features/ {
                        feature_line = 1;
                        printf("Licensed Features :");
                        for(i = 4; i<=NF; i++) printf("  "$i); # $4 is the first feature
              }
              !/:/ {
                        if (feature_line != 0) {
                                 for(i = 1; i<=NF; i++) printf("  "$i);
                        }
              }

              END {}
            ' $1
}

#
# Special case for FIPS only:  Do code block below only after FIPS clone has been completed and this script is called the 2nd time to do just 'sync' part.
# 
if [ "$7" = "YES" ]
then
    if [ "$8" = "YES" ]
    then
        echo "$DATE % $1 % start % sync_from" >> /ca/sync/log/history.log 
        echo "sync config for $1 starts..."

        /ca/sync/sync $1 $2 "" $6 > /ca/sync/log/$1.log     
        exit
    fi
fi

# rollback from
if [ "$2" = "rollback" ]
then
	if [ "$3" = "from" ]
	then
		echo "$DATE % $1 % start % rollback_from" >> /ca/sync/log/history.log 
		/ca/sync/sync $1 $2 $3 $6 > /ca/sync/log/$3.log		
		exit
	fi
fi

/ca/sync/munch -l 'show version' -d $1 -f 'init' | sed 's/[[:space:]]*REDUNDANT//' > /tmp/my.version
myFeature=`getFeature '/tmp/my.version'`

myv=`cat /tmp/my.version | grep "^Array.*build" | cut -f2 -d" "`

if [ "$myFeature" = "Licensed Features :" ]
then
	echo "Error: Missing license key on this machine"
	if [ ! -f /ca/sync/bipass ]
	then
		exit;
	fi
fi	

flag=1	
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'`
	#pass the peername, handle the peerip
	if [ $flag -eq 0 ]
	then
		flag=1
		continue
	fi
	flag=0
	dst_ip=`echo $i`
	if [ "x$dst_ip" = "x" ]
	then
		echo " "
		#keep the hostname portion
		hname=$i
	else
		if [ $i = $2 ]
		then
			echo " " #don't bother to do anything for itself
		else
			if [ "$2" = "from" ]
			then
				if [ -n "`echo $dst_ip|grep :`" ]
				then
					PING=ping6
					ping_opt=""
				else
					PING=ping
					ping_opt="-t 3" 
				fi
				STATUS=`$PING -n -c 1 $ping_opt $dst_ip | grep "100.*% packet loss"`
				rm -f /ca/sync/log/from
				echo "$DATE % $dst_ip % start % sync from " >> /ca/sync/log/history.log 
			else
				if [ -n "`echo $2|grep :`" ]
				then
					PING=ping6
					ping_opt=""
				else
					PING=ping
					ping_opt="-t 3" 
				fi
				STATUS=`$PING -S $2 -n -c 1 $ping_opt $dst_ip | grep "100.*% packet loss"`
				echo "$DATE % $dst_ip % start % sync to" >> /ca/sync/log/history.log 
			fi

			if [ "x$STATUS" = "x" ]
			then
				echo " "
				echo "checking version and licenses...."
				#bug 9325, lingx, 20051117
				$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
				#bug 9325, end

				peerv=`cat /tmp/ca.version | grep "^Array.*build" | cut -f2 -d" "`
				mymajorv=`echo $myv |cut -f3-4 -d"."`
				peermajorv=`echo $peerv |cut -f3-4 -d"."`
				
				if [ "$peermajorv" = "$mymajorv" ]
				then
					echo "$myv  $peerv"
				else
					if [ -n "$peerv" ]
					then
						echo "Version mismatches, synconf is aborted"
						echo "This > $myv"
						#bug 7245 donghy 20050206
						#print peer name instead of dest ip address
						#echo "$dst_ip> $peerv"
						echo "$4 > $peerv"
						#end of bug 7245
						echo "$DATE % $dst_ip % error % version mismatches % $peerv" >> /ca/sync/log/history.log 
						echo " "
						if [ ! -f /ca/sync/bipass ]
						then
							#exit 1
							continue
						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
						continue
					fi

					if [ "$feature" = "$myFeature" ]
					then
						echo "$feature"
					else
						echo " "
						echo "..........Warning, feature mismatch..........."
						echo "This> = $myFeature"
						echo "  To> = $feature"
						echo "..........Warning, feature mismatch..........."
						echo " "
						echo "$DATE % $dst_ip % error % feature mismatch $feature" >> /ca/sync/log/history.log
						if [ ! -f /ca/sync/bipass ]
						then
							#exit 1
							continue
						fi
					fi

					if [ "$2" = "from" ]
					then
						#take the snap shot from the current
						#config instead of a running config for
						#the "sync config from", this allows
						#the from to work even when the source 
						#is in config mode.
                        if [ "$7" != "YES" ]
                        then
                          #IMPORTANT: (kkhauv 08/29/2014) Caller of this script must pass 'NO' to allow '/ca/sync/sync' below to run on non-FIPS system.
                          # On FIPS system, which $7='YES' passed in from caller, this '/ca/sync/sync' call below is deferred and will be called in call function in 'sync_ui.c' file instead.
                           echo "sync config for $dst_ip starts..."

						/ca/sync/sync $dst_ip $2 "" $6 $9 > /ca/sync/log/$dst_ip.log		
					else
	   				      #For FIPS case, need to create a one-line file for '/ca/sync/log/from' so that it passes the existence/read check in 'sync_ui.c' for 'sync from' case.
	   				       echo " " >> /ca/sync/log/from
						fi		
					else
						#bug 9325, lingx, 20051117
						echo "sync config for $dst_ip starts..."

						$SYNC_CMD $SYNC_OPT $dst_ip "/ca/sync/sync $2 $3 \"\" $6 $dst_ip > /ca/sync/log/$2.log" 
						#bug 9325, end
						echo " "
					fi
				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
