#! /bin/bash
#
# netbsd-iscsi: init script for iscsi-target daemon in netbsd-iscsi.
#
# Author: Satoru SATOH <ssato@redhat.com>
# Crippled by: Lubomir Rintel <lkundrak@v3.sk>
# 
# chkconfig: - 80 30
# description: NetBSD iSCSI (iscsi-target) is a iSCSI target program.
# processname: iscsi-target
# config: /etc/iscsi/initiatorname.iscsi
# config: /etc/iscsi/iscsid.conf
# config: /etc/iscsi/targets
# pidfile: /var/run/iscsi-target.pid
#
### BEGIN INIT INFO
# Provides: netbsd-iscsi
# Required-Start: network
# Required-Stop:
# Default-Start:
# Default-Stop: 0 1 2 6
# Short-Description: NetBSD iSCSI target
# Description: NetBSD iSCSI (iscsi-target) is a iSCSI target program.
### END INIT INFO

SERVICE=netbsd-iscsi
PROCESS=iscsi-target
DAEMON=/usr/sbin/iscsi-target

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

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

# Source service configuration.
. /etc/sysconfig/$SERVICE

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

[ -x $DAEMON ] || exit 1

start() {
        echo -n $"Starting $SERVICE: "
	set -o pipefail
        daemon $DAEMON $NETBSD_ISCSI_OPTS >/dev/null
	# We suppressed nonessential output, so we need to reprint status msg
	[ "$?" -eq 0 ] && success || failure
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch /var/lock/subsys/$SERVICE
        return $RETVAL
}

stop() {
        echo -n $"Stopping $SERVICE:"
        killproc $PROCESS
	RETVAL=$?
        echo
        if [ $RETVAL -eq 0 ]; then
        	rm -f /var/lock/subsys/$SERVICE
        	rm -f /var/run/$PROCESS.pid
        fi
        return $RETVAL
}

restart() {
	stop && start
}

RETVAL=0
case "$1" in
  start)
	start
	RETVAL=$?
	;;
  stop)
	stop
	RETVAL=$?
	;;
  restart)
	restart
	RETVAL=$?
	;;
  reload)
	echo "$0 does not support configuration reloading, you have to restart it."
	RETVAL=1
	;;
  status)
	status $PROCESS
	RETVAL=$?
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|reload|status}"
	RETVAL=1
esac

exit $RETVAL
