#! /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 postbackout, did not find a SUNWmsgco that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# was configure run? (not necessarily accessible)
#
# - sets the variable wasConfigured to either 0 or 1
# - requires basedir
#
get_wasConfigured() {
  if [ -f $basedir/sbin/start-msg ]; then
    wasConfigured=1
  else
    wasConfigured=0
  fi
  log_only "-- wasConfigured = $wasConfigured"
}

#
# Is configuration accessible?
#
# - calls get_wasConfigured, see that subroutine for details
# - sets the variable isConfigAccessible to either 0 or 1
# - requires basedir
#
# checks to see if config/... is accessible
# if -R is passed then consider this system as not having an accessible
#    configuration.
#
get_isConfigAccessible() {
  get_wasConfigured
  if [ $wasConfigured -eq 1 ]; then
    if [ -f $basedir/config/imta_tailor ]; then
      isConfigAccessible=1
    else
      isConfigAccessible=0
    fi
    if [ $ROOTDIR != "/" ]; then
      isConfigAccessible=0
    fi
  else
    isConfigAccessible=0
  fi
  log_only "-- isConfigAccessible = $isConfigAccessible"
}

#
# perform_custom
# - should only be executed if config is accessible
#
perform_custom() {
  log_only "-- perform_custom being run"

  log_msg "--"
  log_msg "-- Removing the compiled configuration..."
  log_msg "-- Use imsimta cnbuild to create a compiled configuration"
  log_msg "--"

  log_only "--   $basedir/sbin/imsimta cnbuild -remove"
  $basedir/sbin/imsimta cnbuild -remove >> $LOGFILE 2>&1
  log_only "--   return status $?" >> $LOGFILE 2>&1

  log_only "--   $basedir/sbin/imsimta version"
  $basedir/sbin/imsimta version >> $LOGFILE 2>&1
  log_only "     return status $?" >> $LOGFILE 2>&1

  log_only "--   $basedir/sbin/imsimta test -rewrite -debug postmaster"
  $basedir/sbin/imsimta test -rewrite -debug postmaster >> $LOGFILE 2>&1
  log_only "--   return status $?" >> $LOGFILE 2>&1
}

#
# main program starts here
#
# determine basedir
# determine ifConfigured
# if configAccessible
#   perform_custom

get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
LOGFILE=$PATCHDIR/postbackout_`date +%Y%m%d%H%M%S`.log
log_init
log_only "-- ROOTDIR = $ROOTDIR, BASEDIR = $basedir"
get_isConfigAccessible
if [ $isConfigAccessible -eq 1 ]; then
  perform_custom
fi
log_msg "--"
log_msg "-- Note that you should consider restoring your config files"
log_msg "-- to their version prior to application of this patch"
log_msg "-- saved versions (if any) are kept under $PATCHDIR/save"
log_msg "--"
