#! /bin/sh
#
# The file $PATCHDIR/config.list is generated containing the
# list of files under config_templatelist
# i.e. the list of template files that have changed

# list of config files that were changed
# - list of template files
# - list of their final resting places in the config area
#
# list has:
# - mta config files that I know have changed
# - mail/express/install.ins
# - mail/express/imx/install.ins
# - mail/express/ja/help/images/install.ins
# - mail/express/ja/help/install.ins
# - mail/express/ja/install.ins
# - mail/express/ja/images/install.ins
#
config_templatelist="\
	lib/config-templates/html/ja/help/images/check_all15.gif \
	lib/config-templates/html/ja/help/images/check_all17.gif \
	lib/config-templates/html/ja/help/images/check_all18.gif \
	lib/config-templates/html/ja/help/images/check_all19.gif \
	lib/config-templates/html/ja/help/images/check_all20.gif \
	lib/config-templates/html/ja/help/images/check_all21.gif \
	lib/config-templates/html/ja/help/images/check_all23.gif \
	lib/config-templates/html/ja/help/images/check_all29.gif \
	lib/config-templates/html/ja/help/images/LrlAddAddress_wo_1.gif \
	lib/config-templates/html/ja/help/images/LrlAddAddress_wo_114.gif \
	lib/config-templates/html/ja/help/images/LrlColExtMail_wo_19.gif \
	lib/config-templates/html/ja/help/images/LrlGetMail_wo_18.gif \
	lib/config-templates/html/ja/help/images/LrlNewFldr_wo_111.gif \
	lib/config-templates/html/ja/help/images/LrlNewMsg_wo_17.gif \
	lib/config-templates/html/ja/help/images/LrlRdAnsDelMsg_16.gif \
	lib/config-templates/html/ja/help/images/LrlRdDelMsg_15.gif \
	lib/config-templates/html/ja/help/images/LrlRdMsg_16.gif \
	lib/config-templates/html/ja/help/images/LrlRefresh_wo_113.gif \
	lib/config-templates/html/ja/help/images/LrlSearchMsg_wo_110.gif \
	lib/config-templates/html/ja/help/images/LrlSubscribe_wo_112.gif \
	lib/config-templates/html/ja/help/images/LrlUnrdMsg_13.gif \
	lib/config-templates/html/ja/help/images/toolbaroptions4.gif \
	lib/config-templates/html/ja/help/images/uncheck_all16.gif \
	lib/config-templates/html/ja/help/images/uncheck_all22.gif \
	lib/config-templates/html/ja/help/images/uncheck_all24.gif \
	lib/config-templates/html/ja/help/images/uncheck_all26.gif \
	lib/config-templates/html/ja/help/images/uncheck_all28.gif \
	lib/config-templates/html/ja/help/images/uncheck_alla27.gif \
	lib/config-templates/html/ja/help/images/uncheck_alla29.gif \
	lib/config-templates/html/ja/help/catalog.css \
	lib/config-templates/html/ja/help/collextm.html \
	lib/config-templates/html/ja/help/compmsgs.html \
	lib/config-templates/html/ja/help/getnewmg.html \
	lib/config-templates/html/ja/help/getstart.html \
	lib/config-templates/html/ja/help/ix.html \
	lib/config-templates/html/ja/help/manfoldr.html \
	lib/config-templates/html/ja/help/manmesge.html \
	lib/config-templates/html/ja/help/pvtshared.html \
	lib/config-templates/html/ja/help/sharfldr.html \
	lib/config-templates/html/ja/help/spellchk.html \
	lib/config-templates/html/ja/help/srchadres.html \
	lib/config-templates/html/ja/help/srchmsgs.html \
	lib/config-templates/html/ja/help/srchunam.html \
	lib/config-templates/html/ja/help/subscribe.html \
	lib/config-templates/html/ja/help/SunONE.jpg \
	lib/config-templates/html/ja/help/Help.gif \
	lib/config-templates/html/ja/help/topics.html \
	lib/config-templates/html/ja/help/wkwithmg.html \
	lib/config-templates/html/ja/help/atchfile.html \
	\
	lib/config-templates/html/ja/compRecipient_fs.html \
	lib/config-templates/html/ja/default.html \
	lib/config-templates/html/ja/editPabEntry_fs.html \
	lib/config-templates/html/ja/editPabGroup_fs.html \
	lib/config-templates/html/ja/i18n.js \
	lib/config-templates/html/ja/lookup_fs.html \
	lib/config-templates/html/ja/mail.html \
	lib/config-templates/html/ja/messageView.html \
	lib/config-templates/html/ja/pab_fs.html \
	lib/config-templates/html/ja/searchMessage.html \
	lib/config-templates/html/ja/searchOnly.html \
	lib/config-templates/html/ja/searchusers_fs.html \
	lib/config-templates/html/ja/version.html \
	\
	lib/config-templates/html/ja/images/webmail.gif \
	\
        "
