#!/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 get_full_target_name {
	local match=$1

	if [ -z "$match" ]; then 
		echo "Empty target name" >>$log
		return 1;
	fi

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

		#extract target_name from 'iqn.2014.06.com.vstorage:target_name'
		#regex return all characters after first ':'
		local target_name=$(echo "$target" | grep -Po '(?<=:).*')

		if [ "$target_name" == "$match" ]; then
			#echo full target name
			echo "$target"
			break;
		fi
	done

	return 0;
}

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

		#extract target_name from 'iqn.2014.06.com.vstorage:target_name'
		#regex return all characters after first ':'
		local target_name=$(echo "$target" | grep -Po '(?<=:).*')
		
		echo "Process target $target" >>$log
		pcs_iscsi_is_registered_local $target $host_id >/dev/null 2>&1
		rc=$?
		if [ $rc -eq 0 ] ; then
			$action $ISCSI_ROOT/$target $target_name
			[ $? -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"
	local full_name

	if [ -z "$target" ]; then
		echo "Target not specified" >>$log
	fi

	full_name=$(get_full_target_name $target)
	if [ -z $full_name ] ; then
		echo "Unable find target $target" >>$log
		return 1
	fi	

	pcs_iscsi_check_target $full_name

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

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

	return 0
}

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

	if [ -z "$target" ]; then
		echo "Target is not specified" >>$log
	fi

	full_name=$(get_full_target_name $target)
	if [ -z $full_name ] ; then
		echo "Unable find target $target" >>$log
		return 1
	fi	

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

	echo "Target $full_name was stopped succesfully" >>$log

	rm -f $lockfile

	return 0
}

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

	if [ ! -f "$path/control/.auto" ] ; then
		echo "Autostart disabled for target '$target', skip it" >>$log
		return 0
	fi
	
	# default tgtd
	case $SCSI_TGT in
		tcm)    pcs_iscsi_lock_exec iscsi_start_target ${target}
		        ;;
		*)      $SYSTEMD_TOOL start vstorage-iscsid@${target}.service
		        ;;
	esac
}

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

	# default tgtd
	case $SCSI_TGT in
		tcm)    pcs_iscsi_lock_exec iscsi_stop_target ${target}
		        ;;
		*)      $SYSTEMD_TOOL stop vstorage-iscsid@${target}.service
		        ;;
	esac
}

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
  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 $?
	;;
  *)
	echo $"Usage: $0 {start|stop} target"
	echo $"          {start-all|stop-all}"
	exit 2
esac
