#!/bin/bash

_RELEASE_VER="0.98.5.3"

# Do not modify
_CUSTOM_CONF_FILE=$1
_MONITOR_PROC_CNT=
_ISCSI_CON=
_FLAG_FORCE=0
_FLAG_QUIET=0

# The use of iscsiadm requires admin privilages
read_iscsi_con (){ _ISCSI_CON=$(iscsiadm -m session 2>&1 /dev/null | grep -v "No active sessions"*); }

load_conf_file (){
	_default_conf_file=$(echo /etc/smart-iscsi/$_CUSTOM_CONF_FILE.conf)

	if [ -e $_default_conf_file ]
	then
		source $_default_conf_file
	else
		if [ -e $_CUSTOM_CONF_FILE ]
		then
			source $_CUSTOM_CONF_FILE
		else
			echo "ERROR: Config file not specified!"
			exit 2;
		fi
	fi

	# Set variables once valid config file has been loaded
	_MONITOR_PROC_CNT=$(pgrep -f $MONITOR_PROC | wc -l)
}

show_help (){
	echo "Smart iSCSI Mounter v$_RELEASE_VER - Help"
	echo ""
	echo "				[EXECUTABLE] [CONFIG FILE] [OVERRIDE] [FUNCTION]"
	echo "Example:			smart-iscsi config-file -q -m"
	echo "Example using fixed path:	smart-iscsi ~/config-file.conf -q -m"
	echo ""
	echo "Config files:"
	echo "Place config files inside '/etc/smart-iscsi' and call them without the '.conf' extension."
	echo "Configs outside '/etc/smart-iscsi' must be called using their full path."
	echo ""
	echo "Sample config file (/etc/smart-iscsi/config-file.conf):"
	echo "DISK_UUID=\"ABCDEFGHIJK\"				# UUID of mounted disk."
	echo "IQN=\"iqn.2000-01.com.synology:NAS.MyPool\"	# IQN of iSCSI mount."
	echo "PORTAL_IP=\"192.168.0.1\" 			# IP of IQN portal."
	echo "MOUNT_DIR=\"/home/username/VMs\" 			# Mount location."
	echo "MONITOR_PROC=\"vmware\" 				# Process name to block un-mount process, requires below count to be set."
	echo "MONITOR_PROC_CNT=7 				# Processes above this number will counter un-mount."
	echo ""
	echo "Overrides:"
	echo "'-q (--quiet)'"
	echo "'-f (--force)'"
	echo ""
	echo "Functions:"
	echo "'-m (--mount)'"
	echo "'-u (--umount)'"
	echo "'-s (--status)'"
	echo "'-r (--release)'"
	echo "'-h (--help)'"
	echo ""
}

report_status (){
	if [ ${_FLAG_QUIET} == 0 ]
	then
		echo "[Showing status of mount-point]"
		echo "INFO: Process '${MONITOR_PROC}' has ${_MONITOR_PROC_CNT} instances running."

		if [ $(ls $MOUNT_DIR/. | wc -l) -ge 1 ]
		then
			echo ""
			echo "Directory is mounted at:" $MOUNT_DIR
		else
			echo ""
			echo "Directory is un-mounted from:" $MOUNT_DIR
		fi

		read_iscsi_con
		echo ""
		echo "There is/are currently '$(${_ISCSI_CON} 2>&1 | wc -l)' iSCSI connection(s) open:"
		echo "${_ISCSI_CON}"
	fi
}

check_proc_status (){
	if [ ${_MONITOR_PROC_CNT} -gt $MONITOR_PROC_CNT ]
	then
		echo "WARNING: process '${MONITOR_PROC}' is still running, unable to unmount volume safely."
		exit 2;
	else
		if [ ${_FLAG_QUIET} == 0 ]
		then
			echo "INFO: Process ${MONITOR_PROC} is not running."
		fi
	fi
}

mount_func (){
	if [ ${_FLAG_QUIET} == 0 ]
	then
		echo "[Attempting to mount iSCSI volume]"
	fi

	if [[ ${_ISCSI_CON} == *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]]
	then
		echo ""
		echo "WARNING: You're already connected to the '${IQN}' iSCSI target."
		echo "Use the -f (--force) flag to bypass this warning."
		exit 2;
	fi

	iscsiadm --mode discovery -t sendtargets --portal $PORTAL_IP > /dev/null 2>&1
	iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --login > /dev/null 2>&1
	sleep 3
	mount /dev/disk/by-uuid/$DISK_UUID $MOUNT_DIR -o noauto,noperm
}

umount_func (){
	if [ ${_FLAG_QUIET} == 0 ]
	then
		echo "[Attempting to un-mount iSCSI volume]"
	fi

	if [[ $_FLAG_FORCE -eq 0 ]]; then 
		check_proc_status
	fi

	if [[ ${_ISCSI_CON} != *${IQN}* ]] && [[ $_FLAG_FORCE -eq 0 ]]
	then
		echo ""
		echo "WARNING: You're not currently connected to the '${IQN}' iSCSI target."
		echo "Use the -f (--force) flag to bypass this warning."
		exit 2;
	fi

	umount -l $MOUNT_DIR
	sleep 3
	iscsiadm --mode node --targetname $IQN --portal $PORTAL_IP --logout > /dev/null 2>&1
}

check_admin (){
	if [[ $EUID -ne 0 ]]
	then
		echo "This command requires root, please re-run with appropriate permissions."
		exit 1;
	fi
}

# Process input parameters
for arg in "$@"; do
	case "$arg" in
		-q|--quiet)
			_FLAG_QUIET=1
			shift
			;;
		-f|--force)
			_FLAG_FORCE=1
			shift
			;;
		-m|--mount)
			check_admin
			load_conf_file
			read_iscsi_con
			mount_func
			report_status
			shift
			;;
		-u|--umount)
			check_admin
			load_conf_file
			read_iscsi_con
			umount_func
			report_status
			shift
			;;
		-s|--status)
			check_admin
			load_conf_file
			read_iscsi_con
			report_status
			shift
			;;
		-r|--release)
			echo "Smart iSCSI Mounter v$_RELEASE_VER"
			shift
			;;
		-h|--help)
			show_help
			shift
			;;
	esac
done

exit 0;
