#!/bin/sh

# 
# This script is executed after the patch has been installed
#
#
#################################################################################
# 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

Ls="/bin/ls"
Rm="/bin/rm"
Ln="/bin/ln"
Chown="/bin/chown"
Cp="/bin/cp"
Awk="/bin/awk"
Nawk="/bin/nawk"
Sed="/bin/sed"
Cut="/bin/cut"
Echo="/bin/echo"
Grep="/bin/grep"
Cat="/bin/cat"
Tail="/bin/tail"
PKGPARAM="/bin/pkgparam"
PKGCHK="/usr/sbin/pkgchk"
Removef="/usr/sbin/removef"
Installf="/usr/sbin/installf"

#Name of the Calendar package
PKG="SUNWics5"

#Packages patched by this patch
PACKAGES="SUNWica5 SUNWics5"

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

#Calendar Root directory
if [ "$ROOTDIR" = "/" -o "X$ROOTDIR" = "X" ]; then
  PKG_INSTALL_ROOT=""
else
  PKG_INSTALL_ROOT="$ROOTDIR"
fi

# Remove the following entries from the pkg database
# But first check whether it is in the pkg database.

$PKGCHK -l -p /etc/opt/SUNWics5/config/en > /tmp/$PKG.tmp
# If pkg name is found then the following entries are in the pkg database.
$Grep $PKG /tmp/$PKG.tmp > /dev/null
if [ $? -eq 0 ]; then
  $Echo "Remove the following entries from the pkg database..."
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/certmap.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/counter.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventcancel.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventnotification.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventpublish.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventreminder.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventreply.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_eventrequest.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_todocancel.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_todopublish.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_todoreminder.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_todoreply.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en/mail_todorequest.fmt
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/ics.conf.template
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/ics50-schema.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/ns-wcal-schema.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/20subscriber.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/50ns-delegated-admin.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/50ns-mail.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/50ns-mlm.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/50ns-msg.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/50ns-value.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/55ims-ical.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/56ims-schema.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema/60iplanet-calendar.ldif
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/sslpassword.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/um50-common-schema.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/version.conf
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/en
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config/schema
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5/config
  $Removef -R ${ROOTDIR} $PKG /etc/opt/SUNWics5
  $Removef -R ${ROOTDIR} $PKG /etc/opt
  $Removef -R ${ROOTDIR} $PKG $BASEDIR/$PKG/cal/config

  #################################################################################
  # tell installation database we are done fooling with it
  #################################################################################

  $Removef -R ${ROOTDIR} -f $PKG  || exit $e_warning
fi

$Rm /tmp/$PKG.tmp > /dev/null

#Relative root directory path
DEPTH=`$Echo $ROOTDIR$BASEDIR/$PKG/cal/lib | $Sed "s/\/\//\//g" | $Sed "s/ +/_/g" | $Sed "s/\// /g" | $Sed "s/^ *//" | $Sed "s/ *$//"`

for i in $DEPTH
do
  if [ -z "$CS_ROOT_FROM_LIB" ]
  then
    CS_ROOT_FROM_LIB=".."
  else
    CS_ROOT_FROM_LIB=`$Echo $CS_ROOT_FROM_LIB/..`
  fi
done

LINKS=`$Cat */pkgmap | $Grep "=\\$PKG_INSTALL_ROOT" | $Cut -d" " -f4 | $Sed "s:\\$PKG_INSTALL_ROOT:$CS_ROOT_FROM_LIB:g"`
for link_line in $LINKS
do
  SRC=`$Echo $link_line | $Cut -d"=" -f1`
  DST=`$Echo $link_line | $Cut -d"=" -f2`
  $Rm $PKG_INSTALL_ROOT$BASEDIR/$SRC
  $Ln -sf $DST $PKG_INSTALL_ROOT$BASEDIR/$SRC
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="$PKG_INSTALL_ROOT$linktarget/ics.conf"
fi

