#! /bin/sh
# bourne shell script
#
# This is the original RTM version that this patch applies to
VERSION="6.3,REV=2004.08.12"

#
# 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
  PKGS=`/bin/ls -d $PKGDB/SUNWcomds*`
  # 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 SUNWcomds 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
}


#
# main program starts here
#
# determine server-root
# 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
