#! /bin/sh

. /etc/init.d/functions.sh
. /etc/conf.d/iod

NAME=iodserver
DESCRIPTION="I/O daemon"
DAEMON=/bin/$NAME
SOCKETFILE=/var/run/iod/iodsocket

start_iod () {
	local pid=

	if [ -e $SOCKETFILE ]; then
		! pid=$(pidof $NAME) || [ -z "$pid" ] || {
			information "$DESCRIPTION is already running"
			return 0
		}

		rm -f $SOCKETFILE
	fi

	start_daemon $DAEMON $IOD_OPTIONS || return 1

	if exists get_id; then
		if exists update_io_nbr; then
			if [ $(get_id cpu) -gt 1 ] &&
		   	   [ $(get_id blade) -eq 15 ]; then
				update_io_nbr
			fi
		fi
	fi
}

stop_iod () {
	stop_daemon $DAEMON
}

case "$1" in
	start)
		begin "Starting $DESCRIPTION"
		start_iod
		end $?
		;;
	stop)
		begin "Stopping $DESCRIPTION"
		stop_iod
		end $?
		;;
	restart)
		begin "Restarting $DESCRIPTION"
		stop_iod && start_iod
		end $?
		;;
	*)
		error "Usage: $0 start|stop|restart"
		;;
esac

exit 0
