#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/rc-scripts/init.d/hostname,v 1.21 2004/10/19 04:20:27 vapier Exp $

# This only set what /bin/hostname returns.  If you need to setup NIS, meaning
# what /bin/domainname returns, please see:
#
#   http://www.linux-nis.org/nis-howto/HOWTO/
#
# To have a proper FQDN, you need to setup /etc/hosts and /etc/resolv.conf
# properly (domain entry in /etc/resolv.conf, and FQDN in /etc/hosts).

depend() {
	need checkroot
}

checkconfig() {
	if [ ! -f /etc/hostname ] || [ -z "$(</etc/hostname)" ]
	then
		if [ "${myhost}" = "localhost" ]
		then
			eerror "You need to set /etc/hostname to a valid hostname"
			return 1
		fi
	fi
}

start() {
	local myhost="$(/bin/hostname 2>/dev/null)"
	local retval=0

	# If the hostname is already set via the kernel, and /etc/hostname 
	# isn't setup, then we shouldn't go reseting the configuration #38172.
	if [ -z "${myhost}" ] || [ "${myhost}" = "(none)" ]
	then
		myhost="localhost"
	fi

	# We use whatever is in /etc/hostname here.  That means:
	#
	# 1)  For doing it the proper way, you should NOT put a
	#     FQDN in there, but:
	#     a) Only have the hostname in there (no dns/nis domainname)
	#     b) Set the dnsdomainname via /etc/resolv.conf
	#     c) Set the nisdomainname via 'domainname'.
	#
	# 2)  If the user want a FQDN in there, it should be possible
	#     without hacking things to pieces, but then he should
	#     know what he is doing ...
	#
	if checkconfig
	then
		myhost="$(</etc/hostname)"
	fi

	ebegin "Setting hostname to ${myhost}"
	/bin/hostname "${myhost}"
	retval=$?
	eend ${retval} "Failed to set the hostname"

	if [ "${retval}" -eq 0 ]
	then
		# setup $HOSTNAME, ignore errors in case /etc is readonly
		echo "HOSTNAME=\"${myhost}\"" 2> /dev/null > /etc/env.d/01hostname
	fi

	return ${retval}
}


# vim:ts=4
