#!/bin/sh
#
# description: pvfs2-server is the server component of PVFS2
#
# chkconfig: 345 35 55

# Source function library.
. /etc/rc.d/init.d/functions

# these should not be changed here, look below for the file to
# change
PVFS2_FS_CONF=
PVFS2_SERVER_CONF=
# override this if your server binary resides elsewhere
PVFS2SERVER=/usr/sbin/pvfs2-server
# override this if you want servers to automatically pick a conf file,
#   but you just need to specify what directory they are in
PVFS2_CONF_PATH=/etc/PVFS2

# change above defaults in this file
if [ -f /etc/sysconfig/pvfs2-server ];then
	. /etc/sysconfig/pvfs2-server
fi

# the server will record its PID in this file
PVFS2_PIDFILE=/var/run/pvfs2.pid

# verify presence of server binary
if ! [ -x ${PVFS2SERVER} ]; then
	echo "Error: could not find executable ${PVFS2SERVER}"
	exit 1
fi

# look for fs conf
if test "x$PVFS2_FS_CONF" = x
then
	PVFS2_FS_CONF=${PVFS2_CONF_PATH}/pvfs2-fs.conf
fi
if ! [ -r ${PVFS2_FS_CONF} ]; then
	echo "Error: could not read ${PVFS2_FS_CONF}"
	exit 1
fi

# look for server conf
if test "x$PVFS2_SERVER_CONF" = x
then
	#determine hostname
	if test "x$HOSTNAME" = x
	then
		HOSTNAME=`hostname`
	fi
	THIS_HOSTNAME=`echo $HOSTNAME | cut -d '.' -f 1`

	if test "x$THIS_HOSTNAME" = xlocalhost
	then
		echo "Warning: detected hostname as localhost, may confuse PVFS2 startup"
	fi
	
	PVFS2_SERVER_CONF=${PVFS2_CONF_PATH}/pvfs2-server.conf-${THIS_HOSTNAME}
fi
if ! [ -r ${PVFS2_SERVER_CONF} ]; then
	echo "Error: could not read ${PVFS2_SERVER_CONF}"
	exit 1
fi

# See how we were called.
case "$1" in
  start)
	echo -n "Starting PVFS2 server: "
	daemon ${PVFS2SERVER} --pidfile ${PVFS2_PIDFILE} ${PVFS2_FS_CONF} ${PVFS2_SERVER_CONF}
	echo
	touch /var/lock/subsys/pvfs2-server
	;;
  stop)
	echo -n "Stopping PVFS2 server: "
	kill `cat /var/run/pvfs2.pid`
	echo
	rm -f /var/lock/subsys/pvfs2-server
	;;
  status)
        status pvfs2-server
	;;
  restart)
	$0 stop
	# give server time to die cleanly
	sleep 2
	$0 start
	;;
  condrestart)
	if [ -e /var/lock/subsys/pvfs2-server ]; then
		$0 stop
		# give server time to die cleanly
		sleep 2
		$0 start
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|condrestart}"
	exit 1
esac

exit 0

