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

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

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

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

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

#
# determine basedir
# - sets the variable basedir
#
get_basedir() {   # start of get_basedir
  PKGS=`pkginfo -R $ROOTDIR | grep SUNWmsgco | awk '{print $2}`
  # 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
    echo "Error in prebackout, did not find a SUNWmsgco that matches expected VERSION: $VERSION"
    exit 1
  fi
}
#
# do_backout
#
do_backout() {
  log_only "-- do_backout being run"
  if [ -f $PATCHDIR/file.list ]; then
    for f in `cat $PATCHDIR/file.list`
    do
      if [ -f $BACKOUTDIR/$f ]; then
	log_only "mv $BACKOUTDIR/$f  $basedir/$f"
	mv $BACKOUTDIR/$f $basedir/$f >> $LOGFILE 2>&1
      fi
    done
  fi
}

#
# main program starts here
#
# Do I need to do stop-msg?
# determine server-root
# do_backout
#
get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
BACKOUTDIR=$PATCHDIR/backout
LOGFILE=$PATCHDIR/prebackout_`date +%Y%m%d%H%M%S`.log
log_init
log_only "-- ROOTDIR = $ROOTDIR, BASEDIR = $basedir"
do_backout
