#!/bin/bash
#
# Init file for the NorduGrid/ARC Information Endpoint Registration
#
# chkconfig: - 75 45
# description: NorduGrid/ARC Information Endpoint Registration
#
# config: /etc/sysconfig/nordugrid
# config: /etc/sysconfig/nordugrid-arc-infosys
# config: /etc/arc.conf
#
######################################################################

### BEGIN INIT INFO
# Provides:          nordugrid-arc-inforeg
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: NorduGrid/ARC Information Endpoint Registration
# Description:       Init file for the NorduGrid/ARC Information Endpoint
#                    Registration
### END INIT INFO

# Helper functions
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=nordugrid-arc-inforeg
RETVAL=0
RUN=yes

# 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/$prog ]; then
    . /etc/sysconfig/$prog
elif [ -r /etc/default/$prog ]; then
    . /etc/default/$prog
fi

[ -n "$ARC_LOCATION ] && export ARC_LOCATION
[ -n "$ARC_CONFIG ]   && export ARC_CONFIG

if [ "x$RUN" != "xyes" ]; then
    echo "$prog disabled, please adjust the configuration to your needs "
    echo "and then set RUN to 'yes' in /etc/default/$prog to enable it."
    exit 0
fi

ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    echo "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi

# Source the config parsing routines
if [ -r "$ARC_LOCATION/share/arc/config_parser_compat.sh" ]; then
    . $ARC_LOCATION/share/arc/config_parser_compat.sh || exit $?
else
    echo "Could not find $ARC_LOCATION/share/arc/config_parser_compat.sh"
    exit 1
fi

ARC_CONFIG=${ARC_CONFIG:-/etc/arc.conf}
if [ ! -r "$ARC_CONFIG" ]; then
    echo "ARC configuration file ($ARC_CONFIG) not found"
    echo "If this file is in a non-standard place it can be set with the"
    echo "  ARC_CONFIG environment variable"
    exit 1
fi

# Read arc.conf
config_parse_file $ARC_CONFIG || exit $?

# Check for infosys-block
if ! config_match_section infosys; then
    echo "Missing infosys configuration block"
    exit 0
fi

config_hide_all
config_import_section common
config_import_section infosys

# Use this command to change user
if [ -x /sbin/runuser ]; then
    RUNUSER=runuser
else
    RUNUSER=su
fi

# If missing, we have a problem
USERSHELL=${USERSHELL:-"/bin/sh"}
if [ ! -x ${USERSHELL} ]; then
    log_failure_msg "Could not find /bin/sh"
    exit 1
fi

# Debian does not have /var/lock/subsys
if [ -d /var/lock/subsys ]; then
    lockfile=/var/lock/subsys/$prog
else
    lockfile=/var/lock/$prog
fi

registrationlog=${CONFIG_registrationlog:-/var/log/arc/inforegistration.log}
arc_runtime_config="/var/run/arc/infosys"
registrationconf=$arc_runtime_config/grid-info-resource-register.conf
pid_file="/var/run/nordugrid-arc-inforeg.pid"

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 "^sleep$" 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

    rm -f $registrationconf
    $ARC_LOCATION/share/arc/create-inforeg-config
    if grep -q ^regtype: $registrationconf ; then
        # We should sleep forever really...
        sleep 10000d >/dev/null 2>&1 &
        pid=$!
        echo $pid > $pid_file
        $ARC_LOCATION/share/arc/grid-info-soft-register \
            -log $registrationlog -f $registrationconf \
            -p $pid >/dev/null 2>&1 &
        if [ $? -eq 0 ]; then
            touch $lockfile
            log_success_msg
        else
            log_failure_msg
        fi
    fi
}

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

    RETVAL=0
    if [ -f "$pid_file" ]; then
        read pid < "$pid_file"
        if [ -n "${pid}" ]; then
            kill -15 ${pid} 2>/dev/null
            ps ${pid} >/dev/null 2>&1
            if [ $? -eq 0 ]; then
                sleep 2
                ps ${pid} >/dev/null 2>&1
                if [ $? -eq 0 ]; then
                    kill -9 ${update_pid} 2>/dev/null
                    sleep 2
                    ps ${pid} >/dev/null 2>&1
                    if [ $? -eq 0 ]; then
                        RETVAL=1
                    fi
                fi
    
            fi
        fi
        if [ ${RETVAL} -eq 0 ];  then
            rm -f "$pid_file" "$lockfile"
            log_success_msg
        else
            log_failure_msg "Could not kill $prog"
        fi
    else
        log_success_msg "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
}

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

exit $?
