#!/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"
PKGINFO="/bin/pkginfo"
PKGCHK="/usr/sbin/pkgchk"
REMOVEF="/usr/sbin/removef"

#Name of the Calendar package
PKG="SUNWics5"
PKG_DE="SUNWdeics"
PKG_FR="SUNWfrics"
PKG_ES="SUNWesics"
PKG_JA="SUNWjaics"
PKG_KO="SUNWkoics"
PKG_ZH="SUNWzhics"
PKG_TW="SUNWtwics"

#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.

PKGLIST="SUNWdeics SUNWesics SUNWfrics SUNWjaics SUNWkoics SUNWzhics SUNWtwics"

for p in $PKG_LIST
do
  $PKGINFO -q $p
  if [ $? -eq 0 ]; then

    if [ $p = "SUNWdeics" ] ; then
      LANGSTR="de"
    fi
    if [ $p = "SUNWesics" ] ; then
      LANGSTR="es"
    fi
    if [ $p = "SUNWfrics" ] ; then
      LANGSTR="fr"
    fi
    if [ $p = "SUNWjaics" ] ; then
      LANGSTR="ja"
    fi
    if [ $p = "SUNWkoics" ] ; then
      LANGSTR="ko"
    fi
    if [ $p = "SUNWzhics" ] ; then
      LANGSTR="zh"
    fi
    if [ $p = "SUNWtwics" ] ; then
      LANGSTR="zh_TW"
    fi

    $PKGCHK -l -p /etc/opt/SUNWics5/config/$LANGSTR > /tmp/$p.tmp
    # If pkg name is found then the following entries are in the pkg database.
    $GREP $p /tmp/$p.tmp > /dev/null
    if [ $? -eq 0 ]; then
      $ECHO "Remove the following entries from the pkg database for $p..."
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventcancel.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventnotification.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventpublish.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventreminder.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventreply.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_eventrequest.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_todocancel.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_todopublish.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_todoreminder.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_todoreply.fmt
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR/mail_todorequest.fmt
      if [ $p = "SUNWzhics" ] ; then
        $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/zh-CN
      fi
      if [ $p = "SUNWtwics" ] ; then
        $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/zh-TW
      fi
      $REMOVEF -R ${ROOTDIR} $p /etc/opt/SUNWics5/config/$LANGSTR

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

      $REMOVEF -R ${ROOTDIR} -f $p  || exit 2
    fi
    $RM /tmp/$p.tmp > /dev/null
  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="$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


for pkg in $PKGLIST; do
  $PKGINFO -q $pkg
  if [ $? -eq 0 ]; then
    #Obtain file list, but only the files that have ownership user and group at ?
    FILE_LIST=`$GREP '^1 f' $pkg/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 /
        file2chown="$f"
      else
        file2chown="$PKG_INSTALL_ROOT$BASEDIR/$f"
      fi
      
      chown_cmd="$CHOWN $CS_RUNTIME_USER:$CS_RUNTIME_GROUP $file2chown"
      $ECHO $chown_cmd
      $chown_cmd
    done
  fi
done

exit $e_ok
