#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: gfsd
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: FreeBSD shutdown

#
# Add the following lines to /etc/rc.conf to enable gfsd:
# @config_gfarm_gfsd_rc_var@_enable (bool):      Set to "NO" by default.
# gfsd_flags (str):        Set to "" by default.
#                             Extra flags passed to start command
#
if [ -f /usr/local/etc/rc.subr ]; then
	. /usr/local/etc/rc.subr
elif [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
else
	echo "$0: abort: rc.subr not found"
	exit 2
fi

CONF="@config_gfarm_gfarm_config@"
OPTIONS="@config_gfarm_gfsd_option@"

name="gfsd"
FILE="@config_gfarm_gfsd_rc_name@" # usually "gfsd", or maybe "gfsd-IP_ADDRESS"
rcvar=`set_rcvar @config_gfarm_gfsd_rc_var@`

command="@config_gfarm_prefix@/sbin/${name}"
pidfile="@config_gfarm_gfsd_pid_file@"
required_files="$CONF"
# add more flags through ${${name}_flags}
command_args="-P $pidfile $OPTIONS"
globus_location="@config_gfarm_globus_location@"

# gfsd service certificate
X509_USER_CERT=/etc/grid-security/@config_gfarm_gfsd_rc_name@/gfsdcert.pem
X509_USER_KEY=/etc/grid-security/@config_gfarm_gfsd_rc_name@/gfsdkey.pem
export X509_USER_CERT X509_USER_KEY

[ -z "$@config_gfarm_gfsd_rc_var@_enable" ]       && @config_gfarm_gfsd_rc_var@_enable="NO"
[ -z "$@config_gfarm_gfsd_rc_var@_flags" ]        && @config_gfarm_gfsd_rc_var@_flags=""

load_rc_config ${FILE}

if [ -z "${GLOBUS_LOCATION-}" ] && [ -n "$globus_location" ] &&
   [ -f "$globus_location/etc/globus-user-env.sh" ]
then
	GLOBUS_LOCATION="$globus_location"
	export GLOBUS_LOCATION
	. "$GLOBUS_LOCATION/etc/globus-user-env.sh"
fi

run_rc_command "$1"
