#!/bin/bash
#
# globus-gridftp-server - Globus GridFTP Server
# chkconfig: - 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: 3 5
# Default-Stop:      0 1 2 3 4 5 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

# source function library
. /etc/rc.status

sbindir=/usr/sbin
sysconfdir=/etc

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

# Include defaults if available
if [ -r /etc/sysconfig/globus-gridftp-sshftp ] ; then
	. /etc/sysconfig/globus-gridftp-sshftp
fi

# First reset status of this service
rc_reset

case "$1" in
	start)
		echo -n "Enabling sshftp access to globus-gridftp-server: "
		if [ ! -f $defaultconf ]; then	
		  ${sbindir}/globus-gridftp-server-enable-sshftp -out $defaultconf
		  rc=$?
		fi
		if [ ! -f $conf ]; then
			touch $conf
		fi
		if [ ! -d $confdir ]; then
			mkdir $confdir;
		fi

		if [ $rc -eq 0 -a ! -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
		if [ $rc -eq 0 ]; then
			rc_status -v
		else
			rc_failed 1
			rc_status -v1
			rc_exit
		fi
		;;

	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
		if [ $rc -eq 0 ]; then
			rc_status -v
		else
			rc_failed 1
			rc_status -v1
			rc_exit
		fi
		;;

	status) 
		echo -n "Checking for sshftp access to globus-gridftp-server:"
		if [ -f $enabledconf ]; then
		  	echo -n ""
		  	rc_status -v
		  	exit 0
		else
		  	rc_failed 3
		  	rc_status -v
		  	rc_exit
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|status}"
		exit 1
		;;
esac
rc_exit
