#!/bin/sh
#
# SPAMCONTROL (UN)INSTALL Script (spamcontrol.sh)
# -----------------------------------------------
#
# Purpose:      To install and uninstall the SPAMCONTROL patch
#
# Parameters:   -u (uninstall)
#	        VRF (Version to be uninstalled)
#
# Usage:        ./spamcontrol.sh [-u] [Version]
#
#		Installation: 	./spamcontrol.sh
# 		Uninstallation: ./spamcontrol.sh -u 105
#
# Return Codes: 0 - Patches applied successfully
#		1 - Original QMAIL files not found (Patch not extracted in QMAIL source directory)
#		2 - Patch files not found 
#
# Output:	spamcontrol.log
#
# History:      1.0.0 - Erwin Hoffmann - Initial release
#		1.0.1 - Erwin Hoffmann - Some more verbose information
#		1.0.3 - Erwin Hoffmann - REV=103, removed "cp" in some output line
#		1.0.4 - Erwin Hoffmann - REV=104, small bug fixed
#		1.0.5 - Erwin Hoffmann - REV=105
#		1.0.6 - Erwin Hoffmann - REV=106, Uninstallation added
#		1.0.7 - Erwin Hoffmann - REV=107, Flag for Solaris added
#
#
#---------------------------------------------------------------------------------------
#
DATUM=`(date)`
LOCDIR=${PWD}
LOGFILE=spamcontrol.log
REV=142 # Should be identical to SPAMCONTROL level
FLAG=""
# FLAG="-i" # For Solaris only

if [ $# -eq 0 ] ; then

echo ">>> Running spamcontrol.sh INSTALLATION at $DATUM for Revision $REV <<<" >> $LOGFILE 

if [ -s Makefile ] ; then
  cp Makefile Makefile.$REV 2>> $LOGFILE
  echo "--> Makefile copied to Makefile.$REV" >> $LOGFILE
else
  echo "Makefile not found !"
  exit 1
fi
if [ -s Makefile.patch ] ; then
  patch $FLAG Makefile Makefile.patch 2>> $LOGFILE
else
  echo "!! Makefile.patch not found !"
  exit 2
fi

if [ -s qmail-smtpd.c ] ; then
  cp qmail-smtpd.c qmail-smtpd.c.$REV 2>> $LOGFILE
  echo "--> qmail-smtpd.c copied to qmail-smtpd.c.$REV" >> $LOGFILE
else
  echo "!! qmail-smtpd.c not found !"
  exit 1
fi
if [ -s qmail-smtpd.c.patch ] ; then
  patch $FLAG qmail-smtpd.c qmail-smtpd.c.patch 2>> $LOGFILE
else
  echo "!! qmail-smtpd.c.patch not found !"
  exit 2
fi

if [ -s qmail-smtpd.8 ] ; then
  cp qmail-smtpd.8 qmail-smtpd.8.$REV 2>> $LOGFILE
  echo "--> qmail-smtpd.8 copied to qmail-smtpd.8.$REV" >> $LOGFILE
else
  echo "!! qmail-smtpd.8 not found !"
  exit 1
fi
if [ -s qmail-smtpd.8.patch ] ; then
  patch $FLAG qmail-smtpd.8 qmail-smtpd.8.patch 2>> $LOGFILE
else
  echo "!! qmail-smtpd.8.patch not found !"
  exit 2
fi

if [ -s qmail-showctl.c ] ; then
  cp qmail-showctl.c qmail-showctl.c.$REV 2>> $LOGFILE
  echo "--> qmail-showctl.c copied to qmail-showctl.c.$REV" >> $LOGFILE
else
  echo "!! qmail-showtl.c not found !"
  exit 1
fi
if [ -s qmail-showctl.c.patch ] ; then
  patch $FLAG qmail-showctl.c qmail-showctl.c.patch 2>> $LOGFILE
else
  echo "!! qmail-showctl.c.patch not found !"
  exit 2
fi

if [ -s qmail-control.9 ] ; then
  cp qmail-control.9 qmail-control.9.$REV 2>> $LOGFILE
  echo "--> qmail-control.9 copied to qmail-control.9.$REV" >> $LOGFILE
else
  echo "!! qmail-control.9 not found !"
  exit 1
fi
if [ -s qmail-control.9.patch ] ; then
  patch $FLAG qmail-control.9 qmail-control.9.patch 2>> $LOGFILE
else
  echo "!! qmail-control.9.patch not found !"
  exit 2
fi

if [ -s qmail-log.5 ] ; then
  cp qmail-log.5 qmail-log.5.$REV 2>> $LOGFILE
  echo "--> qmail-log.5 copied to qmail-log.5.$REV" >> $LOGFILE
else
  echo "!! qmail-log.5 not found !"
  exit 1
fi
if [ -s qmail-log.5.patch ] ; then
  patch $FLAG qmail-log.5 qmail-log.5.patch 2>> $LOGFILE
else
  echo "!! qmail-log.5.patch not found !"
  exit 2
fi

# Now go for the uninstallation....

elif [ "$1" = "-u" ] ; then

# Get the Version Number from INPUT 

if [ $# -eq 2 ] ; then
   if [ $2 -gt 100 ] ; then
      REV=$2
   fi
fi

echo ">>> Running spamcontrol.sh UNINSTALLATION at $DATUM for Revision $REV <<<" >> $LOGFILE 

if [ -s Makefile.$REV ] ; then
  mv Makefile.$REV Makefile 2>> $LOGFILE
  echo "--> Makefile.$REV moved to Makefile" >> $LOGFILE
else
  echo "!! Makefile.$REV not found !"
  exit 1
fi

if [ -s qmail-smtpd.c.$REV ] ; then
  mv qmail-smtpd.c.$REV qmail-smtpd.c 2>> $LOGFILE
  echo "--> qmail-smtpd.c.$REV moved to qmail-smtpd.c" >> $LOGFILE
else
  echo "!! qmail-smtpd.c.$REV not found !"
  exit 1
fi

if [ -s qmail-smtpd.8.$REV ] ; then
  mv qmail-smtpd.8.$REV qmail-smtpd.8 2>> $LOGFILE
  echo "--> qmail-smptd.8.$REV moved to qmail-smtpd.8" >> $LOGFILE
else
  echo "!! qmail-smtpd.8.$REV not found !"
  exit 1
fi

if [ -s qmail-showctl.c.$REV ] ; then
  mv qmail-showctl.c.$REV qmail-showctl.c 2>> $LOGFILE
  echo "--> qmail-showctl.c.$REV moved to qmail-showctl.c" >> $LOGFILE
else
  echo "!! qmail-showctl.c.$REV not found !"
  exit 1
fi

if [ -s qmail-control.9.$REV ] ; then
  mv qmail-control.9.$REV qmail-control.9 2>> $LOGFILE
  echo "--> qmail-control.9.$REV moved to qmail-control.9" >> $LOGFILE
else
  echo "!! qmail-control.9.$REV not found !"
  exit 1
fi

if [ -s qmail-log.5.$REV ] ; then
  mv qmail-log.5.$REV qmail-log.5 2>> $LOGFILE
  echo "--> qmail-log.5.$REV moved to qmail-log.5" >> $LOGFILE
else
  echo "!! qmail-log.5.$REV not found !"
  exit 1
fi


fi

exit 0
# Thats it!
