#!/bin/bash
PSTORAGE_ISCSI_LIB=/usr/libexec/vstorage-iscsi/vstorage_functions
ROOT=""
IQN=""
LUN_ID=""
VOLUME_ATTR=""
ARGS="${@}"

function print_usage {
        exec 1>&2
	echo "Set attributes for existing LUN."
        echo "Usage:"
	echo -n " $CTL_TOOL lun-set [-r,--root ROOT_DIR] -t,--target IQN -l,--lun ID "
	echo "--vstorage-attr ATTRS"
        echo "Options:"
	echo "  -r,--root ROOT_DIR      set ISCSI_ROOT instead of use"
	echo "                          $ISCSI_ETC/config (optional)."
	echo "  -t,--target IQN         target's IQN."
	echo "  -l,--lun ID             ID of existing LUN."
        echo "  --vstorage-attr ATTRS   Set Vstorage attribues. The ATTRS are attributes delimited by"
        echo "                          comma (optional). Use command 'vstorage help set-attr' to get"
        echo "                          list of valid attributes."
        echo " "
        exit 1
}

function parse_args {

	[ ${#} -eq 0 ] && print_usage

        while [ "${#}" -gt 0 ]; do
                case "${1}" in
			"-h"|"--help")
				print_usage
				;;
			"-r"|"--root")
				[ -z "${2}" ] && print_usage
				ROOT="${2}"
				shift
				shift
				;;
                        "-t"|"--target")
				[ -z "${2}" ] && print_usage
				IQN="${2}"
				shift
				shift
				;;
			"-l"|"--lun")
				[ -z "${2}" ] && print_usage
				LUN_ID="${2}"
				shift
				shift
				;;
                        "--vstorage-attr")
                                [ -z "${2}" ] && print_usage
                                VOLUME_ATTR="${2}"
                                shift
                                shift
                                ;;
                        *)
                                echo "Unknown option '${1}'." 1>&2
                                print_usage
                                ;;
                esac
        done

	if [ -z "$IQN" ] ; then
		echo "Target (-t,--target) must be specified." 1>&2
		print_usage
	fi

	if [ -z "$LUN_ID" ] ; then
		echo "LUN id (-l,--lun) must be specified." 1>&2
		print_usage
	fi

	if [ -z "$VOLUME_ATTR" ] ; then
		echo "Volume attributes (--vstorage-attr) must be specified." 1>&2
		print_usage
	fi
}

function do_set_attrs {
	local iqn="$1"
	local lun_id="$2"
	local lun_attr="$3"
	local lun_dir="$ISCSI_ROOT/$iqn/lun${lun_id}"

	pcs_iscsi_check_target $iqn

        if [ ! -d "$lun_dir" ] ; then
                echo "LUN $lun_id for target $iqn doesn't exist" 1>&2
                return 1
        fi

	attrs=$(echo $VOLUME_ATTR | tr "," "\n")
	for attr in $attrs
	do
		$VSTORAGE_BIN "set-attr" -R "$lun_dir" $attr >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Unable to apply volume attributes '$attr' for $lun_dir" 1>&2
			exit 2
		fi
	done

	exit 0
}

if [ ! -x $PSTORAGE_ISCSI_LIB ] ; then
	echo "Unable find executable $PSTORAGE_ISCSI_LIB" 1>&2
	exit 1
fi

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

parse_args $ARGS

ISCSI_ROOT=`pcs_iscsi_get_root "${ROOT}"`
pcs_iscsi_check_root
pcs_iscsi_lock_exec do_set_attrs "$IQN" "$LUN_ID" "$VOLUME_ATTR"
exit $?

