#!/bin/bash
#
# Init file for the A-REX service
#
# chkconfig: 2345 75 25
# description: NorduGrid A-REX
#
# config: /etc/sysconfig/globus
# config: /etc/sysconfig/nordugrid
# config: /cvmfs/dirac.egi.eu/dirac/v8.0.43/Linux-x86_64/etc/arc.conf
# config: /etc/arc.conf
#
# This startup script takes ARC0 configuration file as
# its input and generates ARC1 arched configuration file
# which contains commands to start A-REX service. Service
# is either run isolated or with WS interface enabled.
# To enable WS interface ARC0 configuration file must 
# contain [arex/ws/jobs] section
# and mandatory option in [arex/ws]:
#    wsurl="a_rex_url"

### BEGIN INIT INFO
# Provides:          arc-arex
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ARC grid manager
# Description:       The unit of the NorduGrid's ARC middleware to 
#                    accept and control jobs. 
### END INIT INFO

# source function library
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    log_success_msg() {
        echo -n "$@"
        success "$@"
        echo
    }
    log_warning_msg() {
        echo -n "$@"
        warning "$@"
        echo
    }
    log_failure_msg() {
        echo -n "$@"
        failure "$@"
        echo
    }
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    echo "Error: Cannot source neither init.d nor lsb functions"
    exit 1
fi

prog=arched

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
elif [ -r /etc/default/nordugrid ]; then
    . /etc/default/nordugrid
fi
if [ -r /etc/sysconfig/arc-arex ]; then
    . /etc/sysconfig/arc-arex
elif [ -r /etc/default/arc-arex ]; then
    . /etc/default/arc-arex
fi

# ARC_LOCATION
ARC_LOCATION=${ARC_LOCATION:-/cvmfs/dirac.egi.eu/dirac/v8.0.43/Linux-x86_64}
if [ ! -d "$ARC_LOCATION" ]; then
    log_failure_msg "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi

# PID and lock file
PID_FILE=`${ARC_LOCATION}/share/arc/arc-arex-start --getpidfile`
if [ $? -ne 0 ]; then
    # When --getpidfile fails it returns the error on stdout
    log_failure_msg "$PID_FILE"
    exit 1
fi

if [ `id -u` = 0 ] ; then
    # Debian does not have /run/lock/subsys
    if [ -d /run/lock/subsys ]; then
        LOCKFILE=/run/lock/subsys/arex
    else
        LOCKFILE=/run/lock/arex
    fi
else
    LOCKFILE=$HOME/arex.lock
fi

start() {
    echo -n "Starting $prog: "

    # Check if we are already running
    if [ -f "$PID_FILE" ]; then
        read pid < "$PID_FILE"
        if [ "x$pid" != "x" ]; then
            ps -p "$pid" -o comm 2>/dev/null | grep "^$prog$" 1>/dev/null 2>/dev/null
            if [ $? -eq 0 ] ; then
                log_success_msg "already running (pid $pid)"
                return 0
            fi
        fi
        rm -f "$PID_FILE" "$LOCKFILE"
    fi

    ${ARC_LOCATION}/share/arc/arc-arex-start
    RETVAL=$?

    if [ $RETVAL -eq 0 ]; then
        touch $LOCKFILE
        log_success_msg
    else
        log_failure_msg
    fi
    return $RETVAL
}

stop() {
    echo -n "Stopping $prog: "

    if [ -f "$PID_FILE" ]; then
        read pid < "$PID_FILE"
        if [ ! -z "$pid" ] ; then
            if [ "x$1" != "x" ]; then
                # kill whole process group on force-kill
                kill -TERM "-$pid"
            else
                kill "$pid"
            fi
            RETVAL=$?
            if [ $RETVAL -eq 0 ]; then
                log_success_msg
            else
                log_failure_msg
            fi
      
            timeout=300; # for stopping nicely
            if [ "x$1" != "x" ]; then
                timeout=1 # 1 second for force-kill
            fi
            
            while ( ps -p "$pid" -o comm 2>/dev/null | grep "^$prog$" 1>/dev/null 2>/dev/null ) && [ $timeout -ge 1 ] ; do
                sleep 1
                timeout=$(($timeout - 1))
            done

            [ $timeout -lt 1 ] && kill -9 "$pid" 1>/dev/null 2>&1
            rm -f "$PID_FILE" "$LOCKFILE"
        else
            RETVAL=1
            log_failure_msg "$prog shutdown - pidfile is empty"
        fi
    else
        RETVAL=0
        log_success_msg "$prog shutdown - already stopped"
    fi
    return $RETVAL
}

status() {
    if [ -f "$PID_FILE" ]; then
        read pid < "$PID_FILE"
        if [ "$pid" != "" ]; then
            if ps -p "$pid" > /dev/null; then
                echo "$1 (pid $pid) is running..."
                return 0
            fi
            echo "$1 stopped but pid file exists"
            return 1
        fi
    fi
    if [ -f $LOCKFILE ]; then
        echo "$1 stopped but lockfile exists"
        return 2
    fi
    echo "$1 is stopped"
    return 3
}

restart() {
    stop
    start
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $prog
        ;;
    restart | force-reload)
        restart
        ;;
    reload)
        ;;
    condrestart | try-restart)
        [ -f $LOCKFILE ] && restart || :
        ;;
    force-kill)
        stop 1
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart|force-reload|reload|condrestart|try-restart|force-kill}"
        exit 1
        ;;
esac

exit $?