#!/bin/bash

[ -f /etc/sysconfig/network ] || exit 0

# Source function library.
source /usr/libexec/vstorage-iscsi/vstorage_functions

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

log=/var/log/vstorage/vstorage-iscsid.log
lockfile=/var/lock/subsys/vstorage-iscsid

pcs_iscsi_init_host >>$log 2>&1

function for_each_target {
	local action=$1
	local retval=0

	host_id=`get_host_id`
	[ $? -ne 0 ] && return 1

	ls $ISCSI_ROOT | grep "^iqn*" 2>/dev/null | while read target
	do
		[ ! -d  "$ISCSI_ROOT/$target/control" ] && continue

		echo "Process target $target" >>$log
		pcs_iscsi_is_registered_local $target $host_id >/dev/null 2>&1
		rc=$?
		if [ $rc -eq 0 ] ; then
			$action $target
			[ $? -ne 0 ] && retval=1
		else
			[ $rc -ne 1 ] && retval=$rc
		fi
	done

	return $retval
}

#executed by systemctl start with lock
function iscsi_start_target() {
	local target="$1"

	pcs_iscsi_check_target $target

	echo "Starting target $target" >>$log
	pcs_iscsi_up_target $target >>$log 2>&1
	if [ $? -ne 0 ] ; then
		echo "Unable start target $target" >>$log
		return 1
	fi

	echo "Target $target was started succesfully" >>$log
	
	touch $lockfile

	return 0
}

function iscsi_cleanup_target() {
       local target="$1"
       echo "cleanup target $target" >>$log 2>&1
       
       pcs_iscsi_cleanup_target $target >>$log 2>&1
       rc=$?
       if [ $rc -ne 0 ]; then
               echo "Failed to cleanup target $target" >>$log
       fi
       return $rc
}

#executed by systemctl stop with lock
function iscsi_stop_target() {
	local target="$1"
	local status=""

	pcs_iscsi_check_target $target

	status=`pcs_iscsi_get_status $target`
	if [ $? -eq 0 -a "$status" == "running" ] ; then
		echo "Stopping target $target" >>$log

		pcs_iscsi_down_target $target "" "force" >>$log 2>&1
		if [ $? -ne 0 ] ; then
			echo "Unable stop target $target" >>$log
			return 1
		fi

		echo "Target $target was stopped succesfully" >>$log
	fi

	rm -f $lockfile

	return 0
}

function iscsi_target_up() {
	local target="$1"

	if [ ! -f "$ISCSI_ROOT/$target/control/.auto" ] ; then
		echo "Autostart disabled for target '$target', skip it" >>$log
		return 0
	fi

	$SYSTEMD_TOOL start vstorage-iscsid@"${target}".service
}

function iscsi_target_down() {
	local target="$1"

	$SYSTEMD_TOOL stop vstorage-iscsid@"${target}".service
}

function iscsi_fence() {
	echo "Fencing host" >>$log
	pcs_iscsi_fence_host >>$log 2>&1
	if [ $? -ne 0 ]; then
		echo "Failed to fence host" >>$log
		return 1
	fi
	echo "Host is succesfully fenced" >>$log
	return 0
}

function iscsi_enable_target_addresses {
	local target="$1"

	echo "Unfencing target '$target'" >>$log

	local status=`pcs_iscsi_get_status $target`
	if [ "$status" != "running" ]; then
		echo "Target '$target' is not running, skip it" >>$log
		return 0
	fi

	pcs_iscsi_add_target_addresses $target >>$log 2>&1
	if [ $? -ne 0 ] ; then
		echo "Failed to add addresses of taget '$target'" >>$log
		return 1
	fi

	echo "Target '$target' was unfenced successfully" >>$log
	return 0
}

function iscsi_unfence {
	echo "Unfencing host" >>$log
	for_each_target iscsi_enable_target_addresses
	if [ $? -ne 0 ]; then
		echo "Failed to unfence host" >>$log
		return 1
	fi
	echo "Host is succesfully unfenced" >>$log
	return 0
}

CMD="${1}"
shift
ARGS="${@}"


if [ -z "$ISCSI_ROOT" ]; then
	echo "Unable to find directory with iSCSI targets, please check 'ISCSI_ROOT' in $ISCSI_ETC/config" >>$log
	exit 0
fi

case "${CMD}" in
  cleanup)
	#called by systemd services from vstorage-iscsid@.service
	pcs_iscsi_lock_exec iscsi_cleanup_target ${ARGS}
	exit $?
	;;
  start)
	#called by systemd services from vstorage-iscsid@.service
	pcs_iscsi_lock_exec iscsi_start_target ${ARGS}
	exit $?
	;;
  stop)
	#called by systemd services from vstorage-iscsid@.service
	pcs_iscsi_lock_exec iscsi_stop_target ${ARGS}
	exit $?
	;;
  start-all)
	#called by systemd services from vstorage-iscsid.service
	for_each_target iscsi_target_up
	exit $?
	;;
  stop-all)
	#called by systemd services from vstorage-iscsid.service
	for_each_target iscsi_target_down
	exit $?
	;;
  fence)
	iscsi_fence
	exit $?
	;;
  unfence)
	iscsi_unfence
	exit $?
	;;
  *)
	echo $"Usage: $0 {start|stop} target"
	echo $"          {start-all|stop-all}"
	exit 2
esac
