# !/bin/sh
# --
# redhat-rcotrs - rc script of otrs for RedHat Linux
# Copyright (C) 2002 Martin Edenhofer <martin+code@otrs.org>
# Copyright (C) 2002 Pablo Ruiz <pruiz@ip6seguridad.com>
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see 
# the enclosed file COPYING for license information (GPL). If you 
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --
# chkconfig: 2345 92 92
# description: Open Ticket Request System
# config: /etc/sysconfig/otrs

# load the configuration
#
test -r /etc/sysconfig/otrs && . /etc/sysconfig/otrs


# --- All this shit will go on /etc/sysconfig/otrs when i'll got
# --- time and make another rpm.. (maybe on beta7? ;)) [pruiz@ip6seguridad.com]
 
# if one of this is false, it will not be checked at the startup!
# Note: (may be the database isn't on the same host! --> DB_RUNNING=0) 
# 
OTRS_ROOT=/opt/OpenTRS
DB_RUNNING=1
HTTP_RUNNING=1
CRON_RUNNING=1

#
# Used database [mysql|postgresql]
# Note: Take care, that you have changed the Config.pm database settings!
#

HOST=`hostname -f`

USED_DB=MySQL
USED_DB_TEST="ps --pid $(cat /var/lib/mysql/$HOST.pid)"
# --- The Wonderfull redhat's mysql init script does not have a status check.. sucks
#USED_DB=PostgreSQL
#USED_DB_RCSCRIPT="service postgresql status| grep 'is running' > /dev/null 2>&1"

USED_WEBSERVER=httpd   #Redhat's default httpd is apache..
USED_WEBSERVER_TEST="service httpd status | grep 'is running'"

OTRS_POSTMASTER=$OTRS_ROOT/bin/PostMaster.pl
OTRS_SPOOLDIR=$OTRS_ROOT/var/spool
OTRS_CHECKDB=$OTRS_ROOT/bin/CheckDB.pl

CRON_DIR=$OTRS_ROOT/var/cron
CRON_TMP_FILE=$OTRS_ROOT/var/tmp/cron
CRON_USER=" -u otrs "

OTRS_HTTP_LOCATION="otrs"

# --- END SHIT ;)
# 
# check needed files
# 
if ! test -r $OTRS_POSTMASTER; then
    echo "Error: $OTRS_POSTMASTER not found!"
    exit 5
fi

if ! test -d $OTRS_SPOOLDIR; then
    echo "Error: $OTRS_SPOOLDIR not found!"
    exit 5
fi

if ! test -r $OTRS_CHECKDB; then
    echo "Error: $OTRS_CHECKDB not found!"
    exit 5
fi
 

prog="OpenTRS"
# 
# main part
# 
case "$1" in
    # ------------------------------------------------------
    # start
    # ------------------------------------------------------
    start)
      echo $"Starting $prog.."

      # --
      # test if apache is running..
      # --
      if test $HTTP_RUNNING -gt 0; then
	echo -n " Checking httpd ..."	
        if $USED_WEBSERVER_TEST >/dev/null 2>&1; then
          echo " done."
        else
          echo "  failed!"
          echo "  --> Please start the webserver at first! (service $USED_WEBSERVER start) <--"
          exit 1
        fi
      else
          echo " Disabled: httpd check!"
      fi

      # --
      # check mysql, mysql init script does not have a status check...
      # --
      if test $DB_RUNNING -gt 0; then
	echo -n " Checking MySQL ..."
        if $USED_DB_TEST >/dev/null 2>&1; then 
          echo " done."
        else 
          echo " failed."
          echo "  --> Please start the database at first! (service mysql start) <--"
          exit 1;
        fi
      else 
          echo " Disabled: mysql check!"
      fi

      # --
      # database connect
      # --
      echo -n " Checking database connect... "
      if ! $OTRS_CHECKDB; then
          echo " Error! "
	  echo "  May your database isn't configured yet? "
          echo ""
          echo " Try the web installer to configure your database: "
          echo ""
          echo "     -->> http://$HOST/$OTRS_HTTP_LOCATION/installer.pl <<-- "
          echo ""                               
          echo " or configure your database with README.database (DB - Setup Example)    "
          exit 1;
      else 
          echo " done."
      fi

      # --
      # disable PostMaster.pl
      # --
      echo -n " Enable $OTRS_POSTMASTER ..."
      if chmod 755 $OTRS_POSTMASTER; then
          echo " done."
      else
          echo " failed."
      fi

      # --
      # check otrs spool dir
      # --
      echo -n " Checking otrs spool dir... "
      for i in $OTRS_SPOOLDIR/* ; do
          # process old emails
          echo $i | grep -v '*' >> /dev/null && \
            echo "" && \
            echo -n "   Starting otrs PostMaster... ($i) " && \
            cat $i | $OTRS_POSTMASTER >> /dev/null 2>&1 && \
            echo -n "remove email... " && \
            (rm $i || echo "failed") 
      done
      echo " done."
      
      # --
      # Cron stuff
      # -- 
      if test $CRON_RUNNING -gt 0; then
	echo -e " Creating cronjobs (source $CRON_DIR/*) ..."
        if mkdir -p $CRON_DIR; cat $CRON_DIR/* > $CRON_TMP_FILE && crontab $CRON_USER $CRON_TMP_FILE ; then
          echo " done."
        else
          echo " failed!"
          exit 1
        fi
      else
          echo " Disabled: cronjobs!"
      fi
 
      echo ""
      echo "  -->> http://$HOST/otrs/index.pl <<-- "
      echo $"Final start of $prog.. done"
    ;;
    # ------------------------------------------------------
    # stop
    # ------------------------------------------------------
    stop)
      echo "Shutting down OpenTRS "

      # --
      # disable PostMaster.pl
      # --
      echo -n " Disable $OTRS_POSTMASTER ..."
      if chmod 644 $OTRS_POSTMASTER; then
          echo " done."
      else
          echo " failed."
      fi
      # --
      # stop cron stuff
      # --
      if test $CRON_RUNNING -gt 0; then
        echo -e " Shutting down cronjobs ..."
	if crontab $CRON_USER -r ; then
          echo " done."
        else
          echo " failed!"
          exit 1
        fi
      else
          echo " Disabled: cronjobs!"
      fi

      echo $"Final shutdown of $prog.. done"
    ;;
    # ------------------------------------------------------
    # restart
    # ------------------------------------------------------
    restart)
      $0 stop  
      $0 start
      # Remember status and be quiet
    ;;
    # ------------------------------------------------------
    # status
    # ------------------------------------------------------
    status) 
      # --
      # httpd
      # --
      service httpd status

      # --
      # mysql
      # --
      service mysql status
      # --
      # db check
      # --
      echo -n "Checking database connect... "
      if ! $OTRS_CHECKDB; then
          echo " Error! "
	  echo " May your database isn't configured yet? "
      else
          echo "done."
      fi

      # --
      # postmaster check
      # --
      echo -n "Checking $OTRS_POSTMASTER ... "
      if test -x $OTRS_POSTMASTER; then
          echo "(activ) done."
      else
          echo "(not activ) failed."
      fi

      # --
      # spool dir
      # --
      echo -n "Checking otrs spool dir... "
      for i in $OTRS_SPOOLDIR/* ; do
          # echo old emails
          echo $i | grep -v '*' > /dev/null && \
            echo "" && \
            echo -n " (message:$i) found! " 
      done
      echo "done."
    ;;
    *)
    echo "Usage: $0 {start|stop|stop-force|start-force|status|restart|restart-force}"
    exit 1
esac

# Inform the caller not only verbosely and set an exit status.

