#!/bin/sh
# Startup script for spinfs
#
# chkconfig: 35 15 10
# description: Load the Spinnaker Networks filessystem software.

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

# Source networking configuration.
. /etc/sysconfig/network

# Source RDB configuration.
if [ -f /etc/sysconfig/rdb ]; then
	. /etc/sysconfig/rdb
fi

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x /sbin/hamgr ] || exit 0
[ -x /sbin/bip_dblade ] || exit 0
[ -x /sbin/bip_stream ] || exit 0
[ -x /sbin/smbconfig ] || exit 0
[ -x /sbin/authenticate ] || exit 0
[ -x /sbin/spinauthd ] || exit 0
[ -f /etc/sysconfig/portmap.cfg ] || (echo "/etc/sysconfig/portmap.cfg file not found\n"; exit 0)

# Check for the RDB export variable.
if [ -z $RDB_DIR ]; then
	echo "RDB_DIR Director not defined, using the default: /etc/sysconfig\n"
	RDB_DIR=/etc/sysconfig
fi
export RDB_DIR
export CIFS_CONFIG_PAGEFILE=/etc/sysconfig/cifs_config
export HA_VIFALIAS_CONFIG=/etc/sysconfig/vifalias
RETVAL=0
USEBIP=1

SPIN_CFPGA_MODULE=spin_cfpga
SPIN_STREAM_MODULE=spin_stream
SPIN_COMMON_MODULE=spincommon
SPIN_FCD_MODULE=spinfcd
SPIN_DBLADE_MODULE=spind
SPIN_NBLADE_MODULE=spinstream
SPIN_PROTO_MODULE=spinproto
SPIN_IF_MODULE=spinifproc
SPIN_PORT=7700
SPIN_GROUP='root'
SPIN_MODE='664'

make_node() {
	module=$1

	major=`cat /proc/devices | awk "\\$2==\"$module\" {print \\$1}"`
	sim_major=`cat /proc/devices | awk "\\$2==\"sim_$module\" {print \\$1}"`

        rm -f /dev/${module}
	mknod /dev/${module} c $major 0
	chgrp ${SPIN_GROUP} /dev/${module}
	chmod ${SPIN_MODE}  /dev/${module}

	if [ $sim_major ]; then
		rm -f /dev/${module}_sim
		mknod /dev/${module}_sim c $sim_major 0
		chgrp ${SPIN_GROUP} /dev/${module}_sim
		chmod ${SPIN_MODE}  /dev/${module}_sim
	fi
}

start_dblade() {
	if ! [ -e /proc/spindblade ]; then
        	action $"Loading the Dblade modules: " modprobe ${SPIN_DBLADE_MODULE} useBip=$USEBIP
		mqtest 103 0
	fi
	pidofproc bip_dblade > /dev/null
	if [ $? == 1 ]; then 
                if [ "$USEBIP" == 1 ]
                then
                    echo "starting bip_dblade"
		    bip_dblade /proc/spindblade
                else
                    echo "did not start bip_dblade"
                fi
	fi
}

start_nblade() {
	if ! [ -e /proc/spinstats ]; then
		action $"Loading the Nblade modules: " modprobe ${SPIN_IF_MODULE}
		modprobe ${SPIN_CFPGA_MODULE}
		modprobe ${SPIN_STREAM_MODULE}
		make_node ${SPIN_CFPGA_MODULE}
		make_node ${SPIN_STREAM_MODULE}
	fi

	pidofproc hamgr > /dev/null
	if [ $? == 1 ]; then
		HOSTNAME=`hostname`
		rm -f $HA_VIFALIAS_CONFIG
		export LD_PRELOAD=/usr/spinmgmt/lib/libbindresvport.so
		hamgr -pciportmap /etc/sysconfig/portmap.cfg -node $HOSTNAME -dbladeStart 0x10F
		export -n LD_PRELOAD
	fi

	pidofproc bip_stream > /dev/null
	if [ $? == 1 ]; then
		bip_stream /dev/spin_stream
	fi

	pidofproc smbconfig > /dev/null
	if [ $? == 1 ]; then
		smbconfig & 
	fi

	pidofproc authenticate > /dev/null
	if [ $? == 1 ]; then
		export LD_PRELOAD=/usr/spinmgmt/lib/libbindresvport.so
		authenticate &
		export -n LD_PRELOAD
	fi

	pidofproc spinauthd > /dev/null
	if [ $? == 1 ]; then
		export LD_PRELOAD=/usr/spinmgmt/lib/libbindresvport.so
		spinauthd -d &
		export -n LD_PRELOAD
	fi
}

