#! /bin/sh
#
# Files generated:
# - $PATCHDIR/install.list - output of devinstall dryrun
# - $PATCHDIR/file.list - list of files that are created/modified by 
#   devinstall

# 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() {
  /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/SUNWmsgco*`
  # 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 SUNWmsgco that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# create the patch staging area
# - sets the variables: PATCHDIR BACKOUTDIR SAVEDIR
#
create_staging_area() {
  log_only "-- creating patch directory $PATCHDIR"
  /usr/bin/mkdir -p $PATCHDIR/backout $PATCHDIR/save
  BACKOUTDIR=$PATCHDIR/backout
  SAVEDIR=$PATCHDIR/save
}

#
# these set of files are always filtered regardless of whether there is
# a configuration. This is from postinstall.install
#
filter_special_files() {
  log_only "-- filter special files"
  FILTERFILES="$basedir/lib/config-templates/DevsetupDefaults.properties $basedir/lib/config-templates/installer.properties $basedir/sbin/configure $basedir/sbin/useconfig"
  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? (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"
}

#
# read saveState file for configuration variables
# - given basedir
# - get iMS.UserId, iMS.GroupId
# - set the shell variables
#   saveState         shell variable
#   ---------         --------------
#   iMS.UserId     -> user
#   iMS.GroupId    -> group
# - set the variable "saveState" for the location of the saveState file
#
read_saveState() {
  # determine saveState file
  # have to use the latest install/configure_YYYYMM... directory
  CONFIGDIRS=`/bin/ls -dt $basedir/install/configure_[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/configure_[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_only "-- WARNING: no configure_YYYYMM... directory found"
    log_only "-- Hard coding mailsrv user and group for devinstall utility"
    saveState="none"
    # user is not relevant as of now, there are not files that I can examine
    # to determine what the mailsrv user is. As long as nothing in
    # config.ins.template requires it, I am okay
    user=bin
    log_only "-- user: $user"
    group=mail
    log_only "-- group: $group"
  else
    for dir in $CONFIGDIRS
    do
      saveState=$dir/saveState
      if [ -f $saveState ]; then
	break
      fi
    done
    log_only "-- saveState file: $saveState"
    set `/usr/bin/grep iMS.UserId $saveState`
    user=$3
    log_only "-- user: $user"
    set `/usr/bin/grep iMS.GroupId $saveState`
    group=$3
    log_only "-- group: $group"
  fi
}

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

#
# perform_custom
#
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_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
#
# Do I need to do stop-msg?
# determine server-root
# create patch staging area (for backout and new files to apply)
# determine ifConfigured
# if configured
#   - read saveState
#     - get schema type, comp list, iMS.UserId, iMS.GroupId
#   - generate config.ins and devtypes.txt
#   - run devinstall
#   - install new files, backup old ones.
#   - do custom stuff
# endif

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
log_only "-- ROOTDIR = $ROOTDIR, BASEDIR = $basedir"
create_staging_area
filter_special_files
get_isConfigAccessible
if [ $wasConfigured -eq 1 ]; then
  read_saveState
  create_configins
  run_devinstall
fi
if [ $isConfigAccessible -eq 1 ]; then
  perform_custom
fi
if [ $wasConfigured -eq 1 ]; then
  log_msg "--"
  log_msg "-- To generate updated config files, run"
  log_msg "-- sbin/patch-config $PATCHDIR"
fi
