#!/bin/bash
#
# globus-gridftp-sshftp	
#
# chkconfig: @CHKCONFIG_DEFAULT@ 20 80
# description: Controls sshftp access to the globus-gridftp-server.
### BEGIN INIT INFO
# Provides:          globus-gridftp-sshftp
# Required-Start:    $remote_fs $syslog $network
# Required-Stop:     $remote_fs $syslog
@DEFAULT_START_LINE@
@DEFAULT_STOP_LINE@
# Short-Description: Globus GridFTP SSHFTP
### END INIT INFO
#

if [ -n "$GLOBUS_LOCATION" ]; then
    prefix="$GLOBUS_LOCATION"
else
    prefix="@prefix@"
fi
exec_prefix="@exec_prefix@"
sbindir="@sbindir@"
bindir="@bindir@"
includedir="@includedir@"
datarootdir="@datarootdir@"
datadir="@datadir@"
libexecdir="@libexecdir@"
sysconfdir="@sysconfdir@"
sharedstatedir="@sharedstatedir@"
localstatedir="@localstatedir@"

rc=0
defaultconf=${sysconfdir}/gridftp-sshftp
enabledconf=/etc/grid-security/sshftp

start() {
	echo -n "Enabling sshftp access to globus-gridftp-server: "

	if [ ! -f $defaultconf ]; then	
          ${sbindir}/globus-gridftp-server-enable-sshftp -out $defaultconf
	  rc=$?
	fi

	[ $rc -ne 0 ] && return 1

        if [ ! -d /etc/grid-security ] ; then
            mkdir /etc/grid-security
            rc=$?
        fi
	if [ $rc -eq 0 -a ! -f $enabledconf ]; then  
	  ln -s $defaultconf $enabledconf
          rc=$?
	fi

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

stop() {
	echo -n "Disabling sshftp access to globus-gridftp-server: "

	if [ -L $enabledconf ]; then
	  rm $enabledconf
          rc=$?
	elif [ -f $enabledconf ]; then
	  mv $enabledconf ${enabledconf}.save
	  rc=$?
	fi
	
	[ $rc -eq 0 ] && echo "OK" || echo "FAILED";
	return $rc
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	if [ -f $enabledconf ]; then
          echo "sshftp access to globus-gridftp-server is enabled."
          exit 0
        else
	  echo "sshftp access to globus-gridftp-server is disabled."
          exit 3
	fi
	;;
    restart|reload)
    	stop
	start
	;;
    reconfigure)
	stop
	${sbindir}/globus-gridftp-server-enable-sshftp -force -out $defaultconf
	start
	;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload|reconfigure}"
	exit 1
	;;
esac
exit $rc

