#!/bin/bash
#
# globus-gridftp-server	- Globus GridFTP Server
# chkconfig: 235 20 80
# description: The Globus GridFTP server is a process which \
#              implements the GridFTP protocol for secure, high-performance \
#              file transfer.
#

### BEGIN INIT INFO
# Provides:          globus-gridftp-server
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 5
# Default-Stop:      0 1 4 6
# Short-Description: Globus GridFTP Server
# Description: The Globus GridFTP server is a process which implements the
# GridFTP protocol for secure, high-performance file transfer.
### END INIT INFO

if [ -n "$GLOBUS_LOCATION" ]; then
    prefix="$GLOBUS_LOCATION"
else
    prefix="/usr"
fi
exec_prefix="/usr"
sbindir="/usr/sbin"
bindir="/usr/bin"
includedir="/usr/include/globus"
datarootdir="${prefix}/share"
datadir="/usr/share"
libexecdir="/usr/share/globus"
sysconfdir="/etc"
sharedstatedir="/var/lib"
localstatedir="/var"

lsb=""
if [ -f /lib/lsb/init-functions ]; then
    lsb=_lsb
    lsb_ok=1
    . /lib/lsb/init-functions
    if [ -f /etc/redhat-release ] && lsb_release -v | grep -q 'core-[123]'; then
        unset lsb_ok
    fi

fi

rc=0
conf=${sysconfdir}/gridftp.conf
confdir=${sysconfdir}/gridftp.d
pidfile=${localstatedir}/run/globus-gridftp-server.pid
gridftpd=${sbindir}/globus-gridftp-server

start()
{
    printf "Starting globus-gridftp-server: "
    rc=0
    if [ ! -f $conf ]; then
      cp $conf.default $conf;
    fi

    if [ -f "$pidfile" ]; then
        read pid < "$pidfile"

        if ! kill -0 $pid; then
            rm "$pidfile"
        fi
    fi

    if [ ! -d $confdir ]; then
        mkdir $confdir;
    fi

    if [ ! -f "$pidfile" ]; then
        $gridftpd -S -c $conf -C $confdir -pidfile "${pidfile}"
        rc=$?
    fi
     
    [ $rc -eq 0 ] && echo "OK" || echo "FAILED";
    return $rc
}	

start_lsb()
{
    rc=0
    if [ ! -f $conf ]; then
      cp $conf.default $conf;
    fi
    if [ ! -d $confdir ]; then
        mkdir $confdir;
    fi

    start_daemon ${lsb_ok:+-p "$pidfile"} "$gridftpd" -S -c $conf \
            -C $confdir -pidfile "${pidfile}"
    rc=$?
     
    if [ $rc -eq 0 ]; then
        log_success_msg "Started GridFTP Server"
    else
        log_failure_msg "Failed to start GridFTP Server"
    fi

    return $rc
}

stop()
{
    printf "Stopping globus-gridftp-server: "
    if [ -f "$pidfile" ]; then
        read pid < "$pidfile" 2> /dev/null

        if [ "$pid" != "" -a "$pid" -gt 0 ]; then
            if kill -0 "$pid" 2> /dev/null; then
                kill -INT "$pid" 2>/dev/null
                rc=$?
                sleep 2
                kill -0 "$pid" 2> /dev/null && kill -KILL "$pid"
            fi
        fi

        rm -f "$pidfile"
    fi

    [ $rc -eq 0 ] && echo "OK" || echo "FAILED";
    return $rc
}

stop_lsb()
{
    killproc ${lsb_ok:+-p "$pidfile"} "$gridftpd"
    rc=$?
    if [ $rc -eq 0 ]; then
        log_success_msg "Stopped GridFTP Server"
    else
        log_failure_msg "Failed to stopped GridFTP Server"
    fi
    return $rc
}

restart()
{
    stop
    start
}

restart_lsb()
{
    stop_lsb
    start_lsb
}

reload()
{
    printf "Reloading GridFTP configuration: "
    read pid < "$pidfile" 2> /dev/null

    if [ "$pid" != "" -a "$pid" -gt 0 ]; then
        kill -HUP "$pid" 2>/dev/null
        rc=$?
    fi

    [ $rc -eq 0 ] && echo "OK" || echo "FAILED";
    return $rc
}

reload_lsb()
{
    killproc ${lsb_ok:+-p "$pidfile"} "$gridftpd" -HUP
    rc=$?
    if [ $rc -eq 0 ]; then
        log_success_msg "Reloaded GridFTP Server Configuration"
    else
        log_failure_msg "Failed to reload GridFTP Server Configuration"
    fi
    return $rc
}

status()
{
    if [ -f $pidfile ]; then
        read pid < "$pidfile" 2> /dev/null

        if [ "$pid" != "" -a "$pid" -gt 0 ]; then
            if kill -0 "$pid" 2> /dev/null; then
                echo "GridFTP server is running (pid=$pid)"
                rc=0
            else
                echo "Stale PID file $pidfile"
                rc=1
            fi
        else
            echo "Invalid PID file $pidfile"
            rc=4
        fi
    else
        echo "GridFTP server is not running"
        rc=3
    fi
    return $rc
}

status_lsb()
{
    pid="$(pidofproc ${lsb_ok:+-p "$pidfile"} "$gridftpd")"
    rc=$?
    if [ -z "$lsb_ok" ] && [ -n "$pid" ] && [ "$pid" -eq $$ ]; then
        # SL5 lsb pidofproc returns this process's pid, which is
        # obviously not correct.
        rc=4
    fi
    case $rc in
        0)
            echo "GridFTP Server Running (pid=$pid)"
            ;;
        1)
            echo "Stale PID file for GridFTP Server"
            ;;
        2|3)
            if [ -f "${lockfile}" ]; then
                echo "Stale lock file for GridFTP Server"
            else
                echo "GridFTP Server is not running"
            fi
            ;;
        4)
            if [ -n "$lsb_ok" -a ! -f "${pidfile}" ]; then
                echo "$gridftpd is not running"
                rc=3
            fi
            ;;
        *)
            echo "Unknown status for GridFTP Server"
            ;;
    esac

    return $rc
}


case "$1" in
    start)
	start${lsb}
	;;
    stop)
	stop${lsb}
	;;
    restart)
    	restart${lsb}
        ;;
    reload)
        reload${lsb}
        ;;
    force-reload)
        restart${lsb}
        ;;
    status)
	status${lsb}
        ;;
    condrestart|try-restart)
        status${lsb} || exit 0
        restart${lsb}
        ;;
    *)
	echo "Usage: $0 {start|stop|restart|reload|force-reload|status|condrestart|try-restart}"
	exit 1
	;;
esac
exit $rc

