#!/bin/sh

EXEC_DIR="/usr/libexec/pstorage-iscsi"

function print_usage {
        exec 1>&2

	echo "Pstorage iSCSI targets control tool."
        echo "Usage:"
        echo " pstorage-iscsi command [command_options]"
	echo " "
        echo "Commands:"
	echo " create [create_options]        Create iSCSI target on shared storage."
	echo " delete [delete_options]        Delete existing iSCSI target."
	echo " start  [options]               Start iSCSI target."
	echo " stop   [options]               Stop iSCSI target."
	echo " register [register_options]    Register iSCSI target on current host."
	echo " list [list_options]            List existing iSCSI targets."
	echo " lun-add [options]              Add new LUN to iSCSI target."
	echo " lun-del [options]              Delete existing LUN."
	echo " help command                   Print usage for specified command."
        echo " "
        exit 1
}

CMD="${1}"
shift
ARGS="${@}"
if [ "${CMD}" = "help" ] ; then
	CMD="${1}"
	ARGS="--help"
fi

case "${CMD}" in
  create)
	${EXEC_DIR}/pstorage-iscsi-make ${ARGS}
	RC=${?}
        ;;
  delete)
	${EXEC_DIR}/pstorage-iscsi-delete ${ARGS}
	RC=${?}
        ;;
  register)
        ${EXEC_DIR}/pstorage-iscsi-register ${ARGS}
	RC=${?}
        ;;
  start)
	${EXEC_DIR}/pstorage-iscsi-start ${ARGS}
	RC=${?}
	;;
  stop)
	${EXEC_DIR}/pstorage-iscsi-stop ${ARGS}
	RC=${?}
	;;
  list)
	${EXEC_DIR}/pstorage-iscsi-list ${ARGS}
	RC=${?}
        ;;
  lun-add|lun-del)
	${EXEC_DIR}/pstorage-iscsi-lunctl ${CMD} ${ARGS}
	;;
  *)
	if [ -n "${CMD}" ] ; then
		echo "Unknown command $CMD" 1>&2
	else
		echo "Command must be specified." 1>&2
	fi
	print_usage
esac

exit $RC

