#!/usr/bin/bash
VSTORAGE_NFS_LIB=/usr/libexec/vstorage-nfs/nfs_functions2
ROOT=""
SHARE_NAME=""
IP_ADDR=""
VOL_ID=""
SIZE=""
BACKEND_GEN=""
VDISK_COUNT=""
VSTORAGE_ATTRS=()
ARGS="${@}"

function print_usage {
        exec 1>&2
        echo "Create NFS share on shared storage."
        echo "Usage:"
        echo " vstorage-nfs create -n,--name NAME -V,--volume VOL_ID -a,--address ADDR"
        if [ -n "$ENABLE_PVFS$ENABLE_MPFS" ]; then
        echo " vstorage-nfs create -n,--name NAME -s,--size SIZE -a,--address ADDR"
        echo "                     [-c,--vdisk-count COUNT]"
        echo "                     [--vstorage-attr ATTR [--vstorage-attr ATTR ...]]"
        echo " "
        echo "The first variant creates a share with the ostor-based backend,"
        echo "the second one creates a share with the ploop-based backend."
        fi
        echo " "
        echo "Options:"
        echo "  -n,--name SHARE_NAME    name for new NFS share"
        echo "  -V,--volume VOLUME_ID   volume ID for new share"
        if [ -n "$ENABLE_PVFS$ENABLE_MPFS" ]; then
        echo "  -s,--size SIZE          capacity of the share"
        echo "                          (accepts <number>K/Ki/M/Mi/G/Gi...)"
        fi
        echo "  -a,--address ADDR       address for new NFS share"
        if [ -n "$ENABLE_PVFS$ENABLE_MPFS" ]; then
        echo "  -c,--vdisk-count COUNT  number of virtual disks"
        echo "                          (default: the number of NFS nodes)"
        echo "  --vstorage-attr ATTR    storage attributes (optional)"
        echo "                          (see 'vstorage set-attr' for details)"
        fi
        echo " "
        exit 1
}

is_uint() { case $1 in '' | *[!0-9]* ) return 1;; esac ; }

function parse_args {

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

        while [ "${#}" -gt 0 ]; do
                case "${1}" in
                        "-r"|"--root")
                                [ -z "${2}" ] && print_usage
                                ROOT="${2}"
                                shift
                                shift
                                ;;
                       "-n"|"--name")
                                [ -z "${2}" ] && print_usage
                                SHARE_NAME="${2}"
                                shift
                                shift
                                ;;
			"-V"|"--volume")
                                [ -z "${2}" ] && print_usage
                                VOL_ID="${2}"
                                shift
                                shift
                                ;;
                        "-s"|"--size")
                                [ -z "$ENABLE_PVFS$ENABLE_MPFS" ] && print_usage
                                [ -z "${2}" ] && print_usage
                                [ -n "$SIZE" ] && print_usage
                                SIZE="${2}"
                                shift
                                shift
                                ;;
                        "-a"|"--address")
                                [ -z "${2}" ] && print_usage
				[ -n "$IP_ADDR" ] && print_usage
				IP_ADDR="${2}"
                                shift
                                shift
                                ;;
                        "-c"|"--vdisk-count")
                                [ -z "$ENABLE_PVFS$ENABLE_MPFS" ] && print_usage
                                [ -z "${2}" ] && print_usage
                                [ -n "$VDISK_COUNT" ] && print_usage
                                VDISK_COUNT="${2}"
                                shift
                                shift
                                ;;
                        "--vstorage-attr")
                                [ -z "$ENABLE_PVFS$ENABLE_MPFS" ] && print_usage
                                [ -z "${2}" ] && print_usage
                                VSTORAGE_ATTRS+=("${2}")
                                shift
                                shift
                                ;;
                        "-h"|"--help")
                                print_usage
                                ;;
                        *)
                                echo "Unknown option '${1}'." 1>&2
                                print_usage
                                ;;
                esac
        done

        SHARE_NAME=`echo $SHARE_NAME | sed "s#\s##g" 2>/dev/null`

        if [ -z "$SHARE_NAME" ] ; then
                echo "Share name (-n,--name) must be specified." 1>&2
                print_usage
        fi

	if [ -z "$VOL_ID" -a -z "$SIZE" ] ; then
                echo "Volume id (-V,--volume) or size (-s,--size) must be specified." 1>&2
                print_usage
        fi

	if [ -n "$VOL_ID" -a -n "$SIZE" ] ; then
                echo "Volume id (-V,--volume) and size (-s,--size) cannot be used together." 1>&2
                print_usage
        fi

	if [ -n "$VOL_ID" -a -n "$VDISK_COUNT" ] ; then
                echo "Volume id (-V,--volume) and vdisk count (-c,--vdisk-count) cannot be used together." 1>&2
                print_usage
        fi

	if [ -n "$VOL_ID" -a ${#VSTORAGE_ATTRS[@]} -ne 0 ] ; then
                echo "Volume id (-V,--volume) and storage attributes (--vstorage-attr) cannot be used together." 1>&2
                print_usage
        fi

        if [ -n "$SIZE" ]; then
                BACKEND_GEN=2
                SIZE=`pcs_nfs_parse_size "$SIZE"`
                [ -z "$SIZE" ] && print_usage
        fi

        if [ -n "$VDISK_COUNT" ]; then
                is_uint $VDISK_COUNT || print_usage
                [ "$VDISK_COUNT" -gt 0 ] || print_usage
        fi

        if [ -z "$IP_ADDR" ] ; then
                echo "Network address (-a,--address) must be specified." 1>&2
                print_usage
        fi

        IP_ADDR=`pcs_ha_validate_addr "$IP_ADDR"`
        [ -z "$IP_ADDR" ] && print_usage
}


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

source $VSTORAGE_NFS_LIB
[ -f $NFS_ETC/config ] && source $NFS_ETC/config

parse_args ${ARGS}
switch_to_nfs_lib_version $BACKEND_GEN

NFS_ROOT=`pcs_nfs_get_root "${ROOT}"`
pcs_nfs_check_root yes
if [ "x$BACKEND_GEN" = "x2" ]; then
        pcs_ha_lock_exec pcs_nfs_make_share $SHARE_NAME \
                "$SIZE" "$IP_ADDR" "$VDISK_COUNT" "${VSTORAGE_ATTRS[@]}"
else
        pcs_ha_lock_exec pcs_nfs_make_share $SHARE_NAME "$VOL_ID" "$IP_ADDR"
fi

RET=$?
if [ $RET -ne 0 ] ; then
        echo "Unable create share $SHARE_NAME" 1>&2
        exit $RET
fi
exit 0
