#!/bin/sh
#
# (C) Copyright 1993, 1994 Mike Jagdis (jaggy@purplet.demon.co.uk)
#

if [ -n "$BASH_VERSION" ]; then
	use_escapes="-e"
else
	use_escapes=""
fi

clear
echo "                         SYSTEM NAME CONFIGURATION"
echo "                         ========================="
echo

# Try and figure out what the hostname is currently set to.
HOSTNAME=

# This is where the Purple Distribution puts it.
if [ -f "/etc/default/hostname" ]; then
	HOSTNAME=`cat /etc/default/hostname 2> /dev/null`
fi

# I've seen this. SLS? MCC? Tamu?
if [ -n "$HOSTNAME" -a -f "/etc/default/host" ]; then
	HOSTNAME=`cat /etc/default/host 2> /dev/null`
fi

# Possible?
if [ -n "$HOSTNAME" -a -f "/etc/hostname" ]; then
	HOSTNAME=`cat /etc/hostname 2> /dev/null`
fi

# This seems to be use by the Net-2 install script.
if [ -n "$HOSTNAME" -a -f "/etc/HOSTNAME" ]; then
	HOSTNAME=`cat /etc/HOSTNAME 2> /dev/null`
fi

# Fer gawd's sake...
if [ -n "$HOSTNAME" -a -f "/etc/systemid" ]; then
	HOSTNAME=`cat /etc/systemid 2> /dev/null`
fi


echo
echo "The host name of your machine is the name by which it is known to"
echo "mail systems, networks etc. You can choose almost anything you like"
echo "as the host however it shouldn't be too long, should not contain any"
echo "of the characters '@.!%()' space or tab and should not start with a"
echo "digit (although it may contain digits). All the best names are, of"
echo "course, already in use by many others who all think they have unique"
echo "names for their machines..."
echo
if [ -z "$HOSTNAME" ]; then
	echo $use_escapes "Enter your host name: \c"
else
	echo $use_escapes "Enter your host name [$HOSTNAME]: \c"
fi
read it
if [ -z "$it" ]; then
	it=$HOSTNAME
fi
while [ -z "$it" ]
do
	if [ -z "$HOSTNAME" ]; then
		echo $use_escapes "Enter your host name: \c"
	else
		echo $use_escapes "Enter your host name [$HOSTNAME]: \c"
	fi
	read it
	if [ -z "$it" ]; then
		it=$HOSTNAME
	fi
done
HOSTNAME=$it

echo $HOSTNAME > /etc/default/hostname

# Split into hostname and domainname parts
DOMAINNAME=`echo $HOSTNAME | awk '{ i = index($0, "."); if (i) print substr($0, i+1) }'`
HOSTNAME=`  echo $HOSTNAME | awk '{ i = index($0, "."); if (i) print substr($0, 1, i-1); else print $0 }'`

# If the domain isn't part of the hostname as returned by gethostname()
# we ask the user to enter it, save it in /etc/default/domainname for
# future reference and create a domain line in /etc/resolv.conf so that
# hostname only lookups work. If we have a domain from gethostname() then
# we get rid of any domain line in /etc/resolv.conf otherwise things may
# get confusing.
if [ -z "$DOMAINNAME" ]; then
	echo
	echo "Machines typically exist within a hierarchy of domains. The highest"
	echo "level domains are fairly general (e.g. .edu for educational sites,"
	echo ".com for commercial sites, .uk for the UK, .au for Australia etc.)"
	echo "The top level domains are divided into small domains, which in turn"
	echo "may be further subdivided until the level of an individual machine"
	echo "is reached. If you are connected to a network you probably have been"
	echo "assigned a domain which you should use. If not you can make your own"
	echo "domain up and change it or register it if you ever connect to a network."
	echo "Common choices of domain include uucp, company.com, company.co.uk etc."
	echo

	DOMAINNAME=`cat /etc/default/domainname 2> /dev/null`
	if [ -z "$DOMAINNAME" ]; then
		echo $use_escapes "Enter your domain name: \c"
	else
		echo $use_escapes "Enter your domain name [$DOMAINNAME]: \c"
	fi
	read it
	if [ -z "$it" ]; then
		it=$DOMAINNAME
	fi
	while [ -z "$it" ]
	do
		if [ -z "$DOMAINNAME" ]; then
			echo $use_escapes "Enter your domain name: \c"
		else
			echo $use_escapes "Enter your domain name [$DOMAINNAME]: \c"
		fi
		read it
		if [ -z "$it" ]; then
			it=$DOMAINNAME
		fi
	done
	DOMAINNAME=$it

	echo $DOMAINNAME > /etc/default/domainname

	echo "domain	$DOMAINNAME" > /etc/.resolv.conf
	grep -v domain /etc/resolv.conf >> /etc/.resolv.conf 2> /dev/null
	rm -f /etc/resolv.conf
	mv /etc/.resolv.conf /etc/resolv.conf
else
	# We appear to have the domain name in the host name.
	grep -v domain /etc/resolv.conf > /etc/.resolv.conf
	if [ -s /etc/.resolv.conf ]; then
		rm -f /etc/resolv.conf
		mv /etc/.resolv.conf /etc/resolv.conf
	fi
fi

echo
echo "Changes will only take effect from the next reboot..."
