#!/bin/sh
# install.sh - installer for scs 2.1p1

BASEDIR=`dirname $0`/..
SCSHOME=/scs
INSTDIR=$BASEDIR/install
FILELIST=$INSTDIR/file_list
FILEDIR=$BASEDIR/files
PATCHRECDIR=$SCSHOME/etc/patches
PATCHLIST=$INSTDIR/patch_list

# Scripts we run outside of this one
UPGRADE=$INSTDIR/upgrade
PREINST=$INSTDIR/preinstall
POSTINST=$INSTDIR/postinstall

printError() {
	echo "ERROR: $@" 1>&2
}

printInfo() {
	echo "Info: $@" 
}

printWarn() {
	echo "Warn: $@" 
}

errorExit() {
	printError "$@"
	exit 1;
}

# Check some basics
if [ ! -r "$PATCHLIST" ] ; then
	errorExit "Patch list not found! Patch may be corrupt!"
fi

if [ ! -d "$FILEDIR" ]; then
	errorExit "Cannot find files directory: $FILEDIR"
fi

if [ ! -r "$FILELIST" ]; then
	errorExit "Cannot find file list: $FILELIST"
fi

if [ ! -r "$UPGRADE" ]; then
	errorExit "Cannot find file: $UPGRADE"
fi

# stop tomcat
if [ -r "$PREINST" ] ; then
	printInfo "Executing preinstall"
	/bin/sh "$PREINST" || errorExit "Preinstall failed"
	printInfo "Preinstall complete"
fi

$UPGRADE $FILEDIR $FILELIST
if [ $? != 0 ]; then
	/bin/sh "$POSTINST" || errorExit "Postinstall failed"
	errorExit "Error running upgrade script"
fi

# start tomcat
if [ -r "$POSTINST" ] ; then
	printInfo "Executing postinstall"
	/bin/sh "$POSTINST" || errorExit "Postinstall failed"
	printInfo "Postinstall complete"
fi

if [ ! -d "$PATCHRECDIR" ]; then
	mkdir -p $PATCHRECDIR
fi

( . $PATCHLIST ;
  if [ X"$PATCH_NAME" = "X" ] ; then
     errorExit "No patch name!" ;
  fi
  sed -e "s/\#\#INSTALLDATE\#\#/`date`/" < $PATCHLIST > \
  "$PATCHRECDIR/$PATCH_NAME" || \
  errorExit "Cannot record patch install" ; ) || exit 1

echo
printInfo "Installation Complete"

exit 0


