#!/bin/ksh -p
#
# This script is designed to compare the contents of two directory 
# locations and copy the contents of the first directory, and the 
# differences between the two directories, to a third directory. 
# The script needs to be passed the mounted path of the first directory, 
# the mounted path of the reference directory, and the mounted path to the 
# final third destination directory. 
#
#
#       @(#)comb_pkgs 1.1.3 00/10/17 SMI
#       @(#)comb_pkgs 1.1.2 00/10/06 SMI
#       @(#)comb_pkgs 1.1.1 00/10/04 SMI
#       @(#)comb_pkgs 1.1 00/09/11 SMI
#       @(#)comp_src 1.0 00/09/06 SMI
#
# Copyright (c) 2000 by Sun Microsystems, Inc.
# All rights reserved
#
#  
#

	USAGE='USAGE: comb_pkgs -r <path_to_reference_dir> -s <path_to_src_product_dir> -d <path_to_destination_dir>\n 
	    \nwhere:\n 
            -r path_to_reference_dir - directory containing the reference product image\n 
            -s path_to_src_product_dir - directory to be compared with the reference directory\n
	    -d destination_dir - full path to the directory for the combined directories
	'
	(($#<1)) && { print $USAGE; exit 1; }

#
# verify user is root
#
	/usr/bin/id | grep root >/dev/null 2>&1
	if [ "$?" != "0" ]; then
            print "$0 must be run as root."
            exit 1
	fi

#
# Initialize the variables
#
	PATH=/usr/sbin:/usr/bin
	REF_DIR=
	SRC_DIR=
	DEST_DIR=
	XFERED=
	DLIST1=/tmp/dir1.list
	DLIST2=/tmp/dir2.list
	COMP_LIST=/tmp/comp.list

#
# Test to make sure the user passed the correct args
#
	while getopts r:s:d: FLAG
	do
            case $FLAG in
                r )   REF_DIR="$OPTARG"
                      ;;
                s )   SRC_DIR="$OPTARG"
                      ;;
                d )   DEST_DIR="$OPTARG"
                      ;;
                * )   print "$USAGE"
                      exit 1
                      ;;
            esac
	done
#
#  Check to makesure the directories exist and the permissions are
#  set correctly for each directory.
#
	[[ -d ${REF_DIR} || -r ${REF_DIR} ]]||{ print "\n"${REF_DIR} is not readable \
	    or does not exist. Please correct this and re-run this script."\n"; \
	    exit 1; \
	}
#
	[[ -d ${SRC_DIR} || -r ${SRC_DIR} ]]||{ print "\n"${SRC_DIR} is not readable \
	    or does not exist. Please correct this and re-run this script."\n"; \
	    exit 1; \
	}
#
	if ([ -d ${DEST_DIR} ] && [ -w ${DEST_DIR} ]); then
    	    continue 
	else	     
    	    print "\nYour destination directory, ${DEST_DIR}, does not exit.";
            print "\nCreating ${DEST_DIR} and setting the permissions, if possible.";
    	    mkdir $DEST_DIR;
     	    chmod 777 $DEST_DIR;
	fi
#
# If we made it here we are ready to start the coping of the new pkgs 
# to the destination dir.
#
	print "\nStarting transfer now: `date` \n"
	cd $SRC_DIR
	find . | cpio -pdum ${DEST_DIR} 2>> ${DEST_DIR}/xfer_status.out
	print "Source dirctory contents are now installed at: ${DEST_DIR}"
	print "\nDONE\n"
#
# Create a list file of all the pkgs for the Reference dir image and the 
# Source directory images to be compared.
#
	ls -1 $REF_DIR > $DLIST1
	ls -1 $SRC_DIR > $DLIST2
#
# Compare the Reference dir image to the Source directory image.
# Then move the differences of the compare to the destination dir.
#
	comm -23 ${DLIST1} ${DLIST2} > ${COMP_LIST}
#
# Now there should be a good usable list of the directory differences
# so copy them over to their final home.
#
# Log all package names that were not in the src build dir in the 
# log file xfered.list
#
	XFERED=${DEST_DIR}/xfered.list
	print "Now coping all missing FCS packages to: ${DEST_DIR} \n"
	ITEM=""
	cd $REF_DIR
	for ITEM in `cat ${COMP_LIST}`
	do
    	    [[ -f ${ITEM}/pkgmap ]]&&{ print "Now coping package: ${ITEM}"; 
    	    find ./${ITEM} | cpio -pdum ${DEST_DIR} 2>> ${DEST_DIR}/xfer_status.out;
    	    print "\n package: ${ITEM} successfully copied to: ${DEST_DIR}\n"; 
    	    print ${ITEM} >> ${XFERED}; 
    	    }
	done
#
# Check for the non standard pkgs and if present copy them to the destination dir.
#
	cd $REF_DIR
	if [ -d ${REF_DIR}/CDKinfo.wos ]; then 
	    find ${REF_DIR}/CDKinfo.wos | cpio -pdum ${DEST_DIR} 2>> ${DEST_DIR}/xfer_status.out;
	    print CDKinfo.wos >> ${XFERED};
	    print "Now coping non standard SVR4 package";
	fi
	if [ -d ${REF_DIR}/SUNWsolnm ]; then
	    find ${REF_DIR}/SUNWsolnm | cpio -pdum ${DEST_DIR} 2>> ${DEST_DIR}/xfer_status.out;
	    print SUNWsolnm >> ${XFERED};
	    print "Now coping non standard SVR4 package";
	fi
	print "DONE\n"
	print "Finished coping all packages at: `date` \n"
#
# Now clean up the /tmp dir
#
	print "Now cleaning up temporary files"
	rm -rf ${DLIST1} ${DLIST2} ${COMP_LIST}
	print "DONE\n"
	print "To see a list of packages that were missing from your"
	print "Volume Source Product build that have been added to the"
	print "destination directory: ${DEST_DIR}"
	print "Please review the following file, xfered.list, located at the destination"
	print "directory location you specified: ${DEST_DIR}"
	exit 0
