# --
# suse-rcotrs - rc script of otrs for SuSE Linux
# Copyright (C) 2002 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: suse-rcotrs,v 1.4 2002/02/03 22:48:20 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.
# --

# PS: I'm not a shell hacker! 

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

OTRS_CONFIG=/etc/sysconfig/otrs
test -r $OTRS_CONFIG || exit 6
. $OTRS_CONFIG

# 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 fount!"
    exit 5
fi

OTRS_SPOOLDIR=/opt/OpenTRS/var/spool
if ! test -d $OTRS_SPOOLDIR; then
    echo "Error: $OTRS_SPOOLDIR not fount!"
    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)
      echo "Starting OpenTRS"

      # --
      # 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 ... faild."
          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 ... faild."
          rc_failed
          rc_status -v 
          exit 1;
        fi
      else 
          echo " Disabled: mysql check!"
      fi

      # --
      # database connect
      # --
      echo -n " Checking database connect... ("
      if ! /opt/OpenTRS/bin/CheckDB.pl; 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

      # --
      # 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 "  -->> http://$HOST/otrs/index.pl <<-- "

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

      # --
      # 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 ... faild."
          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 ... faild."
          rc_failed
        fi
      fi

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

      # --
      # mysql
      # --
      rcmysql status

      # --
      # 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|status|restart}"
    exit 1
esac

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

