#!/bin/ksh

# This script re-establishes hard links that were broken after
# the removal and reinstallation of the gzip package.
# When the source file was restored , the inode number
# was changed.  Therefore, we must reestablish the hard links
# and update the contents database with this information.


rem_file() {

	file=${rootdir}/$1
	pkg=$2

	if [ -f $file ] ; then
                /usr/bin/rm -f $file
		if [ "$?" = "0" ] ; then
			${REMOVEF}    $pkg $file > /dev/null 2>&1
			${REMOVEF} -f $pkg || exit 2
		fi
	fi
}


restore_link() {

	link=${rootdir}/$1
	linkto=$2
	pkg=$3

	if [ ! -a $link ] ; then
		## if there is no link, then restore orig link
		${INSTALLF}    $pkg $link=$linkto l > /dev/null 2>&1
		${INSTALLF} -f $pkg || exit 2
	fi
}

#
# Call installf/removef with the proper options to handle rerooted
# installations.

# If ROOTDIR = "/", we want to prevent the beginning of the absolute path
# name from being assigned "// instead of "/" in the restore_link function.

if [ "$ROOTDIR" = "/" ]; then
   rootdir=""
else
  rootdir=$ROOTDIR
fi


if [ "$rootdir" != "" ]; then
        INSTALLF="/usr/sbin/installf -R ${rootdir}"
        REMOVEF="/usr/sbin/removef -R ${rootdir}"
else
        INSTALLF="/usr/sbin/installf"
        REMOVEF="/usr/sbin/removef"
fi

rem_file usr/bin/gzcmp   SUNWgzip
rem_file usr/bin/gzegrep SUNWgzip
rem_file usr/bin/gzfgrep SUNWgzip
rem_file usr/bin/gunzip  SUNWgzip
rem_file usr/bin/gzcat   SUNWgzip
 
#     According to the installf man page, links should be specified as  path1=path2.  path1 indicates
#     the destination and path2 indicates the source file.  Can use "../../etc ..." notation to avoid
#     having to have to specify ROOTDIR.

restore_link usr/bin/gzcmp   ../../usr/bin/gzdiff SUNWgzip
restore_link usr/bin/gzegrep ../../usr/bin/gzgrep SUNWgzip
restore_link usr/bin/gzfgrep ../../usr/bin/gzgrep SUNWgzip
restore_link usr/bin/gunzip  ../../usr/bin/gzip   SUNWgzip
restore_link usr/bin/gzcat   ../../usr/bin/gzip   SUNWgzip

exit 0

