#!/bin/sh
#
# $Id: arla.init.in,v 1.2.4.1 1999/08/14 02:57:34 lha Exp $
#
# A simple SYSV startup script to start/stop arla for Linux
# 
# You must have a path that includes, insmod, mknod, 
# chmod, mount, mkdir and arlad.
#

PATH="/sbin:/usr/sbin:/usr/bin:/bin:%bindir%"
ARLABINDIR=%bindir%
CACHEDIR=%ARLACACHEDIR%
export PATH

run_program() {
    if $*; then
        :
    else
	echo "failed"
	exit 1
    fi
}

if [ ! -f $ARLABINDIR/xfs.o -o ! -x $ARLABINDIR/arlad ]; then
    exit 1
fi

case "$1" in
    start)
	# Check if there is a stale pid file
	if [ -f /var/run/arlad.pid ]; then
	    if kill -0 `cat /var/run/arlad.pid` 2> /dev/null; then
		echo "arlad already running"
		exit 1
	    else
		# Pid file was stale
		rm -f /var/run/arlad.pid
	    fi
	fi
	echo -n "Loading xfs kernel module: "
	run_program insmod $ARLABINDIR/xfs.o
	sleep 1
	echo "xfs.o"
	if [ ! -c /dev/xfs0 ] ; then
	    echo Creating device /dev/xfs0
	    rm -f /dev/xfs0
	    mknod /dev/xfs0 c 103 0
	    chmod 600 /dev/xfs0
	fi
	if [ ! -e $CACHEDIR ] ; then
	    mkdir $CACHEDIR
	    chmod 700 $CACHEDIR
	    chown root $CACHEDIR
	fi
	echo "Starting arlad: "
	run_program $ARLABINDIR/arlad -z
	echo "arlad"
	if [ ! -d /afs ] ; then
	    mkdir /afs
	fi
	sleep 3
	echo -n "Mounting AFS filesystem: "
	run_program mount -t xfs arla /afs
	echo "done"
	;;
    stop)
	echo -n "Unmounting AFS filesystem... "
	run_program umount /afs
	echo "done"
	if [ -f /var/run/arlad.pid ]; then
	    ARLAD_PID=`cat /var/run/arlad.pid`;
	    echo -n "Killing arlad... "
	    run_program kill -TERM $ARLAD_PID
	    sleep 3
	    if kill -0 $ARLAD_PID 2>/dev/null; then
		echo "arlad didn't die"
		exit 1
	    fi
	    echo "done"
	else
	    echo "No /var/run/arlad.pid, arlad is not running"
	fi
	echo -n "Unloading xfs kernel module... "
	run_program rmmod xfs
	echo "done"
	;;
    restart)
	$0 stop
	$0 start
        ;;
    status)	
	if [ -f /var/run/arlad.pid ]; then
	    if kill -0 `cat /var/run/arlad.pid` 2>/dev/null; then
		echo "arlad is running"
		exit 0
	    fi
	fi
	;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
	;;
esac

