#!/bin/ash
#
# compute_file_diffs:
# This script computes the list of files that have been added, removed or changed
# between the old and the new versions.
# It computes file diffs for both regular files and symlinks.
#
# Usage: compute_file_diffs <dest_partition> <install_type>
#        <dest_partition> = partition in which fw is going to be installed
#                           can be "primary" or "backup"
#        <install_type>   = eth (for ethernet based update) or sp (via ONTAP)
# 
# If <dest_partition> = primary, the differences are computed between the new 
#                       FW package and the primary partition.
# If <dest_partition> = backup, the differences are computed between the primary
#                       and the backup partitions.
# 
# Kiran Kamity, 09/24/2004
#
# Return Values:
#     101 = Invalid arguments
#     102 = Metadata info not present in new package
#     103 = Metadata Info not present in destination partition
#     104 = Miscellaneous Error
#

ETH_FW_PACKAGE_DIR=/tmp/fud_eth/RLM_FW
SP_FW_PACKAGE_DIR=/tmp/ORFTP/fud_sp

# Compute regular files that have been added, changed or deleted

#
# Regfile_diffs: 
# Computes the regular file diffs necessary for differential file based update
# Args:
#     $1 = New regfiles
#     $2 = Current regfiles
#     $3 = Output file containing Regfiles added/changed
#     $4 = Output file containing Regfiles deleted
#
Regfile_diffs () {
#echo "Regfile_diffs $1 $2 $3 $4";
   
# Compute added and changed files

    diff $1 $2 > /tmp/fall;
	grep "<" /tmp/fall > /dev/null 2>&1;
	if [ $? = 0 ]; then
		grep "<" /tmp/fall | cut -d' ' -f4 > $3;
	fi
    
    
# Compute deleted files

	grep ">" /tmp/fall > /dev/null 2>&1;
	if [ $? = 0 ]; then
		grep ">" /tmp/fall | cut -d' ' -f4 > /tmp/ffpdel;
		grep -f /tmp/ffpdel $3 > /tmp/ffcommon;
		diff /tmp/ffpdel /tmp/ffcommon > /tmp/ffdel;
		grep "<" /tmp/ffdel > /dev/null 2>&1;
		if [ $? = 0 ]; then
			grep "<" /tmp/ffdel | cut -d' ' -f2 > $4;
		fi
	fi
 }

# Compute added, changed and deleted symlinks

#
# Symlink_diffs: 
# Computes the symlink diffs necessary for differential file based update
# Args:
#     $1 = New symlinks
#     $2 = Current symlinks
#     $3 = Output file containing symlinks added/changed
#     $4 = Output file containing symlinks deleted
#
Symlink_diffs () {
#echo "Symlink_diffs $1 $2 $3 $4";

# Compute added and changed symlinks

    diff $1 $2 | grep -q "<";
    if [ $? = 0  ] ; then
	diff $1 $2 | grep "<" | sed 's/< //g' >> $3;
#echo "add symlinks";
    fi
    
    
# Compute deleted symlinks

    diff $1 $2 | grep -q ">";
    if [ $? = 0 ] ; then
	DIFFS=`diff $1 $2 | grep ">" | sed 's/>//g'`;
	COUNT=0;
	for word in $DIFFS; do
	    if [ $(($COUNT%2)) -eq 0 ]; then
		grep -q $word $3 > /dev/null 2>&1;
		if ! [ $? = 0 ] ; then
		    echo $word >> $4;
		fi
	    fi
	    COUNT=$(($COUNT + 1));
	done
#echo "del symlinks";
    fi
}


# ...Program execution starts here...

# Verify command line arguments
if ! [ $# -eq 2 ] ; then
	echo "Usage: compute_file_diffs <dest_partition> <install_type>";
	exit 101;
fi

if [ $1 != "primary" ] && [ $1 != "backup" ] ; then
	echo "Usage: compute_file_diffs <dest_partition> <install_type>";
	exit 101;
fi

if [ $2 = "eth" ] ; then
    NEW_PACKAGE=$ETH_FW_PACKAGE_DIR
elif [ $2 = "sp" ] ; then
    NEW_PACKAGE=$SP_FW_PACKAGE_DIR
else
    echo "Usage: compute_file_diffs <dest_partition> <install_type>";
    exit 101;
fi


REGFILES=/etc/rlm_install/$1/checksum.regfiles
SYMLINKS=/etc/rlm_install/$1/info.symlinks

NEW_PACKAGE_REGFILES=$NEW_PACKAGE/install/checksum.regfiles
NEW_PACKAGE_SYMLINKS=$NEW_PACKAGE/install/info.symlinks
DIFFS_DIR=$NEW_PACKAGE/differences

REGFILES_ADD=$DIFFS_DIR/add.regfiles
REGFILES_DEL=$DIFFS_DIR/del.regfiles
SYMLINKS_ADD=$DIFFS_DIR/add.symlinks
SYMLINKS_DEL=$DIFFS_DIR/del.symlinks

mkdir -p $DIFFS_DIR;
rm -f $REGFILES_ADD;
rm -f $REGFILES_DEL;
rm -f $SYMLINKS_ADD;
rm -f $SYMLINKS_DEL;


# Determine which files/symlinks have been removed, added or changed
# compute_file_diffs between new package and target partition
if [ ! -f $NEW_PACKAGE_REGFILES ] ; then
    NEW_PACKAGE_REGFILES=$NEW_PACKAGE_REGFILES".0"
fi

if ! [ -f $NEW_PACKAGE_REGFILES -a \
	-f $NEW_PACKAGE_SYMLINKS ] ; then
	
	echo "ERROR: Metadata absent in new package";
	exit 102;
	
elif ! [ -f $REGFILES -a \
	-f $SYMLINKS ] ; then
	
	echo "ERROR: No Metadata Information in $1";
	exit 103;

else
	Regfile_diffs $NEW_PACKAGE_REGFILES $REGFILES $REGFILES_ADD $REGFILES_DEL;
	Symlink_diffs $NEW_PACKAGE_SYMLINKS $SYMLINKS $SYMLINKS_ADD $SYMLINKS_DEL;
fi
