#!/bin/sh

set -e

update_initramfs()
{
    if [ -x /usr/sbin/update-initramfs ] && \
       [ -e /etc/initramfs-tools/initramfs.conf ] && \
       [ -e /etc/iscsi/iscsi.initramfs ]; then
        update-initramfs -u
    fi
}

case "$1" in
    configure)
	# Move old configuration from /etc/ into /etc/iscsi/
	# But only if configuration in /etc/iscsi is untouched
	
        if [ -f /etc/initiatorname.iscsi ] ; then
            if grep -q "^GenerateName=yes" /etc/iscsi/initiatorname.iscsi ; then
                mv /etc/initiatorname.iscsi /etc/iscsi/initiatorname.iscsi
		chmod 600 /etc/iscsi/initiatorname.iscsi
	    fi
	fi

    # generate a unique iSCSI InitiatorName
    NAMEFILE=/etc/iscsi/initiatorname.iscsi
    if [ ! -e $NAMEFILE ] && [ -z "$2" ] ; then
        if [ ! -x /usr/sbin/iscsi-iname ] ; then
            echo "Error: /usr/sbin/iscsi-iname does not exist, driver was not successfully installed"
            exit 1;
        fi
        # Generate a unique InitiatorName and save it
        INAME=`/usr/sbin/iscsi-iname -p iqn.1993-08.org.debian:01`
        if [ "$INAME" != "" ] ; then
            echo "## DO NOT EDIT OR REMOVE THIS FILE!" > $NAMEFILE
            echo "## If you remove this file, the iSCSI daemon will not start." >> $NAMEFILE
            echo "## If you change the InitiatorName, existing access control lists" >> $NAMEFILE
            echo "## may reject this initiator.  The InitiatorName must be unique">> $NAMEFILE
            echo "## for each iSCSI initiator.  Do NOT duplicate iSCSI InitiatorNames." >> $NAMEFILE
            printf "InitiatorName=$INAME\n"  >> $NAMEFILE
            chmod 600 $NAMEFILE
        else
            echo "Error: failed to generate an iSCSI InitiatorName, driver cannot start."
            echo
            exit 1;
        fi
    fi
	
	if [ -d /var/lib/open-iscsi ]; then
		chmod 700   /var/lib/open-iscsi
	else
		mkdir /var/lib/open-iscsi
		chmod 700   /var/lib/open-iscsi
	fi

	update_initramfs
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

#DEBHELPER#

exit 0
