#!/bin/sh -e

##########################################################################
#   Script description:
#       Add a share to the local NFS setup
#       
#   History:
#   Date        Name        Modification
#   2019-12-27  Jason Bacon Begin
##########################################################################

usage()
{
    cat << EOM

Usage: $0 directory 'allowed-clients' [additional export flags]

The allowed-clients spec is a list of hostnames or a standard network spec.
If it contains spaces, it must be quoted.

Examples:

    $0 /usr/home -network=192.168.0.0/24 -maproot=root
    $0 /usr/home '-network=192.168.0.0 255.255.255.0' -maproot=root
    $0 /share1 'bobspc.local julie.laptop.local'

EOM
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $# -lt 2 ]; then
    usage
fi

dir="$1"
clients="$2"
shift; shift
flags="$@"

if [ ! -d "$dir" ]; then
    printf "$0: $dir is not a directory.\n"
    exit 1
fi

case $(auto-ostype) in
FreeBSD)
    if mount | awk -v dir="$dir" '$3 == dir { print $4 }' | fgrep -q zfs; then
	fs=$(mount | awk -v dir="$dir" '$3 == dir { print $1 }')
	zfs set sharenfs="$flags $clients" "$fs"
    else
	auto-append-line "$dir $flags $clients" /etc/exports $0
    fi
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
auto-nfs-restart
