#!/bin/sh
#	$6	password
#	$7  local ip address

SYNC_CMD=/ca/bin/ssh
SYNC_OPT="-q -b $7 -p 65519 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o PasswordAuthentication=yes -o Sync=$6 -o CheckHostIp=no"
RSYNC_CMD=/ca/sync/rsync

checkAccess()
{
        ERROR=`grep "Access denied" /tmp/clear`
        if [ -n "$ERROR" ]
        then
                echo "Sync diff failed: Access $1 denied due to someone else in config mode"
                exit 1
        fi   
}

if [ "$1" = "summary" ]
then
	echo "                TIME	       IP (FROM)        STATUS  "
	echo " -----------------------------------------------------------" 
	source=`/bin/ls -lt /ca/sync/log/*.status 2> /dev/null | sed -e 's/^.*\///' -e 's/\.status//'`
	for i in $source
	do
		awk -F% '
			BEGIN {
				doneIt = 0; start_date = "none"; start_version = "none"
			}
			/completed/ {
				printf("%-30s | %-16s | success  \n", $1, $3);
				doneIt = 1;
			}
			/error/ {
				printf("%-30s | %-16s | failed  \n", $1, $3);
				doneIt = 1;
			}
			/start/ {
				start_date = $1
				start_version = $3
			}
			END {
				if (doneIt == 0) {
					printf("%-30s | %-16s | incomplete  \n", start_date, start_version);
				}
			}
			' /ca/sync/log/$i.status
	done
	exit
fi

show_status()
{
	if [ ! -f "$1" ]; then  
		echo "The system never performed synchronization from this peer. Please check the peer's IP address"
	else
		echo " "
		echo "Status for $1" | sed -e 's@/ca/sync/log/@@' -e 's@\.status@@'
		echo "----------------------"
		cat $1
		echo " "
		echo "----------------------------------------------"
		echo "Data Transfer and sync processing information"
		echo "----------------------------------------------"
		cat $2
	fi
}

if [ "$1" = "status" ]
then	
	if [ "$2" = "all" ]
	then
		for i in `/bin/ls /ca/sync/log/*.status`
		do
			show_status $i		
		done
	else
		check=`/bin/ls /ca/sync/log/*.status | grep '$2'`
		if [ -n "$check" ]
		then
			echo "Error: source $2 is not found"
			exit
		fi 		
		show_status /ca/sync/log/$2.status /ca/sync/log/$2.log
	fi
fi

if [ "$1" = "history" ]
then
	if [ -f /ca/sync/log/history.log ]
	then
		tail -n 50 /ca/sync/log/history.log | awk -F% '
		/start/ {
			printf("%-30s | %-16s | %-16s \n", $1, $2, $4);
			doneIt = 1;
		}
		/error/ {
			printf("%-30s | %-16s |  error \n %-16s %s \n", $1, $2, $4, $5);
			doneIt = 1;
		}
		'			
	fi 
fi

if [ "$1" = "diff" ]
then
	if [ -n "`echo $2|grep :`" ]
	then
		PING=ping6;
		ping_opt=""
		format_ip2="[""$2""]"
	else
		PING=ping;
		ping_opt="-t 3"
		format_ip2="$2"
	fi
	STATUS=`$PING -n -c 1 $ping_opt $2 | grep "100.*% packet loss"`
	if [ "x$STATUS" = "x" ]
	then
		$SYNC_CMD $SYNC_OPT $2 "/ca/sync/munch -x '/bin/rm -f /ca/conf/uconf/sync_90123456.cfg'" > /dev/null
		if [ $? -ne 0 ]
        	then
        		echo "Error: Wrong synconfig configuration"
            		exit 1
        	fi
		$SYNC_CMD $SYNC_OPT $2 "/ca/sync/munch -l 'write file sync_90123456'  -d $2  -f \"config\" "2>/tmp/clear
		if [ $? -ne 0 ]
        	then
        		echo "Unable to save configuration for $2"
            		exit 1
        	fi
		checkAccess $2
		$RSYNC_CMD -azo --rsh="$SYNC_CMD $SYNC_OPT" --rsync-path=$RSYNC_CMD $format_ip2:/ca/conf/uconf/sync_90123456.cfg /tmp/$2.conf 2> /tmp/rsync_err
		if [ $? -ne 0 ]
        	then
        		echo "Fail to synchronize the configuration file from $2 to local"
            		exit 1
        	fi
		$SYNC_CMD $SYNC_OPT $2 "/ca/sync/munch -x '/bin/rm -f /ca/conf/uconf/sync_90123456.cfg'" > /dev/null
		if [ $? -ne 0 ]
        	then
        		echo "Error: Wrong synconfig configuration"
            		exit 1
        	fi
		sed '/^Warning: Permanently added /d' /tmp/rsync_err > /tmp/rsync_err1
		if [ -f /tmp/rsync_err1 ]
		then
			error=`cat /tmp/rsync_err1`
			if [ -n "$error" ]
			then
				echo "Unable to retreive the configuration from $2"
				exit 1
			fi
		fi

		sed -f /ca/sync/stripfile /tmp/$2.conf | sort > /tmp/other.conf
		sed -f /ca/sync/stripfile /ca/conf/uconf/sync_1234567.cfg | sort > /tmp/my.conf
		diff -u /tmp/other.conf /tmp/my.conf | awk '
		/^\+/ {
			if ($1 != "+++" && $1 != "+#Last") {
				sep = substr($0, 2, 100);
				# huangkai 11/18/2004 Bug: 7255, add below 1 line.
                                if (sep != "")
        				printf("[added]   %s \n", sep);
			}
        	}
		/^-/ {
			if ($1 != "---" && $1 != "-#Last") {
				sep = substr($0, 2, 100);
				# huangkai 11/18/2004 Bug: 7255, add below 1 line.
                                if (sep != "")
        				printf("[missing] %s \n", sep);
			}
      		}
		'			
	else
		echo "Error: $2 is unreachable" 
	fi
fi

if [ "$1" = "clear" ]
then
	rm -fr /ca/sync/SC_* 2> /dev/null 
	rm -fr /ca/sync/log/*.log 2> /dev/null
	rm -fr /ca/sync/log/*.status 2> /dev/null
	if [ -f /ca/sync/log/history.log ]
	then
		cp /ca/sync/log/history.log /tmp/history  
		tail -n 50 /tmp/history.log > /ca/sync/log/history.log 
	fi	
fi
