#!/bin/bash
PSTORAGE_ISCSI_LIB=/usr/libexec/vstorage-iscsi/vstorage_functions
ROOT=""
IQN=""
ARGS="${@}"
CMD=""
FORCE=""

function print_usage {
	exec 1>&2
	if [ "$CMD" = "register" ] ; then
		echo "Register the specified iSCSI target on current host."
		echo "Usage:"
		echo " $CTL_TOOL register -t,--target IQN [--force]"
		echo "Options:"
		echo "  -t,--target IQN         IQN of target to register."
		echo "  --force                 Skip all validation checks."
	else
		echo "Unregister the specified iSCSI target."
		echo "Usage:"
		echo " $CTL_TOOL unregister -t,--target IQN"
		echo "Options:"
		echo "  -t,--target IQN         IQN of target to unregister."
	fi
		echo " "
	exit 1
}

function parse_args {

	if [ ${#} -eq 0 ]; then
		echo "Command not specified, expected register or unregister" 1>&2
		exit 1
	fi

	CMD="${1}"; shift
	if [ "$CMD" != "register" -a "$CMD" != "unregister" ] ; then
		echo "Unknown command '$CMD'" 1>&2
		exit 1
	fi

	while [ "${#}" -gt 0 ]; do
		case "${1}" in
			"--force")
				FORCE="yes"
				shift;
				;;
			"-r"|"--root")
				[ -z "${2}" ] && print_usage
				ROOT="${2}"
				shift
				shift
				;;
			"-t"|"--target")
				[ -z "${2}" ] && print_usage
				IQN="${2}"
				shift
				shift
				;;
			"-h"|"--help")
				print_usage
				;;
			*)
				echo "Unknown option '${1}'." 1>&2
				print_usage
				;;
		esac
	done

	if [ -z "$IQN" ] ; then
		echo "Target (-t,--target) must be specified." 1>&2
		print_usage
	fi
}

function do_unregister {
	local target="$1"
	pcs_iscsi_check_target $target
	if [ -f "$ISCSI_SHM/.running/$target" ] ; then
		echo "Can't unregister running target, please stop it first." 1>&2
		exit 1
	fi

	pcs_iscsi_unregister_target $target
	if [ $? -ne 0 ]; then
		echo "Failed to unregister target $target" 1>&2
		exit 2
	fi
	exit 0
}

function do_register {
	local target="$1"
	if [ ! -d  "$ISCSI_ROOT/$target" ] ; then
		echo "Unable find target $target in $ISCSI_ROOT" 1>&2
		exit 1
	fi

	if [ -z "$FORCE" ] ;  then
		pcs_iscsi_target_has_owner $target
		if [ $? -ne 0 ] ; then
			owner=`cat "$ISCSI_ROOT/$target/control/host" 2>/dev/null`
			rc0=$?
			host=`get_host_id`
			rc1=$?

			if [ $rc0 -eq 0 ] && [ $rc1 -eq 0 ] && [ "$owner" = "$host" ]; then
				exit 0
			fi

			echo "This target already registered on another host." 1>&2
			echo "Please unregister it first or pass --force option." 1>&2
			exit 2
		fi
	fi

	pcs_iscsi_register_target $target
	if [ $? -ne 0 ]; then
		echo "Failed to register target $target" 1>&2
		exit 2
	fi

	[ -n "$FORCE" ] && /usr/bin/vstorage revoke -R "$ISCSI_ROOT/$target" >/dev/null 2>&1
	exit 0
}

if [ ! -x $PSTORAGE_ISCSI_LIB ] ; then
	echo "Unable find executable $PSTORAGE_ISCSI_LIB" 1>&2
	exit 1
fi

source $PSTORAGE_ISCSI_LIB
[ -f $ISCSI_ETC/config ] && source $ISCSI_ETC/config

parse_args $ARGS

ISCSI_ROOT=`pcs_iscsi_get_root "${ROOT}"`
pcs_iscsi_check_root
if [ "$CMD" = "register" ] ; then
	pcs_iscsi_lock_exec do_register $IQN
else
	pcs_iscsi_lock_exec do_unregister $IQN
fi
exit $?

