#!/bin/sh

PREREQ=""

prereqs()
{
	echo "$PREREQ"
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

try_configure_networking ()
{
	configure_networking || return $?

	# if ipconfig wrote to /tmp, copy its output to /run
	if [ -n "${DEVICE}" ] && [ -e "/tmp/net-${DEVICE}.conf" ] &&
	   [ ! -e "/run/net-${DEVICE}.conf" ]; then
		cp "/tmp/net-${DEVICE}.conf" "/run/net-${DEVICE}.conf"
	fi
	if [ -n "${DEVICE}" ] && [ -e "/run/net-${DEVICE}.conf" ]; then
		echo "${DEVICE}" >/run/initramfs/open-iscsi.interface
	fi

	return 0
}

do_iscsi_login ()
{
	# Bring in the main config
	. /conf/initramfs.conf
	for conf in conf/conf.d/*; do
		[ -f ${conf} ] && . ${conf}
	done
	. /scripts/functions

	wait_for_udev

	# FIXME should wait properly for network device rather than polling
	retry_nr=0
	if [ -z "${ROOTDELAY}" ]; then
		delay=180
	else
		delay=${ROOTDELAY}
	fi
	while [ ${retry_nr} -lt ${delay} ]; do
		[ ${retry_nr} -gt 0 ] && \
		[ "$quiet" != "y" ] && log_begin_msg "Retrying network configuration"
		# Run this in a subshell, since a failed attempt to source
		# /run/net-*.conf would otherwise cause this whole script to
		# exit.
		if (try_configure_networking); then
			[ ${retry_nr} -gt 0 ] && [ "$quiet" != "y" ] && log_end_msg
			break
		fi
		retry_nr=$(( ${retry_nr} + 1 ))
		/bin/sleep 1
	done

	modprobe iscsi_tcp
	modprobe crc32c

	if [ -z $ISCSI_INITIATOR ]; then
		. /etc/initiatorname.iscsi
		ISCSI_INITIATOR=$InitiatorName
	fi

	if [ -z $ISCSI_TARGET_PORT ]; then
		ISCSI_TARGET_PORT=3260
	fi

	if [ -z $ISCSI_TARGET_GROUP ]; then
		ISCSI_TARGET_GROUP=1
	fi

	iscsistart -i $ISCSI_INITIATOR -t $ISCSI_TARGET_NAME	\
		   -g $ISCSI_TARGET_GROUP -a $ISCSI_TARGET_IP	\
		   -p $ISCSI_TARGET_PORT			\
		   ${ISCSI_USERNAME:+-u "$ISCSI_USERNAME"}	\
		   ${ISCSI_PASSWORD:+-w "$ISCSI_PASSWORD"}	\
		   ${ISCSI_IN_USERNAME:+-U "$ISCSI_IN_USERNAME"}\
		   ${ISCSI_IN_PASSWORD:+-W "$ISCSI_IN_PASSWORD"}
}

parse_iscsi_ops ()
{
	. /etc/iscsi.initramfs

	for x in $(cat /proc/cmdline); do
		case ${x} in
        	iscsi_initiator=*)
                	ISCSI_INITIATOR="${x#iscsi_initiator=}"
                	;;
        	iscsi_target_name=*)
                	ISCSI_TARGET_NAME="${x#iscsi_target_name=}"
                	;;
        	iscsi_target_ip=*)
                	ISCSI_TARGET_IP="${x#iscsi_target_ip=}"
                	;;
        	iscsi_target_port=*)
                	ISCSI_TARGET_PORT="${x#iscsi_target_port=}"
                	;;
		iscsi_target_group=*)
			ISCSI_TARGET_GROUP="${x#iscsi_target_group=}"
			;;
		iscsi_username=*)
			ISCSI_USERNAME="${x#iscsi_username=}"
			;;
		iscsi_password=*)
			ISCSI_PASSWORD="${x#iscsi_password=}"
			;;
		iscsi_in_username=*)
			ISCSI_IN_USERNAME="${x#iscsi_in_username=}"
			;;
		iscsi_in_password=*)
			ISCSI_IN_PASSWORD="${x#iscsi_in_password=}"
			;;
		esac
	done
}

if [ ! -x /sbin/iscsistart ]; then
	exit 0
fi

parse_iscsi_ops

if [ -z $ISCSI_TARGET_NAME ] || [ -z $ISCSI_TARGET_IP ]; then
	exit 0
fi

do_iscsi_login

exit 0
