#!/bin/sh

# 
# This script is executed after the patch has been installed
#
#

#Name of the Calendar package
CS_PKG="SUNWics5"

#Calendar Base directory
CS_BASEDIR=`pkgparam -R $ROOTDIR $CS_PKG BASEDIR`

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

#Calendar Config directory
CS_CONFIG_DIR="$CS_ROOTDIR$CS_BASEDIR/$CS_PKG/cal/config"

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

if [ ! -f $CS_CONFIG_FILE ]; then
  echo "Could not find ics.conf file: the product has not been configured before, exiting!"
  echo "Please run the configurator: $CS_ROOTDIR$CS_BASEDIR/$CS_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: $CS_ROOTDIR$CS_BASEDIR/$CS_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="/usr/bin/chown $CS_RUNTIME_USER:$CS_RUNTIME_GROUP $f"
  else
    CHOWN_CMD="/usr/bin/chown $CS_RUNTIME_USER:$CS_RUNTIME_GROUP $CS_ROOTDIR$CS_BASEDIR/$f"
  fi
  echo $CHOWN_CMD
  $CHOWN_CMD
done
