#!/bin/sh
# --
# suse-rcotrs - rc script of otrs for SuSE Linux
# Copyright (C) 2001-2011 OTRS AG, http://otrs.org/
# --
# $Id: suse-rcotrs,v 1.34 2011/08/22 10:59:17 mb Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

### BEGIN INIT INFO
# Provides:          otrs
# Required-Start:    apache
# Required-Stop:
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Description:       OTRS Help Desk.
### END INIT INFO

export LANG=POSIX
PATH="/bin:/usr/bin:/sbin:/usr/sbin"

#
# load the configuration
#
CONFIGFILE="rcconfig file not found!"
test -r /etc/rc.config && . /etc/rc.config

test -r /etc/rc.config.d/otrs && . /etc/rc.config.d/otrs && CONFIGFILE=/etc/rc.config.d/otrs

test -r /etc/sysconfig/otrs && . /etc/sysconfig/otrs && CONFIGFILE=/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

#
# check needed variables
#
if ! test $OTRS_ROOT; then
    echo "Error: Corrupt config file ($CONFIGFILE)! OTRS_ROOT not found! Please check your /etc/sysconfig/otrs - maybe it wasn't changed by rpm!"
    exit 5
fi
if ! test $OTRS_POSTMASTER; then
    echo "Error: Corrupt config file ($CONFIGFILE)! OTRS_POSTMASTER not found! Please check your /etc/sysconfig/otrs - maybe it wasn't changed by rpm!"
    exit 5
fi
if ! test $OTRS_SPOOLDIR; then
    echo "Error: Corrupt config file ($CONFIGFILE)! OTRS_SPOOLDIR not found! Please check your /etc/sysconfig/otrs - maybe it wasn't changed by rpm!"
    exit 5
fi
#
# check needed files
#
if ! test -r $OTRS_ROOT; then
    echo "Error: $OTRS_ROOT not found!"
    exit 5
fi

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

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

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

