#!/bin/bash

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

function print_usage {
        exec 1>&2

	echo "vstorage iSCSI targets control tool."
        echo "Usage:"
        echo " vstorage-iscsi command [command_options]"
	echo " "
        echo "Targets related commands:"
	echo " create     [options]           Create iSCSI target on shared storage."
	echo " delete     [options]           Delete existing iSCSI target."
	echo " start      [options]           Start iSCSI target."
	echo " stop       [options]           Stop iSCSI target."
	echo " register   [options]           Register the specified iSCSI target on current host."
	echo " unregister [options]           Unregister the specified iSCSI target."
	echo " set        [options]           Change target's parameters."
	echo " list       [options]           List existing iSCSI targets."
	echo " "
	echo "LUNs related commands:"
	echo " lun-add    [options]           Add new LUN to an iSCSI target."
	echo " lun-del    [options]           Delete existing LUN."
	echo " lun-grow   [options]           Grow size of existing LUN."
	echo " lun-set    [options]           Set attributes for existing LUN."
	echo " "
	echo "Snapshot related commands:"
	echo " snapshot-create [options]      Create snapshot for specified LUN."
	echo " snapshot-delete [options]      Delete specified snapshot."
	echo " snapshot-list   [options]      List existing snapshot for specified LUN."
	echo " snapshot-switch [options]      Switch to the specified snapshot."
	echo " snapshot-info   [options]      Show information about specified snapshot."
	echo " "
	echo "CHAP account commands:"
	echo " account-create [options]       Create new CHAP account."
	echo " account-delete [options]       Delete existing account."
	echo " account-set    [options]       Change existing account."
	echo " account-list   [options]       List existing CHAP accounts."
	echo " "
	echo "FCoE related commands:"
	echo " fcoe-create  [options]         Create a FCoE HBA"
	echo " fcoe-destroy [options]         Destroy a FCoE HBA"
	echo " fcoe-list    [options]         Print created FCoE HBA"
	echo " "
	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}/cmd-iscsi-make ${ARGS}
	RC=${?}
        ;;
  delete)
	${EXEC_DIR}/cmd-iscsi-delete ${ARGS}
	RC=${?}
        ;;
  register)
        ${EXEC_DIR}/cmd-iscsi-register register ${ARGS}
	RC=${?}
        ;;
  unregister)
        ${EXEC_DIR}/cmd-iscsi-register unregister ${ARGS}
	RC=${?}
        ;;
  start)
	${EXEC_DIR}/cmd-iscsi-start ${ARGS}
	RC=${?}
	;;
  stop)
	${EXEC_DIR}/cmd-iscsi-stop ${ARGS}
	RC=${?}
	;;
  list)
	${EXEC_DIR}/cmd-iscsi-list ${ARGS}
	RC=${?}
        ;;
  set)
	${EXEC_DIR}/cmd-iscsi-set set ${ARGS}
	RC=${?}
	;;
  lun-add|lun-del)
	${EXEC_DIR}/cmd-iscsi-lunctl ${CMD} ${ARGS}
	;;
  lun-grow)
	${EXEC_DIR}/cmd-iscsi-lun-grow ${ARGS}
	;;
  lun-set)
	${EXEC_DIR}/cmd-iscsi-lun-set ${ARGS}
	;;
  account-create|account-list|account-delete|account-set)
	${EXEC_DIR}/cmd-iscsi-account ${CMD} ${ARGS}
	;;
  snapshot-create|snapshot-list|snapshot-delete|snapshot-switch|snapshot-info)
	${EXEC_DIR}/cmd-iscsi-snapshot ${CMD} ${ARGS}
	;;
  fcoe-create|fcoe-destroy|fcoe-list)
	${EXEC_DIR}/cmd-fcoe-ctl ${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

