#!/bin/ksh   -up
#!
#
# Rob Blader K55
#
# file_verify
#
# This script is invoked from read_xfer, the file that read the tape 
# containing tar files to be transferred between 2 non-connected networks.
#
# Once the files are copied to target network, we want to check that
# files/directories have same access modes and ownership as they
# did on the source network.
#
# Included with the files we transfered, is a long listing of
# what they looked like originally.
#
# We'll run the verify script and compare the 2 listings.
#
#
# Declarations:
#

export PATH='/usr/bin:/usr/ucb:/usr/sbin'
XFER_DIR=/auto/admin/net_xfer
USER=$1
SOURCE_LS=$XFER_DIR/xfer_verify.$USER.out
SOURCE_LS2=$XFER_DIR/xfer_verify.$USER.out2
TARGET_LS=$XFER_DIR/xfer_verify.$USER.out_ts
TARGET_LS2=$XFER_DIR/xfer_verify.$USER.out_ts2
VERIFY_SCRIPT=$XFER_DIR/xfer_verify.$USER

cp $SOURCE_LS $SOURCE_LS2

#
# By uncommenting the next 3 function calls,  we control whether we really
# perform chmod, chown and chgrp commands or just output what we would
# do. Uncomment if just want to see what needs changing, but don't really want 
# to update anything.  

#chmod()
#	{
#		echo "I would be doing this chmod $@"
#	}
#chown()
#	{
#		echo "I would be doing this chown  $@"
#	}
#chgrp()
#	{    
#    	 echo "I would be doing this chgrp  $@"
#	}
#

#
# Create the TARGET listing:
# 

$VERIFY_SCRIPT > $TARGET_LS

#
cp $TARGET_LS $TARGET_LS2
/usr/ucb/mail -s "FILE transfer verification" $USER < $TARGET_LS2

while [ true ]; 
	do
	read TARGET < $TARGET_LS
		case $? in
		0)
			;;

		*) echo "you reached end of file or an error"
			exit 0
			;;
		esac

     sed 1d $TARGET_LS > $TARGET_LS.tmp
     mv -f $TARGET_LS.tmp $TARGET_LS
 
#
# Compare results to SOURCE ls listing
#
	read SOURCE_LS_LINE < $SOURCE_LS
	
sed 1d $SOURCE_LS > $SOURCE_LS.tmp
     	mv -f $SOURCE_LS.tmp $SOURCE_LS

/usr/bin/echo "TARGET:\n$TARGET"
	/usr/bin/echo "SOURCE:\n$SOURCE_LS_LINE"
	
#
# Assume for now that things work right and just compare fields of
# interest

#
# We reach this part we know something is different for this particular
# line.  We are only interested in access mode, owner and group.
#

#
# Squeeze out multiple blanks to make it easier to extract fields
#
	TARGET_SHORT=`echo $TARGET | tr -s `
	SOURCE_SHORT=`echo $SOURCE_LS_LINE | tr -s `

	TARGET_MODE=`echo $TARGET_SHORT | cut -f1 -d" "`
	TARGET_OWNER=`echo $TARGET_SHORT | cut -f3 -d" "`
	TARGET_GROUP=`echo $TARGET_SHORT | cut -f4 -d" "`
	TARGET_FILE=`echo $TARGET_SHORT | cut -f9 -d" "`

/usr/bin/echo "Mode: $TARGET_MODE \t Owner: $TARGET_OWNER \t Group: $TARGET_GROUP \t File: $TARGET_FILE"

	SOURCE_MODE=`echo $SOURCE_SHORT | cut -f1 -d" "`
	SOURCE_OWNER=`echo $SOURCE_SHORT | cut -f3 -d" "`
	SOURCE_GROUP=`echo $SOURCE_SHORT | cut -f4 -d" "`
	SOURCE_FILE=`echo $SOURCE_SHORT | cut -f9 -d" "`

/usr/bin/echo "Mode: $SOURCE_MODE \t Owner: $SOURCE_OWNER \t Group: $SOURCE_GROUP \t File: $SOURCE_FILE"
#
#
# First verify that the file names are the same or else bail with the
# whole thing
#
	if [[ $TARGET_FILE != $SOURCE_FILE ]]; then
		echo "SOURCE file name is $SOURCE_FILE"
		echo "TARGET file name is $TARGET_FILE"
		echo " "
		echo "File names do not match up between source and target"
		echo "listings.  Bailing!"
		exit 0
	fi

#
# Compare the modes
#

	if [[ $SOURCE_MODE != $TARGET_MODE ]]; then
	chmod 000 $TARGET_FILE

#		Just in case the "lock bit is set, make sure to unset it"
# 		If a regular file is setGID and not executable, mandatory locking 
# 		will be in effect for the file.  See chmod (2) for more details

	if [ -d $TARGET_FILE ]; then
		chmod g-l $TARGET_FILE
	fi

#  skip the first bit that indicates if the file is a link, directory, 
# or plain file

	let index=2
	while [ $INDEX -le 10 ]; 
	do

		MODE_BIT=`echo $SOURCE_MODE | cut -c $INDEX`
		if [ $MODE_BIT != '-' ]; then
	
		case $INDEX in
	
		2|3)
			chmod u+$MODE_BIT $TARGET_FILE
				;;
		4)
			case $MODE_BIT in

				s) chmod u+x $TARGET_FILE
				   chmod u+s $TARGET_FILE
					;;
				x)
				   chmod u+x $TARGET_FILE
					;;
			esac
					;;

		5|6)
			 chmod g+$MODE_BIT $TARGET_FILE
				;;	
#
# Some code to handle GID bit
#
		7)
			case $MODE_BIT in

#
# On directories, an l means set GID without group execution.
#
				s|l)
					chmod g+x $TARGET_FILE
					chmod g+s $TARGET_FILE
						;;
				x)	chmod g+x $TARGET_FILE
						;;
			esac


				;;	
		
		8|9)

			chmod o+`echo $SOURCE_MODE | cut -c $INDEX` $TARGET_FILE
				;;
		10) 
#
# The sticky bit complicates this scheme because we have to 
# add "t" to owner, not other.
#
			case $MODE_BIT in

				x)
	
					
chmod o+`echo $SOURCE_mode | cut -c $index` $TARGET_FILE
						;;
				t)
	
					chmod o+x $TARGET_FILE
					chmod u+t $TARGET_FILE
						;;
	
				T)
				
					chmod u+t $TARGET_FILE
						;;
			esac	

		esac

	
	fi
		let INDEX=$INDEX+1
	done
	fi # end mode comparison

#
# Compare ownership
#
	if [[ $TARGET_OWNER != $SOURCE_OWNER ]]; then
		chown $SOURCE_OWNER $TARGET_FILE
	fi

#
# Compare group ownership
#
	if [[ $TARGET_GROUP != $SOURCE_GROUP ]]; then
		chgrp $SOURCE_GROUP $TARGET_FILE
	fi

#	fi # end difference check


	done