config_configlist="\
	config/html/ja/help/images/check_all15.gif \
	config/html/ja/help/images/check_all17.gif \
	config/html/ja/help/images/check_all18.gif \
	config/html/ja/help/images/check_all19.gif \
	config/html/ja/help/images/check_all20.gif \
	config/html/ja/help/images/check_all21.gif \
	config/html/ja/help/images/check_all23.gif \
	config/html/ja/help/images/check_all29.gif \
	config/html/ja/help/images/LrlAddAddress_wo_1.gif \
	config/html/ja/help/images/LrlAddAddress_wo_114.gif \
	config/html/ja/help/images/LrlColExtMail_wo_19.gif \
	config/html/ja/help/images/LrlGetMail_wo_18.gif \
	config/html/ja/help/images/LrlNewFldr_wo_111.gif \
	config/html/ja/help/images/LrlNewMsg_wo_17.gif \
	config/html/ja/help/images/LrlRdAnsDelMsg_16.gif \
	config/html/ja/help/images/LrlRdDelMsg_15.gif \
	config/html/ja/help/images/LrlRdMsg_16.gif \
	config/html/ja/help/images/LrlRefresh_wo_113.gif \
	config/html/ja/help/images/LrlSearchMsg_wo_110.gif \
	config/html/ja/help/images/LrlSubscribe_wo_112.gif \
	config/html/ja/help/images/LrlUnrdMsg_13.gif \
	config/html/ja/help/images/toolbaroptions4.gif \
	config/html/ja/help/images/uncheck_all16.gif \
	config/html/ja/help/images/uncheck_all22.gif \
	config/html/ja/help/images/uncheck_all24.gif \
	config/html/ja/help/images/uncheck_all26.gif \
	config/html/ja/help/images/uncheck_all28.gif \
	config/html/ja/help/images/uncheck_alla27.gif \
	config/html/ja/help/images/uncheck_alla29.gif \
	config/html/ja/help/catalog.css \
	config/html/ja/help/collextm.html \
	config/html/ja/help/compmsgs.html \
	config/html/ja/help/getnewmg.html \
	config/html/ja/help/getstart.html \
	config/html/ja/help/ix.html \
	config/html/ja/help/manfoldr.html \
	config/html/ja/help/manmesge.html \
	config/html/ja/help/pvtshared.html \
	config/html/ja/help/sharfldr.html \
	config/html/ja/help/spellchk.html \
	config/html/ja/help/srchadres.html \
	config/html/ja/help/srchmsgs.html \
	config/html/ja/help/srchunam.html \
	config/html/ja/help/subscribe.html \
	config/html/ja/help/SunONE.jpg \
	config/html/ja/help/Help.gif \
	config/html/ja/help/topics.html \
	config/html/ja/help/wkwithmg.html \
	config/html/ja/help/atchfile.html \
	\
	config/html/ja/compRecipient_fs.html \
	config/html/ja/default.html \
	config/html/ja/editPabEntry_fs.html \
	config/html/ja/editPabGroup_fs.html \
	config/html/ja/i18n.js \
	config/html/ja/lookup_fs.html \
	config/html/ja/mail.html \
	config/html/ja/messageView.html \
	config/html/ja/pab_fs.html \
	config/html/ja/searchMessage.html \
	config/html/ja/searchOnly.html \
	config/html/ja/searchusers_fs.html \
	config/html/ja/version.html \
	\
	config/html/ja/images/webmail.gif \
	\
	"
