#!/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"
AWK="/bin/awk"
NAWK="/bin/nawk"
SED="/bin/sed"
CUT="/bin/cut"
ECHO="/bin/echo"
GREP="/bin/grep"
CHOWN="/bin/chown"
TAIL="/bin/tail"
PKGPARAM="/bin/pkgparam"
PKGCHK="/usr/sbin/pkgchk"
REMOVEF="/usr/sbin/removef"

#Name of the Calendar package
PKG="SUNWics5"

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

#Calendar Root directory
if [ "$ROOTDIR" = "/" -o "$ROOTDIR" = "" ]; 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 2
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 0
fi


#Runtime user
CS_RUNTIME_USER=`$GREP "^ *local.serviceuser" $CS_CONFIG_FILE | $TAIL -1 | $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`

#Runtime group
CS_RUNTIME_GROUP=`$GREP "^ *local.servicegroup" $CS_CONFIG_FILE | $TAIL -1 | $CUT -d"=" -f2 | $SED "s/\"//g" | $SED "s/^ *//" | $SED "s/ *$//"`

if [ "$CS_RUNTIME_USER" = "" -o "$CS_RUNTIME_GROUP" = "" ]; 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 0
fi


#Obtain file list, but only the files that have ownership user and group at ?
FILE_LIST=`$GREP "^1 f" */pkgmap | $AWK '{if ( ( ($6 == "nobody") && ($7 == "nobody") ) || ($6 == "?") || ($7 == "?") || ( ($6 == "$RA_CSSVCUSER") || ($7 == "$RA_CSSVCGROUP") ) )  print $4}'`

for f in $FILE_LIST
do
  if [ "`$ECHO $f | $CUT -d/ -f1`" = "" ]
  then
    #if the file root is /
    CHOWN_CMD="$CHOWN $CS_RUNTIME_USER:$CS_RUNTIME_GROUP $f"
  else
    CHOWN_CMD="$CHOWN $CS_RUNTIME_USER:$CS_RUNTIME_GROUP $PKG_INSTALL_ROOT$BASEDIR/$f"
  fi
  $ECHO $CHOWN_CMD
  $CHOWN_CMD
done