stop_nblade() {
        echo -n $"Shutting SpinAuth daemon: "
        killproc spinauthd
        echo
        echo -n $"Shutting Authenticate: "
        killproc authenticate
        echo
        echo -n $"Shutting SMB config: "
        killproc smbconfig
        echo
        echo -n $"Shutting down the Stream BIP: "
        killproc bip_stream
	echo
	echo -n $"Shutting HA manager: "
	killproc hamgr
        echo
	action $"Unloading the ${SPIN_IF_MODULE} module: " rmmod -s ${SPIN_IF_MODULE}
        action $"Unloading the ${SPIN_PROTO_MODULE} module: " rmmod -s ${SPIN_PROTO_MODULE}
        action $"Unloading the ${SPIN_NBLADE_MODULE} module: " rmmod -s ${SPIN_NBLADE_MODULE}
        action $"Unloading the ${SPIN_STREAM_MODULE} module: " rmmod -s ${SPIN_STREAM_MODULE}
        action $"Unloading the ${SPIN_CFPGA_MODULE} module: " rmmod -s ${SPIN_CFPGA_MODULE}
}

# set the current working directory to /core
cd /core

# See how we were called.

case "$1" in
  start)
	[ -x /etc/rc.d/init.d/random ] && /etc/rc.d/init.d/random start
	[ -x /etc/rc.d/init.d/sshd ] && /etc/rc.d/init.d/sshd start

	if ! [ -e /proc/fcd ]; then
		action $"Loading the ${SPIN_FCD_MODULE} module: " modprobe ${SPIN_FCD_MODULE}
	fi

	if ! [ -f /etc/nospin ]; then
		climit=`ulimit -Sc`
		ulimit -Sc 1000000
		start_dblade
		start_nblade
		pidofproc vod > /dev/null
		if [ $? == 1 ]; then
			vod -h120
		fi
		ulimit -Sc $climit
		touch /var/lock/subsys/spinfs
	fi

	(initlog -c "/sbin/spinlocate.pl -save"; echo -e "\r") &
	;;
  starthamgr)
        HOSTNAME=`hostname`
        STARTVAL=$2
	if [ "$#" -eq 1 ]; then
	    STARTVAL=0x10f
	fi
	export LD_PRELOAD=/usr/spinmgmt/lib/libbindresvport.so
	hamgr -pciportmap /etc/sysconfig/portmap.cfg -node $HOSTNAME -dbladeStart $STARTVAL
	export -n LD_PRELOAD
	;;
  stophamgr)
        hamgr -stop
	;;
  stop)
	[ -x /etc/rc.d/init.d/netfs ] && /etc/rc.d/init.d/netfs stop
	rm -f /var/lock/subsys/spinfs
	killproc vod
	lpRpc localhost lpLayerShutdown
	[ $? != 0 ] && lpSync localhost
	mqtest 109
	hamgr -stop
	sync
	;;
  status)
	status bip_dblade
	status bip_stream
	[ -f /proc/spinstats ] && cat /proc/spinstats
	hamgr -status
	/sbin/spinlocate.pl
	;;
  *)
	echo "Usage: $0 {start|stop|status|starthamgr dbladeStartMode|stophamgr}"
	RETVAL=1
esac

exit $RETVAL