if [ ! -f $CS_CONFIG_FILE ]; then
  $Echo "Could not find ics.conf file: the product has not been configured before."
  $Echo "Please run the configurator: $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/sbin/csconfigurator.sh"
  exit $e_ok
fi

#Runtime user
CS_RUNTIME_USER=`$Grep "^ *local.serviceuser" $CS_CONFIG_FILE | $Tail -1 | $Cut -d"=" -f2 | $Sed "s/\"//g" | $Sed "s/^ *//" | $Sed "s/ *$//"`
CS_RUNTIME_USER_ID=`$Grep $CS_RUNTIME_USER $PKG_INSTALL_ROOT/etc/passwd | $Cut -d: -f3`

#Runtime group
CS_RUNTIME_GROUP=`$Grep "^ *local.servicegroup" $CS_CONFIG_FILE | $Tail -1 | $Cut -d"=" -f2 | $Sed "s/\"//g" | $Sed "s/^ *//" | $Sed "s/ *$//"`
CS_RUNTIME_GROUP_ID=`$Grep $CS_RUNTIME_GROUP $PKG_INSTALL_ROOT/etc/group | $Cut -d: -f3`

if [ "X$CS_RUNTIME_USER" = "X" -o "X$CS_RUNTIME_GROUP" = "X" ]; then
  $Echo "Could not find the runtime user and/or group ids in ics.conf file."
  $Echo "Please run the configurator: $PKG_INSTALL_ROOT$BASEDIR/$PKG/cal/sbin/csconfigurator.sh"
  exit $e_ok
fi

$Echo "Setting file ownerships and permissions..."

# Makes sure the default var directory, if existing, belongs to runtime user/group
if [ -d $PKG_INSTALL_ROOT/var$BASEDIR/$PKG ]; then
  $Chown $CS_RUNTIME_USER_ID:$CS_RUNTIME_GROUP_ID $PKG_INSTALL_ROOT/var$BASEDIR/$PKG
fi

for pkg in $PACKAGES
do 
  #Obtain file list, but only the files that have ownership user and group at nobody
  FILE_LIST=`$Awk '{if ( ($6 == "nobody") || ($7 == "nobody") )  print $4}' $pkg/pkgmap`

  for f in $FILE_LIST
  do
    filetype=`$Grep " $f " $pkg/pkgmap | $Cut -d" " -f2`
    mode=`$Grep " $f " $pkg/pkgmap | $Cut -d" " -f5`
    user=`$Grep " $f " $pkg/pkgmap | $Cut -d" " -f6 | sed "s/^nobody/$CS_RUNTIME_USER/g"`
    group=`$Grep " $f " $pkg/pkgmap | $Cut -d" " -f7 | sed "s/^nobody/$CS_RUNTIME_GROUP/g"`
    
    if [ "`$Echo $f | $Cut -d/ -f1`" = "" ]
    then
      #if the file root is /
      INSTALLF_CMD="$Installf -R $ROOTDIR $pkg $f $filetype $mode $user $group"
    else
      INSTALLF_CMD="$Installf -R $ROOTDIR $pkg $PKG_INSTALL_ROOT$BASEDIR/$f $filetype $mode $user $group"
    fi
    # $Echo "Setting $PKG_INSTALL_ROOT$BASEDIR/$f to $mode for $user:$group"
    $INSTALLF_CMD
  done
  
  # Tell installation database we are done fooling with it
  $Installf -R $ROOTDIR -f $pkg || $e_warning
done

#Adding the NEW format configuration files, if any
file_list=`$Grep "$PKG/cal/config-template/.*\.fmt" */pkgmap | awk '{ if ($2 == "f") print $4}'`
if [ "X$file_list" != "X" ]; then
  for src in $file_list
  do
    dst=`$Echo "$src" | $Sed "s#/config-template/#/config/#g"`
    src=$PKG_INSTALL_ROOT$BASEDIR/$src
    dst=$PKG_INSTALL_ROOT$BASEDIR/$dst
    if [ ! -f $dst ]; then
      $Echo "Adding $dst"
      $Cp $src $dst
    fi
  done
fi

exit $e_ok
