#!/usr/bin/ksh
# Script to recreate a symlink which are create by rpm.rte install.
# Symlink is removed, if users install an rpm package which overwrites
# the symlink created by rpm.rte and then later rpm package been removed.

# This scripts can be used to recreate the symlink to avoid recreating the
# links manually or if we don't want to install an rpm package.
 
optlib="/usr/opt/freeware/lib"
optbin="/usr/opt/freeware/bin"
newbp="/usr/opt/rpm/bin"
newlp="/usr/opt/rpm/lib"

/usr/bin/echo "bunzip2 bzcat bzip2 bzip2recover gendiff\
 gzexe gzip gunzip zcat install-info patch zcmp zgrep zmore\
 znew zdiff zegrep zfgrep zforce zless\
 file" | /usr/bin/tr " " "\n" | while read inst_bin
do
    if [ -e ${optbin}/${inst_bin} ] # File is already present
    then
	/usr/opt/freeware/bin/rpm -qf ${optbin}/${inst_bin} > /dev/null 2>&1
	if [ $? -ne 0 ] && [ ! -L ${optbin}/${inst_bin} ] && [ -e ${newbp}/${inst_bin} ] # Not owned by an rpm package.
	then
	    /usr/bin/echo "Creating symlink for ${optbin}/${inst_bin}"
	    /usr/bin/ln -sf ${newbp}/${inst_bin} ${optbin}/${inst_bin}
	fi
    elif [ ! -e ${optbin}/${inst_bin} ] && [ -e ${newbp}/${inst_bin} ]
    then
	# File isn't present.
	/usr/bin/echo "Creating symlink for ${optbin}/${inst_bin}"
	/usr/bin/ln -sf ${newbp}/${inst_bin} ${optbin}/${inst_bin}
    fi
done


/usr/bin/echo "libbz2.a libintl.a libdb-4.8.a libdb-4.8.so\
 libdb-4.so libdb.so libdb.a libdb4.a libdb-4.a libmagic.a\
 liblua.a liblua-5.1.so libnspr4.a libnspr4.so libplc4.a libplc4.so\
 libplds4.a libplds4.so libfreebl3.a libfreebl3.so libnss3.a libnss3.so\
 libnssckbi.a libnssckbi.so libnssdbm3.a libnssdbm3.so libnssutil3.a\
 libnssutil3.so libsmime3.a libsmime3.so libsoftokn3.a libsoftokn3.so\
 libsqlite3.a libsqlite3.so libssl3.a libssl3.so libpopt.a libpopt.so.0\
 libpopt.so libpopt.so.0.0.0 libreadline.a libreadline.so.6 libhistory.a\
 libhistory.so.6 libz.a" | /usr/bin/tr " " "\n" | while read inst_lib
do
    if [ -e ${optlib}/${inst_lib} ] # File is already present
    then
	/usr/opt/freeware/bin/rpm -qf ${optlib}/${inst_lib} > /dev/null 2>&1
	if [ $? -ne 0 ] && [ ! -L ${optlib}/${inst_lib} ] && [ -e ${newlp}/${inst_lib} ] # Not owned by an rpm package.
	then
	    /usr/bin/echo "Creating symlink for ${optlib}/${inst_lib}"
	    /usr/bin/ln -sf ${newlp}/${inst_lib} ${optlib}/${inst_lib}
	fi
    elif [ ! -e ${optlib}/${inst_lib} ] && [ -e ${newlp}/${inst_lib} ]
    then
	# File isn't present.
	/usr/bin/echo "Creating symlink for ${optlib}/${inst_lib}"
	/usr/bin/ln -sf ${newlp}/${inst_lib} ${optlib}/${inst_lib}
    fi
done 