# 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 $OTRS_PROG"

      # --
      # start web server
      # --
      if test $OTRS_HTTP_RUNNING -gt 0; then
        if $OTRS_USED_WEBSERVER_RCSCRIPT status > /dev/null 2>&1 ; then
          echo " Checking $OTRS_USED_WEBSERVER ... done."
          rc_status
        else
          echo " Checking $OTRS_USED_WEBSERVER ... failed!"
          echo "  --> Please start the web server first! ($OTRS_USED_WEBSERVER_RCSCRIPT start) <--"
          rc_failed
          exit 1
        fi
      else
          echo " Disabled: web server check!"
      fi

      # --
      # database connect
      # --
      echo -n " Checking database connection... ("
      if ! $OTRS_CHECKDB -s 1; then
          echo ") "
          echo "----------------------------------------------------------------------------"
          echo " Error: Maybe your database isn't configured yet? "
          echo "----------------------------------------------------------------------------"
          echo ""
          echo ""
          echo " Try the web installer to configure your database: "
          echo ""
          echo ""
          echo "     -->> http://$OTRS_HOST/$OTRS_HTTP_LOCATION/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

      # --
      # OTRS Scheduler Service
      # --
      echo -n " Checking OTRS Scheduler Service... "
      if ! $OTRS_SCHEDULER_TEST >/dev/null 2>&1; then
          echo " Error! "
      echo "  This can happen if your database is not configured yet. "
          exit 1;
      else
          echo " done."
      fi  

      # --
      # enable otrs.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
          if echo $i | grep -v '*' >> /dev/null; then
              echo -n "   Starting otrs PostMaster... ($i) "
              if cat $i | $OTRS_POSTMASTER >> /dev/null 2>&1; then
                  rm $i && echo "(remove email)";
              else
                  rc_failed
              fi
          fi
      done
      echo " done."
      rc_status

      # --
      # start cron stuff
      # --
      if test $OTRS_CRON_RUNNING -gt 0; then
        if mkdir -p $OTRS_CRON_DIR; cd $OTRS_CRON_DIR && ls *|grep -v '.dist'|grep -v '.save'|grep -v 'CVS'|grep -v '.rpm'|xargs cat > $OTRS_CRON_TMP_FILE && crontab $OTRS_CRON_USER $OTRS_CRON_TMP_FILE ; then
          echo " Creating cronjobs (source $OTRS_CRON_DIR/*) ... done."
          rc_status
        else
          echo " Creating cronjobs (source $OTRS_CRON_DIR/*) ... failed!"
          rc_failed
          exit 1
        fi
      else
          echo " Disabled: cronjobs!"
      fi
      
      echo ""
      echo "  -->> http://$OTRS_HOST/$OTRS_HTTP_LOCATION/index.pl <<-- "

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

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

      # --
      # stop cron stuff
      # --
      if test $OTRS_CRON_RUNNING -gt 0; then
        if type -a crontab 2&>/dev/null && crontab $OTRS_CRON_USER -r ; then
          echo " Shutting down cronjobs ... done."
          rc_status
        else
          echo " Shutting down cronjobs ... failed!"
          rc_failed
          exit 1
        fi
      else
          echo " Disabled: cronjobs!"
      fi

      # --
      # stop scheduler
      # --
      if test $OTRS_SCHEDULER_RUNNING -gt 0; then
          echo -e " Shutting down OTRS scheduler ..."
          $OTRS_SCHEDULER -a stop
          echo " done."
      fi

      # show status
      rc_status -v

    ;;
    # ------------------------------------------------------
    # start-force
    # ------------------------------------------------------
    start-force)
      echo "Starting $OTRS_PROG (completely)"

      # --
      # start web server
      # --
      if test $OTRS_HTTP_RUNNING -gt 0; then
        if $OTRS_USED_WEBSERVER_RCSCRIPT restart > /dev/null 2>&1 ; then
          echo " Starting $OTRS_USED_WEBSERVER ... done."
          rc_status
        else
          echo " Starting $OTRS_USED_WEBSERVER ... failed!"
          rc_failed
          exit 1
        fi
      else
          echo " Disabled: web server check!"
      fi

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

      # show status
      rc_status -v
    ;;
    # ------------------------------------------------------
    # stop-force
    # ------------------------------------------------------
    stop-force)
      # --
      # stop normal
      # --
      $0 stop
      # --
      # clean up
      # --
      if test $OTRS_CLEANUP; then
        if $OTRS_CLEANUP > /dev/null 2>&1 ; then
          echo " Cleaning up session and log cache ... done."
        else
          echo " Cleaning up session and log cache ... failed."
        fi
      fi
      # --
      # stop force
      # --
      echo "Shutting down $OTRS_PROG (completely)"

      # --
      # webserver
      # --
      if test $OTRS_HTTP_RUNNING -gt 0; then
        if $OTRS_USED_WEBSERVER_RCSCRIPT stop > /dev/null 2>&1 ; then
          echo " Shutting down $OTRS_USED_WEBSERVER ... done."
          rc_status
        else
          echo " Shutting down $OTRS_USED_WEBSERVER ... 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
    ;;
    # ------------------------------------------------------
    # try-restart
    # ------------------------------------------------------
    try-restart|condrestart)
      if test "$1" = "condrestart"; then
         echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
      fi
      $0 status
      if test $? = 0; then
        $0 restart
      else
        # Not running is not a failure.
        rc_reset
      fi
      # Remember status and be quiet
      rc_status
    ;;
    # ------------------------------------------------------
    # cleanup
    # ------------------------------------------------------
    cleanup)
      # --
      # 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
      # Remember status and be quiet
      rc_status
    ;;
    # ------------------------------------------------------
    # status
    # ------------------------------------------------------
    status)
      # --
      # web server
      # --
      $OTRS_USED_WEBSERVER_RCSCRIPT status

      # --
      # db check
      # --
      echo -n "Checking database connection... ("
      if ! $OTRS_CHECKDB -s 1; then
          echo ") "
          echo "----------------------------------------------------------------------------"
          echo " Error: Maybe your database isn't configured yet? "
          echo "----------------------------------------------------------------------------"
      else
          echo ")."
      fi

      # --
      # scheduler check
      # --
      echo -n "Checking OTRS Scheduler service.... "
      if ! $OTRS_CHECKSCHEDULER -s 1; then
          echo " Error! "
      echo " Maybe your database isn't configured yet? "
      else
          echo "done."
      fi

      # --
      # postmaster check
      # --
      echo -n "Checking $OTRS_POSTMASTER ... "
      if test -x $OTRS_POSTMASTER; then
          echo "(active) done."
      else
          echo "(not active) 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|cleanup}"
    exit 1
esac

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

