#! /bin/sh
#
# Script to check if SUNWxcu4 pkg is installed on the system. 
# If so, proceed to install the patch.  If not, check if SUNWxcu4
# prerequisite pkgs are on the system and then auto install the
# SUNWxcu4 FCS base pkg that comes with this patch and continue
# to install the libc patch.  This will be the default behavior.
# 
# Echo appropriate warning and error messages. 
# Exit 1 if all prereq pkgs are not on the system or if something
# Goes wrong with SUNWxcu4 pkg install. 
# 
#
# This script also saves the original /etc/pam.conf file so that
# customizations made are not lost when the new pam.conf 
# upgrade changes are made. 
#
#

/usr/bin/mkdir /tmp/"$PatchNum"_$$

TMPDIR=/tmp/"$PatchNum"_$$
ADMIN="$TMPDIR"/admin_$$
PKGDIR="$patchdir"
PKG_STREAM="SUNWxcu4.stream.pkg"


save_pamconf () {

#       Save the original pam.conf file
#

        if [ -f $ROOTDIR/etc/pam.conf ]; then
	echo
	echo "    NOTICE: The following file is being changed by this patch:"
       	echo "  "
       	echo "                /etc/pam.conf"
        echo "\n"
        echo "    A copy of the previous version has been saved under the name:"
        echo "\n"
        echo "               /etc/pam.conf.pre$PatchNum"
        echo "\n"
        echo "    You must merge your previous changes with the new version of"
        echo "    the file modified by this patch before the system is" 
        echo "    rebooted."
        echo "\n"
	echo
	fi

}

check_ldconfig() {

if [ ! -f $ROOTDIR/usr/lib/passwdutil.so.1 -a \
    -f $ROOTDIR/var/ld/ld.config -o \
    ! -f $ROOTDIR/usr/lib/sparcv9/passwdutil.so.1 -a \
    -f $ROOTDIR/var/ld/64/ld.config ]; then
cat <<EOF

    NOTICE: The target system may have been configured to use a runtime
    linking configuration cache (see crle(1)).  The crle(1) configuration
    file needs to be updated by running the command:

    # crle -u 

    as the root user before the system is rebooted.

EOF
fi

}

check_core_pkgs () {  

echo "Attempting to install optional package SUNWxcu4."
echo "    Verifying prerequisite packages ....."
echo ""

pkgs_not_installed=""
for pkg_there in  SUNWcar SUNWkvm SUNWcsr SUNWcsu SUNWcsd SUNWcsl 
do
   pkginfo -R $ROOTDIR -q $pkg_there 
   if [ $? -eq 0 ]
   then
       :
   else
       pkgs_not_installed="notfound"
   fi 
done
  
if [ -z "$pkgs_not_installed" ]
then
         echo "Prerequisite pkgs verified."
else 
         echo "ERROR: One or more of the following prerequisite packages "
         echo "       is not installed on the system:"
         echo "      "  
         echo "       SUNWcar SUNWkvm SUNWcsr SUNWcsu SUNWcsd SUNWcsl" 
         echo ""
         echo "End patch install."
         echo ""
         echo
         exit 1
fi 


}

build_admin_file () {

	cat > $ADMIN << EOF
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=quit
setuid=nocheck
conflict=nocheck
action=nocheck
EOF

}

install_pkg ()  {

echo "Installing SUNWxcu4 package...."
echo ""

# Run pkgadd to install the optional package. 

/usr/sbin/pkgadd -R $ROOTDIR -a $ADMIN -S -d $PKGDIR/$PKG_STREAM all > /dev/null 2>&1
[ $? != 0 ] && echo "SUNWxcu4 failed to install!!" \
			&& "Please check the filesystem for corruption." \
			&& /usr/bin/rm -fr $TMPDIR \
			&& exit 1


}


run_all () {


# Check if correct version of SUNWxcu4 is on the system.
# If so, do nothing and exit 0, if not exit 1.
# If not on system, run functions to verify prereqs and then
# install the SUNWxcu4 pkg.

    pkginfo -R $ROOTDIR -q SUNWxcu4 
    xcu4_there=$?
    
    if [ $xcu4_there -eq 0  ] 
    then
          set_version="11.8.0,REV=2000.01.08.18.12" 
          version_check="`/usr/bin/pkgparam -R $ROOTDIR -f $ROOTDIR/var/sadm/pkg/SUNWxcu4/pkginfo VERSION`"
          if [ "$set_version" = "$version_check" ] 
          then 
              save_pamconf
              check_ldconfig
              exit 0
          else 
              echo ""
              echo "ERROR: Installed version of SUNWxcu4 is not FCS version." 
              echo ""
              exit 1
          fi
    else 
          check_core_pkgs
          build_admin_file
          install_pkg
          save_pamconf
          check_ldconfig
     fi       

[ -d "$TMPDIR" ] && /usr/bin/rm -fr $TMPDIR
 
exit 0
}



#
# Asks user if they would like to continue.  It will timeout in
# SLEEP_TIME and installation will continue if no response is given.
#

SLEEP_TIME=60
times_up()
{
   
   run_all 

}

#
# Re-Rooted install doesn't need run_all only save_pamconf
#

if [ "$ROOTDIR" != "/" ]
then
   save_pamconf
   exit 0
fi

trap 'times_up' ALRM
( sleep $SLEEP_TIME ; kill -ALRM $$ ) &
child_pid=$!

echo "Do you wish to continue this installation {yes or no} [yes]? \\c"
echo "\n(by default, installation will continue in $SLEEP_TIME seconds)"

read Response
while [ 1 ]
do
        case $Response in
                n | no | N | NO | No)
 
                        kill -9 $child_pid 2> /dev/null
                        exit 1 ;;

                "" | y | yes | Y | Yes | YES)
                         
                        
                        kill -9 $child_pid 2> /dev/null 

	                run_all	

                        exit 0 ;;

                *)  echo "yes or no please. \\c"
                        read Response ;;
        esac
done


