#!/bin/sh
#set -x
#
# patch the configuration files
# takes one argument - the PATCHDIR, typically underinstall/patch/<patchnum>
#
basedir=<msg.RootPath>
#
# usage
#
usage()
{
  echo
  echo  "Usage: $0 <patchdir>\n"
  echo
  echo  "  <patchdir>: The patch directory area created during patchaadd."
  echo  "    e.g. $basedir/install/patch/116568-05"
  echo
  exit 1
}

#
# check to make sure config is accessible
#
check_configaccess() {
  if [ ! -w $basedir/config/imta_tailor ]; then
    log_msg "-- Could not access the config area $basedir/config"
    log_msg "-- This utility requires access to the config area"
    exit 2
  fi
}

#
# check to make sure servers are stopped
#
check_servers() {
  log_only "-- check_servers"
  if [ -x $basedir/lib/msstart ]; then
    $basedir/lib/msstart -l >> $LOGFILE 2>&1
    stat=$?
    log_only "-- msstart -l returned $stat"
    case $stat in
      0) log_only "-- No servers running";;
      *)  log_msg "--"
	  log_msg "-- Please stop the services before applying the patch"
          log_msg "--"
          exit 1;;
    esac
  fi
}

#
# determine if configured for HA
# - set the variable isHAconfigured to 0 (false) or 1 (true)
#
isHAconfigured() {
  VCSCMD=/opt/VRTSvcs/bin/hares 
  SC3CMD=/usr/cluster/lib/rgm/rtreg/SUNW.ims
  IP_INTERFACE=`$basedir/lib/configutil -o service.listenaddr 2> $LOGFILE`
  if [ $# -eq 0 -a \
       ! \( -z "$IP_INTERFACE" -o "$IP_INTERFACE" = "INADDR_ANY" \) -a \
       \( -f "$VCSCMD" -o -f "$SC2CMD" -o -f "$SC3CMD" \) ]; then
    isHAconfigured=1
  else
    isHAconfigured=0
  fi
  log_only "-- isHAconfigured = $isHAconfigured"
}

#
# check_newfile
# - argument passed in $cfile - the config file relative to $basedir
#   $SAVEDIR/$cfile.new will be the "new config file"
# - check if file exists
# - check to see if it has any diff3 conflicts
# If a test fails, increment the variable newfile_error
check_newfile() {
  file=$SAVEDIR/$cfile.new
  if [ ! -f $file ]; then
    log_msg "-- ERROR: $file does not exist"
    newfile_error=`expr $newfile_error + 1`
  else
    /usr/bin/grep -s '^=======$' $file
    stat=$?
    if [ $stat -eq 0 ]; then
      log_msg "-- ERROR: $file has diff3 conflicts in it"
      newfile_error=`expr $newfile_error + 1`
    fi
  fi
}

