#!/bin/sh
#
# nfs-server-config
#
# This file is part of the Cygwin port of the Universal NFS Server
#
# TODO: Should ask the user how they would like to install
# the NFS server - as Windows service, or under inetd.
#
# For now, assumes an NT/W2K install, and installs things
# as services.

# Stop and uninstall portmap service
cygrunsrv.exe --stop "portmap" > /dev/null 2>&1
cygrunsrv.exe --remove "portmap" > /dev/null 2>&1

# Stop and uninstall mountd service
cygrunsrv.exe --stop "mountd" > /dev/null 2>&1
cygrunsrv.exe --remove "mountd" > /dev/null 2>&1

# Stop and uninstall nfsd service
cygrunsrv.exe --stop "nfsd" > /dev/null 2>&1
cygrunsrv.exe --remove "nfsd" > /dev/null 2>&1

# Parameters
PMAP_D='Cygwin portmap'
PMAP_F='Cygwin RPC Port Mapping service'
MNTD_D='Cygwin mountd'
MNTD_F='Cygwin NFS Mount service'
NFSD_D='Cygwin nfsd'
NFSD_F='Cygwin Network File System (NFS) service'

# Install portmap service
echo "Installing portmap as '$PMAP_D'"
cygrunsrv.exe -I "portmap" -d "$PMAP_D" -f "$PMAP_F" -p /usr/sbin/portmap -e CYGWIN=ntsec -a "-F"

# Install mountd service
echo "Installing mountd as '$MNTD_D'"
cygrunsrv.exe -I "mountd" -d "$MNTD_D" -f "$MNTD_F" -p /usr/sbin/rpc.mountd -e CYGWIN=ntsec --dep portmap -a "-F"

# Install nfsd service
echo "Installing nfsd as '$NFSD_D'"
cygrunsrv.exe -I "nfsd" -d "$NFSD_D" -f "$NFSD_F" -p /usr/sbin/rpc.nfsd -e CYGWIN=ntsec --dep portmap -a "-F"

# Create sample /etc/exports (ony if it does not already exist)
EXPORTS=/etc/exports
if [ ! -f ${EXPORTS} ]; then
	echo "Creating sample ${EXPORTS} file..."
	cat > ${EXPORTS} <<- EOF
		# sample /etc/exports file

		# Export the entire Cygwin filesystem to machines master and
		# trusty. In addition to write access, all uid squashing is
		# turned off for host trusty.

		# /					master(rw) trusty(rw,no_root_squash)

		# Example of wildcard hostnames.

		# /projects			proj*.local.domain(rw)

		# Example of wildcard netgroups (this is the entry '@trusted').

		# /usr				*.local.domain(ro) @trusted(rw)

		# Gives read-only access to the host grimjack.  The UID and GID
		# for anonymous requests are explicitly set, and all requests
		# are forced to use the anonymous UID/GID.

		# /home/joe			grimjack(ro,all_squash,anonuid=501,anongid=546)

		# Give read-write access to anyone, and force all requests to
		# use the default anonymous UID/GID. The insecure option in this
		# entry also allows clients with NFS implementations that don't
		# use a reserved port for NFS.

		# /pub				(ro,all_squash)

		# Deny all NFS users access to the private directory that exists
		# under the public directory.

		# /pub/private		(noaccess)
	EOF
#	chmod 664 ${EXPORTS}
#	chown 18:544 ${EXPORTS}
fi

if [ "" = "`/bin/grep nobody /etc/passwd`" ]; then
	if [ "" = "`grep Guest /etc/passwd`" ]; then
           cat <<EOF

Could not find user 'Guest' in /etc/passwd

In order for mountd and nfsd to function properly,
you should add the user 'Guest' to your /etc/passwd,
for example:

    mkpasswd.exe -l -u Guest >> /etc/passwd

EOF
	fi
fi

if ! /bin/mount -m | /bin/grep -q ' -s .*"/"'; then
    cat <<EOF

mount(1) command did not return SYSTEM mount(s).

It looks like you have installed Cygwin for a single user.
Cygwin mount points will not be available to programs installed
as Windows services. This will keep portmap, mountd, and nfsd
from running as Windows services.

In order for portmap, mountd and nfsd to function properly,
you should establish global mount points using the /bin/mount
utility. You can change user-specific Cygwin mount points to
global mount points using the following command:

    eval `mount -m | sed -e 's/ -u / -s /g' -e 's/$/;/'`

You current mount -m  listing is:

EOF

    /bin/mount -m


fi
