#!/bin/sh

#
# This script is executed before the patch is applied
#
#
#################################################################################
# Exit codes for installation scripts 
#################################################################################

e_ok=0	       # script completed successfully.
e_fatal=1      # stop installation on this exit
e_warning=2    # Installation will go on. 
e_int=3        # Interrupted. Stop installation
e_reboot=10    # User must reboot after installation of all selected packages
               # To be added to one of the single-digit exit code above
e_rebootnow=20 # User must reboot right after installation of current package
               # To be added to one of the single-digit exit code above
# Trap interrupt
trap `exit $e_int` 15

MYNAME=`basename $0`

LS="/bin/ls"
RM="/bin/rm"
LN="/bin/ln"
WC="/bin/wc"
TR="/bin/tr"
AWK="/bin/awk"
NAWK="/bin/nawk"
SED="/bin/sed"
CUT="/bin/cut"
ECHO="/bin/echo"
GREP="/bin/grep"
CHOWN="/bin/chown"
SORT="/bin/sort"
HEAD="/bin/head"
TAIL="/bin/tail"
PKGPARAM="/bin/pkgparam"
UNAME="/bin/uname"

#Name of the Calendar package
PKG="SUNWics5"

#Calendar Base directory
BASEDIR=`$PKGPARAM -R $ROOTDIR SUNWics5 BASEDIR`

#Absolute root directory
PKG_INSTALL_ROOT="$ROOTDIR"
if [ "$PKG_INSTALL_ROOT" = "/" ]; then
  PKG_INSTALL_ROOT=""
fi

#Get the platform infos
OS_PLATFORM=`$UNAME -m`
OS_VERSION=`$UNAME -r`

#Test for required patches
REQUIRED_PATCHES_58_sparc=""
REQUIRED_PATCHES_58_x86=""
REQUIRED_PATCHES_59_sparc="113713-11"
REQUIRED_PATCHES_59_x86="114568-02"

if [ "$OS_PLATFORM" = "i86pc" ] 
then
  #It's a PC
  case "$OS_VERSION" in
    '5.8')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_58_x86"
      ;;
    '5.9')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_59_x86"
      ;;
  esac
else
  #It's a sparc
  case "$OS_VERSION" in
    '5.8')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_58_sparc"
      ;;
    '5.9')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_59_sparc"
      ;;
  esac
fi

for PATCHID in $REQUIRED_PATCHES ;
do
  patchNum=`$ECHO $PATCHID | $CUT -d"-" -f1`
  patchRev=`$ECHO $PATCHID | $CUT -d"-" -f2`
  installedPatchID=`showrev -p | $GREP "^Patch: $patchNum" | $SORT -r | $HEAD -1 | $CUT -d" " -f2`
  if [ -z "$installedPatchID" ]
  then
    $ECHO "Required patch $PATCHID is not installed"
    $ECHO "Please install patch $PATCHID and try again"
    exit $e_fatal
  else
    installedPatchNum=`$ECHO $installedPatchID | $CUT -d"-" -f1`
    installedPatchRev=`$ECHO $installedPatchID | $CUT -d"-" -f2`
    if [ $installedPatchRev -lt $patchRev ] 
    then
      $ECHO "This patch requires a newer version of patch $installedPatchNum"
      $ECHO "The version currently installed is $installedPatchID"
      $ECHO "Please install $PATCHID or newer and try again"
      exit $e_fatal
    fi
  fi
done

#Calendar Config directory
CS_CONFIG_DIR="$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/config"

#Calendar config file
CS_CONFIG_FILE="$CS_CONFIG_DIR/ics.conf"

if [ -h $CS_CONFIG_DIR ]; then
  linktarget=`$LS -l $CS_CONFIG_DIR | $NAWK -F'-> ' '{print $2}'`
  CS_CONFIG_FILE="$ROOTDIR$linktarget/ics.conf"
fi

