#!/bin/sh -e

##########################################################################
#   Script description:
#       Enable NFS server
#
#   History:
#   Date        Name        Modification
#   2019-12-29  J Bacon     Begin
##########################################################################

usage()
{
    cat << EOM

Usage: $0 'allowed-clients' sysctl|daemon domain

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

For NFSv4 servers, allowed-clients is added to the V4: root directory entry.
This line is NOT automatically updated by auto-nfs-add-share when hosts
outside the original list or network are specified.  Be sure to check
the exports file after running auto-nfs-add-share.

daemon indicates that the nfsuserd/idmap service will be enabled to map
usernames between server and client on NFSv4 mounts.  This method tolerates
mismatched UIDs and GIDs (which you may like or not like).

sysctl is FreeBSD-specific and does not require the use of the nfsuserd
service.  It does require UIDs and GIDs on server and client to agree for
NFSv4 mounts, as they do for NFSv3 and possibly other services.

domain is required only for daemon setups.  It is an arbitrary string
(free of whitespace) traditionally set to the domain name of the server and
clients, but can be overridden in case servers and clients do not all have
the same domain name.  It must be the same on server and client.

Examples:

    $0 -network=192.168.0.0/24 sysctl
    $0 '-network=192.168.0.0 -mask=255.255.255.0' sysctl
    $0 'bobspc.local julie.laptop.local' daemon our.domain

Run "man exports" for a list of options.

EOM
    exit 1
}


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

case $# in
2)
    if [ $2 != sysctl ]; then
	usage
    fi
    if [ $(auto-ostype) != FreeBSD ]; then
	printf "sysctl is only available on FreeBSD.\n"
	exit 1
    fi
    ;;

3)
    if [ $2 != daemon ]; then
	usage
    fi
    domain="$3"
    ;;

*)
    usage
    ;;

esac

clients="$1"
mapping="$2"

case $(auto-ostype) in
FreeBSD)
    if ! fgrep -q $(hostname) /etc/hosts; then
	printf "$0: Warning: $(hostname) is not listed in /etc/hosts.\n"
	printf "The NFS server may not function until this is corrected.\n"
	printf "Run auto-nfs-restart after updating /etc/hosts.\n"
    fi
    
    touch /etc/exports
    auto-append-line "V4: / $clients" /etc/exports $0
    auto-append-line 'nfsv4_server_enable="YES"' /etc/rc.conf $0
    auto-enable-service -s nfsd nfs_server $0
    auto-enable-service -s statd rpc_statd $0
    auto-enable-service -s lockd rpc_lockd $0
    
    if [ $mapping = sysctl ]; then
	auto-set-sysctl vfs.nfs.enable_uidtostring 1 $0
	auto-set-sysctl vfs.nfsd.enable_stringtouid 1 $0
	auto-disable-service nfsuserd
    else
	auto-set-sysctl vfs.nfs.enable_uidtostring 0 $0
	auto-set-sysctl vfs.nfsd.enable_stringtouid 0 $0
	auto-set-conf-var nfsuserd_flags "'-domain $domain'" /etc/rc.conf $0
	auto-enable-service nfsuserd $0
	# auto-enable-service won't restart an existing service and we
	# may have changed the domain
	service nfsuserd restart
    fi
    ;;

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

esac
auto-nfs-restart
