#! /bin/sh
#
# This is the original RTM version that this patch applies to
VERSION="6.3,REV=2004.08.12"

dssetup_list="\
	lib/comm_dssetup.pl \
	lib/schema/20subscriber.ldif \
        lib/schema/50ns-delegated-admin.ldif \
        lib/schema/50ns-mail.ldif \
	lib/schema/50ns-mlm.ldif \
	lib/schema/50ns-msg.ldif \
	lib/schema/50ns-value.ldif \
	lib/schema/55ims-ical.ldif \
	lib/schema/56ims-schema.ldif \
	lib/schema/60iplanet-calendar.ldif \
	lib/schema/50ns-iabs.ldif \
	lib/schema/98ns-dummy-uwc.ldif \
	lib/schema/ns-value-schema.conf \
	lib/schema/ns-mail-schema.conf \
	lib/schema/ns-mlm-schema.conf \
	lib/schema/ns-msg-schema.conf \
	lib/schema/ns-delegated-admin-schema.conf \
	lib/schema/um50-common-schema.conf \
	lib/schema/ims50-schema.conf \
	lib/schema/ics50-schema.conf \
	lib/schema/merged.at.conf \
	lib/schema/merged.oc.conf"

#
# 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/SUNWcomds*`
  # 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 prepatch, did not find a SUNWcomds that matches expected VERSION: $VERSION"
    exit 1
  fi
}

#
# 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
}

#
# main program starts here
#
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"
save_dssetup
