#!/bin/sh
#
# $Id: arla.init.in,v 1.10.2.1 2001/02/21 05:53:23 assar Exp $
#
# A simple SYSV startup script to start/stop arla for Linux
# 
# You must have a path that includes, insmod/modprobe, mknod, 
# chmod, mount, mkdir and arlad.
#
# * RedHat 
#    1. Copy the file to /etc/rc.d/init.d
#    2. Run linuxconf or
#       ``chkconfig --level 345 arla on''
# * SuSE
#    1. Copy the file to /sbin/init.d
#    2. Configure with YaST
# * Debian, Corel, and other, send in your stuff here.
#    
#
#
#
# Linux conf compat
# 
# description: Arla - a free AFS implementation
# chkconfig: 345 56 45
# processname: arlad
# config: /usr/arla/etc/arlad.conf
# pidfile: /var/run/arlad.pid
#

PATH="/sbin:/usr/sbin:/usr/bin:/bin"
ARLABINDIR=/usr/arla/libexec
MODULEDIR=/usr/arla/bin
CACHEDIR=/usr/arla/cache
export PATH

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

kill_arlad() {
    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
}


if [ ! -f $ARLABINDIR/arlad ]; then
    exit 1
fi

if [ -f /etc/SuSE-release -a -f /etc/rc.config ]; then
    . /etc/rc.config
    if [ X"$START_ARLA" != "XYES" ]; then
	exit 0
    fi
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
	# load xfs (if it's not already loaded)
	if ! lsmod | grep "^xfs " >/dev/null; then
	    echo -n "Loading xfs: "
	    if modprobe xfs ; then
		:
	    elif [ -f $MODULEDIR/xfs.o ]; then
		if ! insmod $MODULEDIR/xfs.o; then
		    echo "missing xfs.o"
		    exit 1
		fi
	    fi
	    sleep 1
	    echo done
	fi
	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 -n "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 /dev/xfs0 /afs
	echo "done"
	;;
    stop)
	kill_arlad
	echo -n "Unmounting AFS filesystem... "
	run_program umount /afs
	echo "done"
	if lsmod | grep -q ^xfs ; then
	    echo -n "Unloading xfs kernel module: "
	    run_program rmmod xfs
	    echo done
	else
	    echo "xfs not loaded"
	fi
	;;
    restart)
	kill_arlad
	echo -n "Starting arlad: "
	run_program $ARLABINDIR/arlad -z
	echo "done"
        ;;
    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 "arla is NOT running"
	;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
	;;
esac