#
# install newconfig files
# - attempt to patch the user files, but do not actually overwrite
# - These are config files that do not have substitutions in them.
#   so they are "easier" to patch
install_newconfig() {
  newfile_error=0
  set `/usr/bin/cat $PATCHDIR/newconfig.list`
  while [ $# -ne 0 ]
  do
    # the "config" file, a path relative to $basedir
    cfile=$1
    shift
    # check if new file exists and has no conflicts
    # increments newfile_error if any error found
    check_newfile
  done
  if [ $newfile_error -ne 0 ]; then
    log_msg "--"
    log_msg "-- $newfile_error errors found, please correct and rerun the"
    log_msg "-- utility. No change was made to the system."
    exit $newfile_error
  fi

  # install the file
  NUMCOPIES=0
  set `/usr/bin/cat $PATCHDIR/newconfig.list`
  while [ $# -ne 0 ]
  do
    cfile=$1
    shift
    # check if it is identical first
    /usr/bin/cmp -s $SAVEDIR/$cfile.new $basedir/$cfile
    stat=$?
    if [ $stat -ne 0 ]; then
      # save existing config file
      if [ -f $basedir/$cfile ]; then
        dir=`dirname $CONFIGBACKOUTDIR/$cfile`
	if [ ! -d $dir ]; then
	  /usr/bin/mkdir -p $dir
        fi
	/usr/bin/cp -p $basedir/$cfile $CONFIGBACKOUTDIR/$cfile
      fi
      # install new config file
      #   no permissions being enforced by mkdir. Nuts.
      dir=`dirname $basedir/$cfile`
      if [ ! -d $dir ]; then
	log_msg "-- /usr/bin/mkdir -p $dir"
        /usr/bin/mkdir -p $dir
      fi
      if [ $fflag -ne 1 ]; then
        log_msg "/usr/bin/cp -pi $SAVEDIR/$cfile.new $basedir/$cfile"
        /usr/bin/cp -pi $SAVEDIR/$cfile.new $basedir/$cfile
      else
        log_msg "/usr/bin/cp -pf $SAVEDIR/$cfile.new $basedir/$cfile"
        /usr/bin/cp -pf $SAVEDIR/$cfile.new $basedir/$cfile
      fi
      NUMCOPIES=`expr $NUMCOPIES + 1`
    fi
  done
  log_msg "-- Number of different files: $NUMCOPIES"
  log_msg "-- Original files are backed up under $CONFIGBACKOUTDIR"
}

#
# perform_checks
#
perform_checks() {
  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 "-- Also Use imsimta chbuild for updated characters sets"

  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
#

# utility routines
. ${basedir}/lib/util.sh

# get args
# whether to install the new config files or not
fflag=0;
while getopts hf c
do
    case $c in
    f)
	fflag=1;;
    h)
        usage;;
    \?)
        usage;;
    esac
done
shift `expr $OPTIND - 1 `

if [ $# -ne 1 ]; then
  usage
fi
PATCHDIR=$*
#
# must be root
#
set `/usr/bin/id`
if [ $1 != "uid=0(root)" ]; then
  echo
  echo "Utility to install the new config files generated by patch-config\n"
  echo "To use this utility you need to be the system's root user. \n"
  echo 
  exit 1
fi
# initialize logging
LOGFILE=$PATCHDIR/install-newconfig_`date +%Y%m%d%H%M%S`.log
log_init

# save directory created by patchadd and patch-config
if [ ! -d $PATCHDIR/save ]; then
  echo "Invalid PATCHDIR, $PATCHDIR/save is not a directory"
  usage
fi
SAVEDIR=$PATCHDIR/save
# backout directory created by patchadd
if [ ! -d $PATCHDIR/backout ]; then
  echo "Invalid PATCHDIR, $PATCHDIR/backout is not a directory"
  usage
fi
BACKOUTDIR=$PATCHDIR/backout
# file containing list of new config files
if [ ! -f $PATCHDIR/newconfig.list ]; then
  echo "$PATCHDIR/newconfig.list not found, Please run patch-config first"
  usage
fi
# where original config files will be backed up
CONFIGBACKOUTDIR=$PATCHDIR/config_`date +%Y%m%d%H%M%S`
/usr/bin/mkdir -p $CONFIGBACKOUTDIR

# check to make sure config is accessible
check_configaccess
isHAconfigured
if [ $isHAconfigured -eq 1 ]; then
  log_msg "Warning an HA configuration is detected on your systems,"
  ans=`ckyorn -Q -d n -p "Continue (default: n)"` || exit $?
  log_only "Continue (default: n) [y,n,?] $ans"
  if [ "$ans" = n -o "$ans" = no ]
  then
    log_msg "-- Okay, terminating. Rerun at your convenience"
    exit 3
  fi
fi
check_servers
install_newconfig
perform_checks
log_msg "-- The final step is to apply the ldif files"
log_msg "-- $basedir/lib/patch/cfgdir_diff.ldif and"
log_msg "-- $basedir/lib/patch/ugdir_diff.ldif"
log_msg "-- to the config Directory and the user/group Directory respectively"
log_msg "-- This is not performed automatically, you must do this manually"
