#!/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"
Sort="/bin/sort"
Head="/bin/head"
Tail="/bin/tail"
Patchadd="/usr/sbin/patchadd"
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-18 117724-10"
REQUIRED_PATCHES_59_x86="114568-17 117725-10"
REQUIRED_PATCHES_510_sparc=""
REQUIRED_PATCHES_510_x86=""

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"
      ;;
    '5.10')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_510_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"
      ;;
    '5.10')
      REQUIRED_PATCHES="$REQUIRED_PATCHES_510_sparc"
      ;;
  esac
fi

for PATCHID in $REQUIRED_PATCHES ;
do
  patchNum=`$Echo $PATCHID | $Cut -d"-" -f1`
  patchRev=`$Echo $PATCHID | $Cut -d"-" -f2`
  installedPatchID=`$Patchadd -p -R $ROOTDIR | $Grep "^Patch: $patchNum" | $Sort -r | $Head -1 | $Cut -d" " -f2`
  obsoletedBy=`$Patchadd -p -R $ROOTDIR | $Grep "Obsoletes:.* $patchNum-.* " | $Cut -d" " -f2`
  if [ -z "$obsoletedBy" ]
  then 
    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
  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 "$NB_RUNNING_PROCESSES Calendar Server process(es) is(are) currently running."
      $Echo "$MYNAME script cannot find the product configuration file $CS_CONFIG_FILE"
      $Echo "to check the server configuration and try to shut it(them) down."
      $Echo "All services must be shutdown in order to process."
      $Echo "Please shutdown every services, and try again."
      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
