#!/bin/bash

exitRC=0
logFile=/tmp/hmc_install.log

#-------------------------------------------------------------------------------
# Common exit point
#-------------------------------------------------------------------------------
exit_cleanup() {

   # keep the log file, but ensure a different user can overwrite it next time
   chmod 666 $logFile

   exit $1$exitRC
}

#-------------------------------------------------------------------------------
# RPM  install
#-------------------------------------------------------------------------------
dorpm() {

   if [ -f /opt/hsc/data/config/NO_UPDATE_RPMS ]
   then
     x="$2"
# Strip version then leading directory name
     f=`echo ${x%%-[0-9]*}`
     r=`echo ${f##*/}`
     for i in `cat /opt/hsc/data/config/NO_UPDATE_RPMS`
     do
       if [ "$r" = "$i" ]
       then
          return
       fi
     done
   fi
     
   # Log the test install output. The format for the "normal" RPM install
   # processing inside this script is 'rpm -i <file spec> --force --nodeps'
   rpm $* 2>> $logFile
   RC=$?
   return $RC
}

#-------------------------------------------------------------------------------
# Start the product install...
#-------------------------------------------------------------------------------
cd /
image=$1

if [ "$image" = "" ]
then
   echo "Please specify directory containing installable packages"
   echo "usage: installImages  <directory>"
   exit_cleanup 1
fi

# check if directory exists
if [ ! -d $image ]
then
 echo "The directory $patchdir doesn't exist"
 echo "Please specify directory containing the installable packages."
 exit_cleanup 2
fi

if [ -f /opt/hsc/data/config/NO_UPDATE_FILES ]
then
   rm -f /tmp/saved_files.tar
   cat /opt/hsc/data/config/NO_UPDATE_FILES | xargs tar -cvf /tmp/saved_files.tar
fi

PATH=$PATH:/opt/IBMJava/jre/bin:
LD_LIBRARY_PATH=/opt/hsc/lib:/opt/hsc/lib/hcmjni:/lib:/usr/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH

echo "--- Installing InfoCenter packages...."
if [ ! -f $image/info/IBMhmc.InfoCenter.ko_KR*rpm ]; then
   rpm -qi IBMhmc.InfoCenter.ko_KR >/dev/null 2>&1
   if [ $? -eq 0 ]; then
	rpm -e IBMhmc.InfoCenter.ko_KR --nodeps
   fi
fi


if [ -d $image/info ]
then
  dorpm -Uvh $image/info/IBMhmc.InfoCenter*rpm --force --nodeps
fi

exit_cleanup 0
