# !/bin/sh
# --
# suse-rcotrs - rc script of otrs for SuSE Linux
# Copyright (C) 2002 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: suse-rcotrs,v 1.10 2002/05/20 23:57:27 martin Exp $
# --
# 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.
# --

### BEGIN INIT INFO
# Provides:          otrs
# Required-Start:    mysql apache
# Required-Stop:
# Default-Start:     3
# Default-Stop:
# Description:       Open Ticket Request System
### END INIT INFO


# PS: I'm not a shell hacker! 

#
# load the configuration
#
test -r /etc/rc.config && . /etc/rc.config

test -r /etc/sysconfig/otrs && . /etc/sysconfig/otrs

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
test -s /etc/rc.status && . /etc/rc.status

# 
# 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) 
# 
DB_RUNNING=1
HTTP_RUNNING=1

# 
# check needed files
# 
OTRS_POSTMASTER=/opt/OpenTRS/bin/PostMaster.pl
if ! test -r $OTRS_POSTMASTER; then
    echo "Error: $OTRS_POSTMASTER not found!"
    exit 5
fi

OTRS_SPOOLDIR=/opt/OpenTRS/var/spool
if ! test -d $OTRS_SPOOLDIR; then
    echo "Error: $OTRS_SPOOLDIR not found!"
    exit 5
fi

OTRS_CHECKDB=/opt/OpenTRS/bin/CheckDB.pl
if ! test -r $OTRS_CHECKDB; then
    echo "Error: $OTRS_CHECKDB not found!"
    exit 5
fi
 

# 
# get host name
#
HOST=`hostname -f`

# reset status of this service
rc_reset

#
# The echo return value for success (defined in /etc/rc.config).
#
return=$rc_done

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

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

      # --
      # check mysql
      # --
      if test $DB_RUNNING -gt 0; then
        if rcmysql status > /dev/null 2>&1 ; then 
          echo " Checking mysql ... done."
          rc_status 
        else 
          echo " Checking mysql ... failed."
          echo "  --> Please start the database at first! (rcmysql start) <--"
          rc_failed
          rc_status -v 
          exit 1;
        fi
      else 
          echo " Disabled: mysql check!"
      fi

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

      # --
      # disable PostMaster.pl
      # --
      echo -n " Enable $OTRS_POSTMASTER ..."
      if chmod 755 $OTRS_POSTMASTER; then
          echo " done."
          rc_status
      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 || rc_failed) 
      done
      echo " done."
      rc_status
     
      echo ""
      echo "  -->> http://$HOST/otrs/index.pl <<-- "

      # show status
      rc_status -v
    ;;
    # ------------------------------------------------------
    # stop
    # ------------------------------------------------------
    stop)
      echo "Shutting down OpenTRS "

      # --
      # disable PostMaster.pl
      # --
      echo -n " Disable $OTRS_POSTMASTER ..."
      if chmod 644 $OTRS_POSTMASTER; then
          echo " done."
          rc_status
      else
          echo " failed."
      fi

      # show status
      rc_status -v

    ;;
    # ------------------------------------------------------
    # start-force
    # ------------------------------------------------------
    start-force)
      echo "Starting OpenTRS (completely)"

      # --
      # start apache
      # --
      if test $HTTP_RUNNING -gt 0; then
        if rcapache restart > /dev/null 2>&1 ; then
          echo " Starting httpd ... done."
          rc_status
        else
          echo " Starting httpd ... failed!"
          rc_failed
          exit 1
        fi
      else
          echo " Disabled: httpd check!"
      fi

      # --
      # check mysql
      # --
      if test $DB_RUNNING -gt 0; then
        if rcmysql restart > /dev/null 2>&1 ; then 
          echo " Starting mysql ... done."
          rc_status 
        else 
          echo " Starting mysql ... failed."
          rc_failed
          rc_status -v 
          exit 1;
        fi
      else 
          echo " Disabled: mysql check!"
      fi

      # --
      # start normal
      # --
      $0 start

      # show status
      rc_status -v
    ;;
    # ------------------------------------------------------
    # stop-force
    # ------------------------------------------------------
    stop-force)
      # --
      # stop normal
      # --
      $0 stop

      # --
      # stop force
      # --
      echo "Shutting down OpenTRS (completely)"

      # --
      # httpd
      # --
      if test $HTTP_RUNNING -gt 0; then
        if rcapache stop > /dev/null 2>&1 ; then
          echo " Shutting down apache ... done."
          rc_status 
        else
          echo " Shutting down apache ... failed."
          rc_failed
        fi
      fi

      # --
      # mysql
      # --
      if test $DB_RUNNING -gt 0; then
        if rcmysql stop > /dev/null 2>&1 ; then
          echo " Shutting down mysql ... done."
          rc_status 
        else
          echo " Shutting down mysql ... failed."
          rc_failed
        fi
      fi

      # show status
      rc_status -v
    ;;
    # ------------------------------------------------------
    # restart
    # ------------------------------------------------------
    restart)
      $0 stop  
      $0 start
      # Remember status and be quiet
      rc_status
    ;;
    # ------------------------------------------------------
    # restart-force
    # ------------------------------------------------------
    restart-force)
      $0 stop-force  && sleep 3
      $0 start-force
      # Remember status and be quiet
      rc_status
    ;;
    # ------------------------------------------------------
    # status
    # ------------------------------------------------------
    status) 
      # --
      # httpd
      # --
      rcapache status

      # --
      # mysql
      # --
      rcmysql status

      # --
      # db check
      # --
      echo -n "Checking database connect... ("
      if ! $OTRS_CHECKDB; then
          echo ") "
          echo "----------------------------------------------------------------------------"
          echo " Error: May your database isn't configured yet? "
          echo "----------------------------------------------------------------------------"
      else
          echo ")."
      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.
rc_exit

