#!/bin/bash
#
# oar-server     OAR Server
#
# chkconfig: 2345 90 10
# description: This script starts or stops the OAR resource manager
# processname: Almighty
# config: /etc/oar/oar.conf
# pidfile: /var/run/oar-server.pid

### BEGIN INIT INFO
# Provides:          oar-server
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Should-Start:      postgresql mysql-server
# Should-Stop:       postgresql mysql-server
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: OAR server
# Description:       This script starts or stops the OAR resource manager
### END INIT INFO

LANG=C
export LANG

PATH=/usr/sbin:/usr/bin:/sbin:/usr/sbin:/bin:/usr/bin:$PATH
NAME=oar-server
DESC="OAR server" 
DAEMON=/usr/sbin/oar-server
DAEMON_NAME=Almighty
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
SELINUXENABLED_CMD=$(which selinuxenabled 2>/dev/null)
NOLSB=

[ -f /lib/lsb/init-functions ] || NOLSB=yes

if [ -f /etc/debian_version ]; then
    system=debian
elif [ -f /etc/redhat-release ]; then
    system=redhat
elif [ -f /etc/SuSE-release ]; then
    system=suse
elif [ -f /etc/gentoo-release ]; then
    system=gentoo
fi

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

if [ -z "$NOLSB" ]; then
    . /lib/lsb/init-functions
    fail_msg() {
        echo ""
        log_failure_msg "$@"
    }
    warn_msg() {
        log_warning_msg "$@"
    }
    succ_msg() {
        log_success_msg "$@"
    }
    begin_msg() {
        echo -n "$@:"
    }
else
    echo "This system doesn't provide the LSB functions. Failing"
    exit 1
fi

do_start() {
    begin_msg "Starting $DESC"
    if [ -n "$SELINUXENABLED_CMD" -a -x "$SELINUXENABLED_CMD" ] && "$SELINUXENABLED_CMD"; then
        fail_msg "SELinux is enabled, $DESC cannot be started."	
        exit 1
    fi

    CHECK_STRING=$(oar-database --check > /dev/null 2>&1) 
    if [ "$?" -ne "0" ]; then
        fail_msg "OAR database seems not ready. See \`oar-database --check'"
        exit 1
    fi
    if pidofproc -p $PIDFILE $DAEMON > /dev/null; then
        warn_msg "$DESC is already running."
        exit 0
    fi

    start_daemon -p "$PIDFILE" "$DAEMON" $DAEMON_ARGS
    RET=$?
    if [ "$RET" != 0 ]; then
        fail_msg "Unable to start $DESC."
        exit 1
    else
        succ_msg

        # redhat world
        [ -d /var/lock/subsys/ ] && touch /var/lock/subsys/$NAME
    fi
}



#
# Function that stops the daemon/service
#
do_stop()
{
    begin_msg "Stopping $DESC"
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    killproc -p $PIDFILE
    RETVAL="$?"
    if [ "$RETVAL" = 2 ]; then
        fail_msg "Unable to stop $DESC."
        exit 2
    fi
    # Sarko is often frozed when the database is unreachable
    killproc sarko
    # Extermination...
    killproc $DAEMON_NAME
    if [ "$?" = 2 ]; then
        fail_msg "Unable to stop $DESC."
        exit 2
    fi
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE

    # redhat world
    [ -d /var/lock/subsys/ ] && rm -f /var/lock/subsys/$NAME
    
    succ_msg
    return 0
}



#
# utility functions to check the status of the daemon
#
__pids_var_run() {
  local base=${1##*/}
  local pid_file=${2:-/var/run/$base.pid}

  pid=

  if [ -f "$pid_file" ]; then
    local line p

    [ ! -r "$pid_file" ] && return 4 # "user had insufficient privilege"

    while : ; do
      read line

      [ -z "$line" ] && break

      for p in $line ; do
        [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"
      done
    done < "$pid_file"

    if [ -n "$pid" ]; then
      return 0
    fi

    return 1 # "Program is dead and /var/run pid file exists"
  fi

  return 3 # "Program is not running"
}

__pids_pidof() {
  pidof -c -o $$ -o $PPID -o %PPID -x "$1" || \
  pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"
}

#
# Function that checks the daemon/service
#

status() {
  local base pid lock_file pid_file
  # Test syntax
  if [ "$#" = 0 ] ; then
    echo $"Usage: status [-p pidfile] {program}"
    return 1
  fi

  if [ "$1" = "-p" ]; then
    pid_file=$2
    shift 2
  fi

  if [ "$1" = "-l" ]; then
    lock_file=$2
    shift 2
  fi

  base=${1##*/}

  # First try "pidof"
  __pids_var_run "$1" "$pid_file"

  RC=$?

  if [ -z "$pid_file" -a -z "$pid" ]; then
    pid="$(__pids_pidof "$1")"
  fi

  if [ -n "$pid" ]; then
    echo $"${base} (pid $pid) is running..."
    return 0
  fi

  case "$RC" in
    0)
      echo $"${base} (pid $pid) is running..."
      return 0
    ;;
    1)
      echo $"${base} dead but pid file exists"
      return 1
    ;;
    4)
      echo $"${base} status unknown due to insufficient privileges."
      return 4
    ;;
  esac

  if [ -z "${lock_file}" ]; then
    lock_file=${base}
  fi
  # See if /var/lock/subsys/${lock_file} exists
  if [ -f /var/lock/subsys/${lock_file} ]; then
    echo $"${base} dead but subsys locked"
    return 2
  fi

  echo $"${base} is stopped"
  return 3
}


case "$1" in
    start)
        do_start
    ;;
    stop)
        do_stop
    ;;
    reload|restart|force-reload)
        if do_stop; then
            sleep 1
            do_start
        fi
    ;;
    status)
        status -p $PIDFILE $NAME
        exit $?
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|status|force-reload}" >&2
        exit 1
    ;;
esac

:
