#! /bin/sh
# bourne shell script
#
# This is the original RTM version that this patch applies to
if [ "`/bin/uname`" = "HP-UX" ]; then
VERSION="6.1"
else
VERSION="6.1,REV=2004.04.29"
fi

#
# debug message
#
debug_log() {
  if [ $DEBUG != 0 ]; then
    log_msg $*
  fi
}

#
# initialize LOGFILE
#
log_init() {
  /usr/bin/touch $LOGFILE
  /usr/bin/echo "============ LOGFILE started `date` ==========" >> $LOGFILE
}

#
# log a message to the screen and to the LOGFILE
#
log_msg() {
  /usr/bin/echo $*
  /usr/bin/echo $* >> $LOGFILE
}

#
# log a message to the LOGFILE only
#
log_only() {
  /usr/bin/echo $* >> $LOGFILE
}

#
# determine basedir
# - sets the variable basedir
#
get_basedir() {   # start of get_basedir
if [ "`/bin/uname`" = "HP-UX" ]; then
  PKGS=`swlist -a name | grep "sun-uwc"`
  # did we find the right PKG
  found=0
  for dir in $PKGS
  do
    pkgname=`basename $dir`
    basedir=`swlist -a location $pkgname | awk 'END {print $NF}'`
    basedir=$basedir/uwc
    	
    version=`swlist -a revision | grep $pkgname | awk 'END {print $NF}'`
    if [ $version = "$VERSION" ]; then
    	found=1
    	break
    fi

  done
else
  PKGS=`/bin/ls -d $PKGDB/SUNWuwc*`
  # did we find the right PKG
  found=0
  for dir in $PKGS
  do
    pkgname=`basename $dir`
    basedir=`pkgparam -R $ROOTDIR $pkgname BASEDIR`
    version=`pkgparam -R $ROOTDIR $pkgname VERSION`
    if [ $version = "$VERSION" ]; then
      found=1
      break
    fi
  done
 fi
  if [ $ROOTDIR != "/" ]; then
    basedir=${ROOTDIR}$basedir
  fi
  # what if we do not find the pkg? should not possible...
  if [ $found -eq 0 ]; then
    /usr/bin/echo "Error in postpatch, did not find a SUNWuwc that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# create the patch staging area
#
create_staging_area() {
  log_only "-- creating patch directory $PATCHDIR"
  /usr/bin/mkdir -p $PATCHDIR
}


#
# these set of files are always filtered regardless of whether there is
# a configuration. This is from postinstall.installcomic
#
filter_special_files() {
  log_only "-- filter special files"
  FILTERFILES="$basedir/lib/config-templates/UwcCfgDefaults.properties $basedir/lib/config-templates/installer.properties $basedir/sbin/config-iscli"
  for f in $FILTERFILES
  do
  # use cp to preserve permissions
    /usr/bin/cp -p $f $f.orig
    /usr/bin/sed -e "s:<msg.RootPath>:$basedir:" -e "s/<INSTALLER_TYPE>/PKGADD/" $f.orig > $f
    /usr/bin/rm $f.orig
  done
}

#
# was configure run previously?
# - sets the variable isConfigured to either 0 or 1
# - requires basedir
get_isConfigured() {
  if [ -r $basedir/config ]; then
    isConfigured=1
  else
    isConfigured=0
  fi
  log_only "-- isConfigured = $isConfigured"
}

#
# read saveState file for configuration variables
# What to do if we do not find the file?
# - given basedir
# - get iMS.UserId, iMS.GroupId, INSTALLER_TYPE, SCHEMA_TYPE, comp list
# - set the variables
#   saveState         shell variable
#   ---------         --------------
#   com.iplanet.install.panels.common.ComponentPanel.selectedcomponents
#                      -> compList
read_saveState() {
  # determine saveState file
  # have to use the latest install/Uwc-config_YYYYMM... directory
  CONFIGDIRS=`/bin/ls -dt $basedir/install/Uwc-config_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]`
  for dir in $CONFIGDIRS
  do
  if [ "`/bin/uname`" = "HP-UX" ]; then 
  	saveState=$dir/../saveState[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]
  else
  	saveState=$dir/saveState
  fi
    
    if [ -f $saveState ]; then
	if [ "`/bin/uname`" = "HP-UX" ]; then 
	   saveState=`ls -rt $dir/../saveState* | tail -1`
	fi
      break
    fi
  done
  log_only "-- saveState file: $saveState"
  set `/usr/bin/grep com.iplanet.install.panels.common.ComponentPanel.selectedcomponents $saveState`
  compList=$3
  log_only "-- compList: $compList"
}



#
# read cfgtypes.txt 
# What to do if we do not find the file?
# - given basedir
# - get msg.RootPath, local.serveruid and local.servergid
# - set the variables
#
#   cfgtypes.txt  Sub variable         shell variable
#   ------------  ------------         --------------
#   defdestdir    msg.RootPath         rootPath
#   runuser       local.serveruid      user
#   rungroup      local.servergid      group
#
read_cfgtypes() {
  CFGTYPES=$basedir/lib/config-templates/cfgtypes.txt
  log_only "-- read_cfgtypes"
  if [ -z "$CFGTYPES" ]; then
    log_only "-- WARNING: no cfgtypes.txt file found"
    log_only "-- Hard coding user and group for devinstall utility"
    rootPath=$basedir
    user=root
    log_only "-- user: $user"
    group=bin
    log_only "-- group: $group"
  else
    set `/usr/bin/grep \^defdestdir $CFGTYPES`
    rootPath=$2
    set `/usr/bin/grep \^runuser $CFGTYPES`
    user=$2
    log_only "-- user: $user"
    set `/usr/bin/grep \^rungroup $CFGTYPES`
    group=$2
    log_only "-- group: $group"
  fi
}

#
# create config.ins and cfgtypes.txt
# substitute for msg.RootPath, local.serveruid, local.servergid
# - variables used $basedir, $user, $group
#
create_configins() {
  log_only "-- creating config.ins and cfgtypes.txt"
  log_only "/usr/bin/mkdir -p $BACKOUTDIR/lib/config-templates"
  /usr/bin/mkdir -p $BACKOUTDIR/lib/config-templates
  # cfgtypes.txt.template
  if [ ! -f $BACKOUTDIR/lib/config-templates/cfgtypes.txt ]; then
    log_only "/usr/bin/cp -p $basedir/lib/config-templates/cfgtypes.txt $BACKOUTDIR/lib/config-templates/cfgtypes.txt"
    /usr/bin/cp -p $basedir/lib/config-templates/cfgtypes.txt $BACKOUTDIR/lib/config-templates/cfgtypes.txt
  fi
  log_only "/usr/bin/sed -e \"s/<local.serveruid>/$user/\" -e \"s/<local.servergid>/$group/\" -e \"s:<msg\.RootPath>:$rootPath:\" $basedir/lib/config-templates/cfgtypes.txt.template > $basedir/lib/config-templates/cfgtypes.txt"
  /usr/bin/sed -e "s/<local.serveruid>/$user/" \
      -e "s/<local.servergid>/$group/" \
      -e "s:<msg\.RootPath>:$rootPath:" \
    $basedir/lib/config-templates/cfgtypes.txt.template \
  > $basedir/lib/config-templates/cfgtypes.txt 2>> $LOGFILE

  # config.ins.template
  if [ ! -f $BACKOUTDIR/lib/config-templates/config.ins ]; then
    log_only "/usr/bin/cp -p $basedir/lib/config-templates/config.ins $BACKOUTDIR/lib/config-templates/config.ins"
    /usr/bin/cp -p $basedir/lib/config-templates/config.ins $BACKOUTDIR/lib/config-templates/config.ins
  fi
  log_only "/usr/bin/sed -e \"s/<local.serveruid>/${user}/\" -e \"s/<local.servergid>/${group}/\" -e \"s:<msg\.RootPath>:$rootPath:\" $basedir/lib/config-templates/config.ins.template > $basedir/lib/config-templates/config.ins"
  /usr/bin/sed -e "s/<local.serveruid>/${user}/" \
      -e "s/<local.servergid>/${group}/" \
      -e "s:<msg\.RootPath>:$rootPath:" \
    $basedir/lib/config-templates/config.ins.template \
    > $basedir/lib/config-templates/config.ins 2>> $LOGFILE
}

#
# run_devinstall
# - uses several variable for input
#
run_devinstall() {
  log_only "----- Running devinstall"
  pkglist="pkgcfg:config"
  #
  log_only "$basedir/lib/devinstall -n -m -P -v -l $pkglist -i $basedir/lib/config-templates/config.ins $basedir/lib/config-templates $basedir/lib/jars $basedir/lib > $PATCHDIR/install.list"
  $basedir/lib/devinstall \
    -n -m -P -v -l $pkglist -i $basedir/lib/config-templates/config.ins \
    $basedir/lib/config-templates $basedir/lib/jars $basedir/lib \
    > $PATCHDIR/install.list 2>> $LOGFILE
  /usr/bin/egrep '^=F' $PATCHDIR/install.list | /usr/bin/nawk '{print $5}' | \
    /usr/bin/sed -e "s:^$basedir/::" > $PATCHDIR/file.list 2>> $LOGFILE
  for f in `/usr/bin/cat $PATCHDIR/file.list`
  do
    dir=`/usr/bin/dirname $BACKOUTDIR/$f`
    if [ ! -d $dir ]; then
      log_only "/usr/bin/mkdir -p $dir"
      /usr/bin/mkdir -p $dir >> $LOGFILE 2>&1
    fi
    if [ ! -f $BACKOUTDIR/$f ]; then
      log_only "/usr/bin/cp -p $basedir/$f $BACKOUTDIR/$f"
      /usr/bin/cp -p $basedir/$f $BACKOUTDIR/$f >> $LOGFILE 2>&1
    fi
  done
  # Now do it for real
  log_only "$basedir/lib/devinstall -m -P -v -l $pkglist -i $basedir/lib/config-templates/config.ins $basedir/lib/config-templates $basedir/lib/jars $basedir/lib"
  $basedir/lib/devinstall \
    -m -P -v -l $pkglist -i $basedir/lib/config-templates/config.ins \
    $basedir/lib/config-templates $basedir/lib/jars $basedir/lib >> $LOGFILE 2>&1
  log_only "--   return status $?" >> $LOGFILE 2>&1
}

#
# main program starts here
#
# create patch staging area (for backout and new files to apply)

get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
/usr/bin/mkdir -p $PATCHDIR
LOGFILE=$PATCHDIR/postpatch_`date +%Y%m%d%H%M%S`.log
log_init
create_staging_area
filter_special_files
get_isConfigured
if [ $isConfigured -eq 1 ]; then
  read_saveState
  read_cfgtypes
  create_configins
  run_devinstall
fi
