#!/bin/bash

umask 022

cd /etc/init.d
chmod 755 nis

# Ask for the NIS domainname if needed.
nisdomain=`cat /etc/defaultdomain 2>/dev/null`
if [ "$nisdomain" = "" ]
then
	cat <<EOF

You now need to choose a NIS domainname for your system. If you want this
machine to just be a client, enter the NIS domainname of your network.
Otherwise choose an appropriate NIS domainname.

EOF
	fqdn=`hostname --fqdn`
	domain=${fqdn#*.}
	echo -n "NIS domain: [$domain] "
	read nisdomain
	if [ "$nisdomain" = "" ]
	then
		nisdomain=$domain
	fi
	echo $nisdomain > /etc/defaultdomain
fi

# Update the links for nis.
if [ -L /etc/rc2.d/S30nis ]
then
	rm /etc/rc?.d/[SK]30nis
fi
update-rc.d nis defaults 19

# And start the service.
/etc/init.d/nis start

# See if we need a '+' in /etc/passwd or /etc/group.
inpasswd=`grep '^+' /etc/passwd`
ingroup=`grep '^+' /etc/group`
if [ "$inpasswd" = "" ]
then
	cat <<EOF

To use the NIS services as a client, you'll need special entries
(so-called "plus-entries") in your /etc/passwd and /etc/group files.
I can add them now or you can add them later by hand.

EOF
	echo -n "Add plus-entries now? [y] "
	read ans
	case "$ans" in
		""|y*|Y*)
			echo "+::::::" >> /etc/passwd
			if [ "$ingroup" = "" ]
			then
				echo "+:::" >> /etc/group
			fi
			;;
	esac
fi

exit 0
