#!/bin/ksh
#
# Written by:  Efraim Yosef
# Nick Chichasov: Automatic shutdown  after happy completion Apr 2001
# Nick Chichasov: Uninstall of the previous installations of the same update
# Nick Chichasov: Added automatic installation of ALL AIX filesets that resides
#                 at the same directory as Update itself
#
#
LOG=/scitex/version/logs/update_software_packages.log
TEMP_DIR=/tmp
#
#
#if 'noboot' -- do not reboot after Upd installation.
# This flag always the first . 'boot' by default
BOOT_FLAG=$1
if [ "${BOOT_FLAG:=boot}" = "noboot" ]; then 
 shift 
else
 BOOT_FLAG="boot"
fi
# if 'nomount' -- do not mount inside this script, get /mnt as is
# this flag always the second . 'mount' by default.
MNT_FLAG=$1
if [ "${MNT_FLAG:=mount}" = "nomount" ]; then 
 shift 
else
 MNT_FLAG="mount"
fi
# if list of updates is "" -- common fixes installed anyway.
LIST="$*"
# Check if Creo Application programs are running
ps -ef|grep /scitex/version/progs|grep -v grep > /dev/null 2>&1
if [ $? -eq 0 ]; then
  echo "FAILED: Creo Application programs are running!"
  echo "Please log out and log as 'root'"
  exit 127
fi
#
trap 'rm -f $TEMP_DIR/*[a-z]$$*; exit $status' 0
trap 'echo; echo "Interrupt received ... Aborted."; status=101' 1 2 3 15
##
# 
##
inst_of_update ()
{
echo `date` 
echo  Installation of \"$*\"
echo  "----------------------------------------"
echo  " "
#
typeset upd_list=$*
#
if [ ! -f /mnt/LABEL ]; then
if [ "$MNT_FLAG" = "mount" ]; then 
  mount | grep /cd0 1>/dev/null
  stat=$?
  if [ $stat -eq 0 ]; then
     umount /dev/cd0
  fi
  #
  if [ ! -d /mnt ]; then
    mkdir /mnt
  fi
  #
  mount -o ro -v'cdrfs' /dev/cd0 /mnt 2>/dev/null
  if [ $? -ne 0 ]; then
    echo "Mount of CD-ROM FAILED"
    return 101
  fi
fi
fi
#
# DEINSTALLATION of old Updates
#
if [ "$upd_list" ]; then
   installp -u $upd_list 2>&1 
   if [ $? -ne 0 ]; then
     return 102
   fi
fi
#
# INSTALLATION of COMMON AIX_FIXES (Every Update install them)
# INSTALLATION 
#
ls -d /mnt/*AIX_ML >/dev/null 2>&1
#
if [ $? -eq 0 ]; then
  cd /mnt/*AIX_ML
# install BASE packages (APPLIED MODE)
  lpps_I=`installp -LI -d . | awk -F: '{print $2}'`
  if [ "$lpps_I" ]; then
    installp -acgQqIX -d '.' $lpps_I
    if [ $? -ne 0 ]; then
      echo "Installation of BASE AIX images FAILURE...\n"
      return 103
    fi
  fi
# Update Latest     
  /usr/lib/instl/sm_inst installp_cmd -a -d '.' \
   -f '_update_all' '-g' '-X'
  if [ $? -ne 0 ]; then
    echo "Installation of INCREMENTAL AIX images FAILURE...\n"
    return 104
  fi
fi
#
# INSTALLATION of SPECIAL AIX FIXES
for i in $upd_list
do
  ls -d /mnt/${i}_AIX_FIX >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    cd /mnt/${i}_AIX_FIX
# Run Update config script
  [ -f "${i}.sh" ] && ./${i}.sh
# install BASE packages (APPLIED MODE)
    lpps_I=`installp -LI -d . | awk -F: '{print $2}'`
    if [ "$lpps_I" ]; then
      installp -acgQqIX -d '.' $lpps_I
      if [ $? -ne 0 ]; then
         echo "Installation of BASE AIX images FAILURE...\n"
         return 103
      fi
    fi  
# Update Latest     
    /usr/lib/instl/sm_inst installp_cmd -a -d '.' \
     -f '_update_all' '-g' '-X'
    if [ $? -ne 0 ]; then
      echo "Installation of INCREMENTAL AIX images FAILURE...\n"
      return 104
    fi
  fi
done
# #
# # INSTALLATION of Update
# # 
if [ "$upd_list" ]; then
  installp -qacFNXd/mnt $upd_list 2>&1 
  inst_stat=$?
fi
#
return $inst_stat
}
##
# MAIN
##
( inst_of_update $LIST 2>&1; echo $? > $TEMP_DIR/status$$ ) | tee -a $LOG
status=`cat $TEMP_DIR/status$$ 2>/dev/null`
#
if [ ${status:=1} -eq 0 ]; then
  echo " "
  echo " "
  echo "Installation is COMPLETED SUCCESSFULLY"
  echo "--------------------------------------------"
  if [ "$BOOT_FLAG" = "boot" ]; then 
    echo "The system will be REBOOTED IMMEDIATELY."
    echo "\n\n";echo "banner 'REBOOTING' > /dev/console"| at now >/dev/null 2>&1
    echo 'shutdown -Fr' | at now >/dev/null 2>&1
    sleep 1000
  fi
else
  echo " "
  echo " "
  echo "Installation FAILED"
  umount /mnt
  if [ $? -ne 0 ]; then
    echo "Unmount of CD-ROM FAILED"
  fi 
fi
exit $status