#
# 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
  /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
# - input: PATCHDIR
# - sets the variables: BACKOUTDIR SAVEDIR
#
create_staging_area() {
  BACKOUTDIR=$PATCHDIR/backout
  SAVEDIR=$PATCHDIR/save
  log_only "-- creating $BACKOUTDIR and $SAVEDIR"
  mkdir -p $BACKOUTDIR $SAVEDIR
}

#
# 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
#      75) log_only "-- watcher not running";;
#      *)  log_msg "--"
#	  log_msg "-- Please stop the services before applying the patch"
#          log_msg "--";;
#          exit 1;;
#    esac
  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"
}

#
# special prerequisites 
# need to have lib/config-templates/Devsetup.properties file
#
check_prereqs() {
  if [ ! -f $basedir/lib/config-templates/Devsetup.properties ]; then
    # find configure_YYYYYMM... 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_msg "-- The patch requires that the following file exists "
      log_msg "--   $basedir/lib/config-templates/Devsetup.properties"
      log_msg "-- You can find this file under your factory config area"
      log_msg "-- This will typically be in one of the following areas:"
      log_msg "--   $basedir/data/setup/configure_YYYYMMDDHHMMSS/"
      log_msg "--   $basedir/install/configure_YYYYMMDDHHMMSS/"
      log_msg "-- Please copy the file and apply the patch again"
      exit 1
    else
      for dir in $CONFIGDIRS
      do
	saveState=$dir/saveState
	if [ -f $saveState ]; then
	  break
	fi
      done
      log_only "-- saveState file: $saveState"
      # extract configure_ directory from saveState
      configDir=`/usr/bin/dirname $saveState`
      if [ -f $configDir/Devsetup.properties ]; then
        log_only "/usr/bin/cp -p $configDir/Devsetup.properties $basedir/lib/config-templates"
        /usr/bin/cp -p $configDir/Devsetup.properties $basedir/lib/config-templates
      else
	log_msg "-- The patch requires that the following file exists "
	log_msg "--   $basedir/lib/config-templates/Devsetup.properties"
	log_msg "-- You can find this file under your factory config area"
	log_msg "-- This will typically be in one of the following areas:"
	log_msg "--   $basedir/data/setup/configure_YYYYMMDDHHMM/"
	log_msg "--   $basedir/install/configure_YYYYMMDDHHMM/"
	log_msg "-- Please copy the file and apply the patch again"
      fi
    fi
  fi
}

#
# special processing for files that have moved locations
# - return_header.opt 
#   - moved template location: lib/locale/C to lib/config-templates
#   - TBD moved package: from msg_en to imta 
save_movedfiles() {
  if [ ! -f $basedir/lib/config-templates/return_header.opt -a -f $basedir/lib/locale/C/return_header.opt ]; then
    log_only "/usr/bin/cp -p $basedir/lib/locale/C/return_header.opt $basedir/lib/config-templates/return_header.opt"
    /usr/bin/cp -p $basedir/lib/locale/C/return_header.opt $basedir/lib/config-templates/return_header.opt >> $LOGFILE 2>&1
  fi
}