if [ -z "$PKG_INSTALL_ROOT" ] 
then
  #Counts the number of process running
  NB_RUNNING_PROCESSES=`ps -ef | $GREP $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal | $GREP -v grep | $WC -l | $TR -d ' '`

  if [ $NB_RUNNING_PROCESSES -gt 0 ]
  then
    if [ -f $CS_CONFIG_FILE ]
    then
      # Check for HA settings in the config file
      HA_ENABLED=`$GREP '^local.server.ha.enabled' $CS_CONFIG_FILE | $TAIL -1| $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`
      HA_AGENT=`$GREP '^local.server.ha.agent' $CS_CONFIG_FILE | $TAIL -1| $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`
      if [ "$HA_ENABLED" != "yes" -a HA_AGENT != "SUNWscics" ]
      then
        # If it not HA we can stop the service safely
        $ECHO "Shutting down the services"
        cd $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/sbin
        $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/sbin/stop-cal
      else
        $ECHO "There is currently $NB_RUNNING_PROCESSES Calendar Server process(es) running."
        $ECHO "All services must be shutdown in order to process."
        $ECHO "Please shutdown every services, and try again."
        exit $e_fatal
      fi
    else
      $ECHO "Cannot find the product configuration file $CS_CONFIG_FILE"
      exit "$e_fatal"
    fi
  fi
fi

if [ -f $CS_CONFIG_FILE ]
then
  #List of the config parameters pointing to a DB directory
  DB_PATH_PARAMS="caldb.berkeleydb.homedir.path caldb.cld.cache.homedir.path local.ldap.cache.homedir.path"

  for db_path_param in $DB_PATH_PARAMS;
  do
    DB_DIR=`$GREP "^$db_path_param" $CS_CONFIG_FILE | $TAIL -1| $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`
    if [ -z "`$ECHO $DB_DIR | $GREP '^/'`" ]
    then
      #path is relative to lib
      DB_DIR=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib/$DB_DIR
    else
      #path is absolute
      DB_DIR=$PKG_INSTALL_ROOT$DB_DIR
    fi
    
    #Execute database cleanup only if there has been an upgrade
    current_db_version=`LD_LIBRARY_PATH=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/tools/unsupported/bin/db_stat -V -h $DB_DIR | $CUT -d":" -f2 | $SED "s/^ *//" | $SED "s/ *$//" | /$CUT -d" " -f3`
    major_current_db_version=`$ECHO $current_db_version | $CUT -d"." -f1`
    if [ $major_current_db_version -lt 4 ]
    then

      if [ -d $DB_DIR ]
      then
        #Check if there is db files before executing db_recover
        if [ -f $DB_DIR/*.db ]
        then
          #Restore the database to a consistent state
          LD_LIBRARY_PATH=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/tools/unsupported/bin/db_recover -h $DB_DIR
        fi

        #Remove the __db* files in the db directory
        if [ -f $DB_DIR/__db.* ]
        then
          $ECHO "Removing Berkeley DB shared memory files in $DB_DIR"
          $RM -f $DB_DIR/__db.*
        fi
      fi    
    fi
  done

  #Session DBs are handled separatly since we need to append session to the directory specified in ics.conf
  SESSION_DB_PATH_PARAMS="service.http.sessiondir.path service.admin.sessiondir.path"
  for db_path_param in $SESSION_DB_PATH_PARAMS;
  do
    DB_DIR=`$GREP "^$db_path_param" $CS_CONFIG_FILE | $TAIL -1| $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`
    if [ -z "`$ECHO $DB_DIR | $GREP '^/'`" ]
    then
      #path is relative to lib
      DB_DIR=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib/$DB_DIR/session
    else
      #path is absolute
      DB_DIR=$PKG_INSTALL_ROOT$DB_DIR/session
    fi
    
    #Execute database cleanup only if there has been an upgrade
    current_db_version=`LD_LIBRARY_PATH=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/tools/unsupported/bin/db_stat -V -h $DB_DIR | $CUT -d":" -f2 | $SED "s/^ *//" | $SED "s/ *$//" | /$CUT -d" " -f3`
    major_current_db_version=`$ECHO $current_db_version | $CUT -d"." -f1`
    if [ $major_current_db_version -lt 4 ]
    then

      if [ -d $DB_DIR ]
      then
        #Check if there is db files before executing db_recover
        if [ -f $DB_DIR/*.db ]
        then
          #Restore the database to a consistent state
          LD_LIBRARY_PATH=$PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/lib $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/tools/unsupported/bin/db_recover -h $DB_DIR
        fi

        #Remove the __db* files in the db directory
        if [ -f $DB_DIR/__db.* ]
        then
          $ECHO "Removing Berkeley DB shared memory files in $DB_DIR"
          $RM -f $DB_DIR/__db.*
        fi    
      fi
    fi
  done
fi

exit $e_ok
