#! /bin/sh
# bourne shell script

# list of config files that were changed
# - list of template files
# - list of their final resting places in the config area
config_templatelist="\
	lib/config-templates/imta.cnf \
	lib/config-templates/option.dat \
	lib/config-templates/mappings \
	lib/config-templates/dispatcher.cnf \
	lib/config-templates/html/main.js \
	lib/config-templates/html/mbox_fs.html \
	lib/config-templates/html/opts_fs.html \
	lib/config-templates/html/spell.html \
	lib/locale/de/return_option.opt \
	lib/locale/es/return_option.opt \
	lib/locale/fr/return_option.opt \
	lib/config-templates/return_header.opt"
config_configlist="\
	config/imta.cnf \
	config/option.dat \
	config/mappings \
	config/dispatcher.cnf \
	config/html/main.js \
	config/html/mbox_fs.html \
	config/html/opts_fs.html \
	config/html/spell.html \
	config/locale/de/return_option.opt \
	config/locale/es/return_option.opt \
	config/locale/fr/return_option.opt \
	config/return_header.opt"
#
# 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() {
  log_only "-- creating patch directory $PATCHDIR"
  mkdir -p $PATCHDIR/backout $PATCHDIR/save
  BACKOUTDIR=$PATCHDIR/backout
  SAVEDIR=$PATCHDIR/save
}

#
# was configure run previously?
# - sets the variable isConfigured to either 0 or 1
# - requires basedir
get_isConfigured() {
  if [ -r $basedir/config ]; then
    isConfigured=1
  else
    isConfigured=0
  fi
  log_only "-- isConfigured = $isConfigured"
}

#
# special processing for files that have moved locations
# - return_header.opt 
#   - moved template location: lib/locale/C to lib/config-templates
#   - moved config location: from config/locale/C/ to config template
#   - 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/$file $BACKOUTDIR/$file"
    /usr/bin/cp -p $basedir/lib/locale/C/return_header.opt $basedir/lib/config-templates/return_header.opt >> $LOGFILE 2>&1
  fi
  if [ ! -f $basedir/config/return_header.opt -a -f $basedir/config/locale/C/return_header.opt ]; then
    log_only "/usr/bin/cp -p $basedir/config/locale/C/return_header.opt $basedir/config/return_header.opt"
    /usr/bin/cp -p $basedir/config/locale/C/return_header.opt $basedir/config/return_header.opt
  fi
}

#
# save config files prior to it being patched
#
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
    log_only "/usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file"
    /usr/bin/cp -p $basedir/$file $BACKOUTDIR/$file >> $LOGFILE 2>&1
    # diff3 will core dump if the file does not end in a newline
    # so add a new line if it is missing
    log_only "lines=\`/usr/bin/tail -1 $BACKOUTDIR/$file | /usr/bin/wc -l\`"
    lines=`/usr/bin/tail -1 $BACKOUTDIR/$file | /usr/bin/wc -l`
    log_only "lines=$lines"
    if [ $lines -eq 0 ]; then
     log_only "-- Adding extra line to $BACKOUTDIR/$file"
     /usr/bin/echo "" >> $BACKOUTDIR/$file
    fi
  done
}

#
# 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
create_staging_area
get_isConfigured
if [ $isConfigured -eq 1 ]; then
  save_movedfiles
  save_config
fi
