#! /bin/sh
#
# This is the original RTM version that this patch applies to
VERSION="1.0,REV=2003.10.29"

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

#
# initialize LOGFILE
#
log_init() {
  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
  PKGS=`/bin/ls -d $PKGDB/SUNWcomic*`
  # 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
  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 SUNWcomic that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# 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"
}

#
# special prerequisites 
# need to have lib/config-templates/IscliCfg.properties file
#
check_prereqs() {
  log_only "-- check_prereqs"
  if [ ! -f $basedir/lib/config-templates/IscliCfg.properties ]; then
    # find Isclid-config_YYYYYMM... directory
    CONFIGDIRS=`/bin/ls -dt $basedir/install/Iscli-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] $basedir/data/setup/Iscli-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] 2> $LOGFILE`
    if [ -z "$CONFIGDIRS" ]; then
      log_msg "-- The patch requires that the following file exists "
      log_msg "--   $basedir/lib/config-templates/IscliCfg.properties"
      log_msg "-- You can find this file under your factory config area"
      log_msg "-- This will typically be in one of the following areas:"
      log_msg "--   $basedir/data/setup/Iscli-config_YYYYMMDDHHMMSS/"
      log_msg "--   $basedir/install/Iscli-config_YYYYMMDDHHMMSS/"
      log_msg "-- Please copy the file and apply the patch again"
      exit 1
    else
      for dir in $CONFIGDIRS
      do
	saveState=$dir/saveState
	if [ -f $saveState ]; then
	  break
	fi
      done
      log_only "-- saveState file: $saveState"
      # extract Iscli-config_ directory from saveState
      configDir=`/usr/bin/dirname $saveState`
      if [ -f $configDir/IscliCfg.properties ]; then
        log_only "/usr/bin/cp -p $configDir/IscliCfg.properties $basedir/lib/config-templates"
        /usr/bin/cp -p $configDir/IscliCfg.properties $basedir/lib/config-templates
      else
	log_msg "-- The patch requires that the following file exists "
	log_msg "--   $basedir/lib/config-templates/IscliCfg.properties"
	log_msg "-- You can find this file under your factory config area"
	log_msg "-- This will typically be in one of the following areas:"
	log_msg "--   $basedir/data/setup/Iscli-config_YYYYMMDDHHMMSS/"
	log_msg "--   $basedir/install/Iscli-config_YYYYMMDDHHMMSS/"
	log_msg "-- Please copy the file and apply the patch again"
      fi
    fi
  fi
}

#
# main program starts here
#
get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
mkdir -p $PATCHDIR
LOGFILE=$PATCHDIR/prepatch_`date +%Y%m%d%H%M%S`.log
log_init
log_only "-- ROOTDIR = $ROOTDIR, BASEDIR = $basedir"
get_isConfigured
if [ $isConfigured -eq 1 ]; then
  check_prereqs
fi