#
# save previous template config files prior to it being patched
# so we can do a 3 way diff later
# also create the config.list file containing the
#   list of config template files and the corresponding config file.
#   The first column is the config template file, the second column is the
#     corresponding config file. 
#
save_config() {
  log_only "-- save_config being run"
  /bin/rm -f $PATCHDIR/config.list >> $LOGFILE 2>&1
  /usr/bin/touch $PATCHDIR/config.list >> $LOGFILE 2>&1
  set $config_configlist
  for file in $config_templatelist
  do
    /usr/bin/echo $file $1 >> $PATCHDIR/config.list
    shift
    dir=`dirname $BACKOUTDIR/$file`
    if [ ! -d $dir ]; then
      log_only "mkdir -p $dir"
      mkdir -p $dir >> $LOGFILE 2>&1
    fi
    if [ -f $BACKOUTDIR/$file ]; then
      log_only "/usr/bin/mv $BACKOUTDIR/$file $BACKOUTDIR/$file.orig"
      /usr/bin/mv $BACKOUTDIR/$file $BACKOUTDIR/$file.orig >> $LOGFILE 2>&1
    fi
    # if this is a new file, then there will not be a file to backup
    if [ -f $basedir/$file ]; then
      log_only "/usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file"
      /usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file >> $LOGFILE 2>&1
    else
      log_only "-- No old $basedir/$file to backup"
    fi
  done
}

#
# save comm_dssetup related files since customers may want to see what
# has changed
#
save_dssetup() {
  log_only "-- save_dssetup being run"
  for file in $dssetup_list
  do
    dir=`dirname $BACKOUTDIR/$file`
    if [ ! -d $dir ]; then
      log_only "mkdir -p $dir"
      mkdir -p $dir >> $LOGFILE 2>&1
    fi
    if [ -f $BACKOUTDIR/$file ]; then
      log_only "/usr/bin/mv $BACKOUTDIR/$file $BACKOUTDIR/$file.orig"
      /usr/bin/mv $BACKOUTDIR/$file $BACKOUTDIR/$file.orig >> $LOGFILE 2>&1
    fi
    # if this is a new file, then there will not be a file to backup
    if [ -f $basedir/$file ]; then
      log_only "/usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file"
      /usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file >> $LOGFILE 2>&1
    else
      log_only "-- No old $basedir/$file to backup"
    fi
  done
}

#
# special hard coded stuff for things I do not handle generically
# - msg.conf has a variable that used to have a hard coded value, but
#   became a substitution variable. This causes a diff3 conflict.
#   So just modify the old template
#
special_hacks () {
  log_only "-- special_hacks"
  BDIR=$BACKOUTDIR/lib/config-templates
  if [ -f $BDIR/msg.conf ]; then
    log_only "/usr/bin/mv $BDIR/msg.conf $BDIR/msg.conf.sed"
    /usr/bin/mv $BDIR/msg.conf $BDIR/msg.conf.sed
    log_only '/usr/bin/sed -e s:local.schedule.expire= "0 23 \* \* \* <msg.RootPath>/sbin/imexpire":local.schedule.expire= <schedExpire>: $BDIR/msg.conf.sed > $BDIR/msg.conf'
    /usr/bin/sed -e 's:local.schedule.expire= "0 23 \* \* \* <msg.RootPath>/sbin/imexpire":local.schedule.expire= <schedExpire>:' $BDIR/msg.conf.sed > $BDIR/msg.conf
  fi
}

#
# 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
#   - save webmail files
# endif

get_basedir
PATCHDIR=$basedir/install/patch/$PatchNum
mkdir -p $PATCHDIR
LOGFILE=$PATCHDIR/prepatch_`date +%Y%m%d%H%M%S`.log
log_init
log_only "-- ROOTDIR = $ROOTDIR, BASEDIR = $basedir"
create_staging_area
get_isConfigAccessible
if [ $wasConfigured -eq 1 ]; then
  check_prereqs
fi
if [ $isConfigAccessible -eq 1 ]; then
  check_servers
fi
save_movedfiles
save_config
save_dssetup
special_hacks
