#!/bin/bash
# starts neverending loop
echo "$$" >/var/run/zerotoaster.pid
# exec >>/var/log/zerotoaster.log 2>&1
while true
do
   ZTDIR=/opt/zerotoaster
   JAVADIR=/usr/java/j2re1.4.2_09/bin
   export JAVADIR
   cd $ZTDIR
   if [ -e _zt_mta.jar ]; then
      echo "-- deploying MTA update"
      mv -f zt_mta.jar zt_mta.old
      echo "-- old version deleted"
      mv _zt_mta.jar zt_mta.jar
      echo "-- new version ready"
   fi

   if [ -e _201_util.jar ]; then
      echo "-- deploying Util update"
      mv -f 201_util.jar 201_util.old
      echo "-- old version deleted"
      mv _201_util.jar 201_util.jar
      echo "-- new version ready"
   fi

   if [ -e _dns.jar ]; then
      echo "-- deploying DNS update"
      mv -f dns.jar dns.old
      echo "-- old version deleted"
      mv _dns.jar dns.jar
      echo "-- new version ready"
   fi

   echo "-- starting mta"
   $JAVADIR/java -Xmx384m -cp "$ZTDIR"/zt_mta.jar:"$ZTDIR"/201_util.jar:"$ZTDIR"/dns.jar net.zerotoaster.mta.main.ZeroToaster "$@"
   RETVAL=$?
   echo "-- mta terminated"
   echo "-- with returncode:"
   echo $RETVAL
   case $RETVAL in
	0)
	   # -- exit by "shut down server", returncode 0
	   echo "-- shutting down server"
	   break ;;
	50)
	   # -- exit by "full restart", returncode 50
	   echo "-- full restart"
	   continue ;;
	*)
	   echo "-- no action specified for this returncode"
	   continue ;;
   esac
   echo "-- and we start again"
done
echo "End of script"
