#!/bin/sh
#
# chkconfig: - 39 35
# description: Starts and stops the iSCSI target
#
# pidfile: /var/run/ietd.pid
# config:  /etc/ietd.conf

PATH=/sbin:/bin:/usr/sbin:/usr/bin
OPTIONS=""

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

if [ -f /etc/sysconfig/iscsi-target ]; then
  . /etc/sysconfig/iscsi-target
fi

RETVAL=0

start()
{
	echo -n "Starting iSCSI Target: "
	modprobe -q crc32c
	modprobe iscsi_trgt
	daemon /usr/sbin/ietd $OPTIONS
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		echo_success
	else
		echo_failure
	fi
	echo
	return $RETVAL
}

stop()
{
	echo -n "Stopping iSCSI Target: "
	ietadm --op delete
	killall ietd
	modprobe -r iscsi_trgt
	RETVAL=$?
	if [ $RETVAL -eq 0 ]; then
		echo_success
	else
		echo_failure
	fi
	echo
	return $RETVAL
}

restart()
{
	stop
	sleep 1
	start
}

condrestart()
{
	PID=`pidofproc ietd`
	if [ $PID ]; then
        	restart
	fi
}

status()
{
	PID=`pidofproc ietd`
	if [ ! $PID ]; then
		echo "iSCSI Target stopped"
		exit 1
	else
		echo "iSCSI Target (pid $PID) is running..."
	fi
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
	restart
        ;;
  condrestart)
        condrestart
        ;;
  status)
        status
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        exit 1
esac

exit 0
