#!/sbin/runscript
# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2 
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/hostname,v 1.14 2003/03/11 02:30:22 azarah Exp $


depend() {
	need checkroot
}

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

start() {
	local myhost="localhost"
	local mydomain="localdomain"
	local retval=0
	
	if checkconfig
	then
		myhost="$(awk '{ sub(/\..*/, ""); print }' /etc/hostname)"
		mydomain="$(awk '{ if (sub(/^[^\.]*\./, "")) print }' /etc/hostname)"

		if [ -z "${mydomain}" ]
		then
			mydomain="localdomain"
		fi
	fi
	
	ebegin "Setting hostname to ${myhost}"
	/bin/hostname "${myhost}"
	retval=$?
	eend ${retval} "Failed to set the hostname"
	
	ebegin "Setting domainname to ${mydomain}"
	/bin/domainname "${mydomain}"
	retval=$(($retval + $?))
	eend ${retval} "Failed to set the domainname"

	if [ "${retval}" -eq 0 ]
	then
		#setup $HOSTNAME
		echo "HOSTNAME=\"${myhost}\"" >/etc/env.d/01hostname
	fi

	return ${retval}
}


# vim:ts=4
