#! /bin/sh

# This script is responsible for providing processing
# that follows a restore of critical console data on the HMC
# This scripts runs right after the data has been restored during
# boot and prior to reboot the HMC


# Handle kernel updates which result in kernel files
# such as bzImage, SystemMap etc.. being included in the
# critical console data backup. The net result is, we
# will have more than 1 set of kernel images.
# If there are kernel images backed up, the backup process
# will create a file /boot/new_hmc_kernel and made it part
# of the backup. As soon as this file is restored, it will signal
# that some cleanup need to be done.

if [ -f /boot/new_hmc_kernel ]; then
      # Some checking before proceeding
      kname=`cat /boot/new_hmc_kernel`
      kversion=`echo $kname | cut -c9-`
      if [ $kname = "" ]; then
         exit 0
      fi
      if [ ! -f /boot/$kname ]; then
         exit 0
      fi
      echo "New kernel restored from critical console data."
      echo "Switching to new kernel..."
      mount -n -o remount,rw /
      mkdir -p /boot/SAVED
      cp -p /boot/*$kversion* /boot/SAVED/
      rm /boot/bzImage-*
      rm /boot/Kerntypes-*
      rm /boot/symvers-hmc_mcp*
      rm /boot/System.map-*
      rm /boot/vmlinux-*
      mv /boot/SAVED/* /boot/
      rm -rf /boot/SAVED
      ln -sf /boot/bzImage-* /boot/bzImage
      rm -f /boot/new_hmc_kernel
      sleep 2
      sync
fi

# Correct the situation for bobcat package. When that product is installed (via a zip file),
# the entire /opt/bobcat directory is first removed.  Unfortunately, when doing a restore
# from DVD archive, we end up with situation of the verison installed from the recovery 
# image installed, then simply overlayed with any service pack un-tar'd files. The product
# is not sophistcated enough to "ignore" older files and this causes startup/run-time errors.
#
# Since we now may have a "mix" of old and new bobcat files, the true list of files for the
# current bobcat service that is archived (last installed, actually) is included in the file
# extbackupfile.list. So, what we'll do here is erase every file in /opt/bobcat that is *NOT*
# included in this list of files. Include symlinks too it appears...

x=`find /opt/bobcat -type f -o -type l`

# in case files other than bobcat are located in this file, filter them out...
grep "^/opt/bobcat" /opt/hsc/data/extbackupfile.list > /tmp/extbackupfile.list
reloadApps=0
for name in $x
do
    grep -q $name /tmp/extbackupfile.list
    if [ $? -ne 0 ]; then
        rm -f $name
	reloadApps=1
    fi
done
rm -f /tmp/extbackupfile.list
if [ $reloadApps -ne 0 ]; then
   echo "---- Starting server ......"
   /opt/bobcat/bin/startServer.sh server1
   echo -n "--- Removing old ASM Proxy .............."
   /opt/bobcat/bin/wsadmin.sh -conntype NONE -c "\$AdminApp uninstall asmproxy"
   echo "done"
   echo -n "--- Installing ASM Proxy ................"
   cp -p /opt/hsc/data/asmproxy.war /opt/bobcat/profiles/profile1/installableApps/

   ADMIN_DATA="{/opt/bobcat/profiles/profile1/installableApps/asmproxy.war}"
   ADMIN_DATA="$ADMIN_DATA { -usedefaultbindings -nodeployejb -appname"
   ADMIN_DATA="$ADMIN_DATA  asmproxy -contextroot asmproxy} "
   /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp install $ADMIN_DATA "
   echo " done"

   echo -n "--- Importing ASM Certificate ..........."
   /opt/bobcat/java/bin/keytool                            \
        -import                                             \
        -trustcacerts                                       \
        -alias asm                                          \
        -file /opt/hsc/data/asm.cer                         \
        -keystore /opt/bobcat/java/jre/lib/security/cacerts \
        -storetype jks                                      \
        -storepass changeit                                 \
        -noprompt
   echo " done"
   #System Plan Viewer: uninstall it if it exists, install it via wsadmin,
   #create lib dir for linked jars, then link in jars
   echo "--- Removing old System Plan Viewer ...."
   /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp uninstall sysplanviewer" >> $LogFile 2>&1

   echo -n "--- Installing System Plan Viewer ......."
   cp -p /opt/hsc/data/sysplanviewer.war /opt/bobcat/profiles/profile1/installableApps/
    
   ADM_DATA="{/opt/bobcat/profiles/profile1/installableApps/sysplanviewer.war}"
   ADM_DATA="$ADM_DATA { -usedefaultbindings -nodeployejb -appname"
   ADM_DATA="$ADM_DATA  sysplanviewer -contextroot sysplanviewer} "
   /opt/bobcat/bin/wsadmin.sh -conntype NONE -c \
        "\$AdminApp install $ADM_DATA "

   mkdir -p /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/
   ln -sf /usr/websm/codebase/pluginjars/csdml.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/

   ln -sf /usr/websm/codebase/pluginjars/hsc.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/
   ln -sf /usr/websm/codebase/pluginjars/aca.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/

   ln -sf /usr/websm/codebase/pluginjars/ldw_csdmlBeans.jar /opt/bobcat/profiles/profile1/installedApps/DefaultNode/sysplanviewer.ear/sysplanviewer.war/WEB-INF/lib/    

   echo "---- Stopping server ......"
   /opt/bobcat/bin/stopServer.sh server1
fi
exit 0
