#!/sbin/runscript
# Copyright 1999-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/rc-scripts/init.d/domainname,v 1.8.4.2 2005/01/31 12:40:14 uberlord Exp $

depend() {
	need checkroot hostname
	before bootmisc
}

checkconfig_nis() {
	if [[ -f /etc/nisdomainname ]] ; then
		ewarn "You should stop using /etc/nisdomainname and use /etc/conf.d/domainname"
		export NISDOMAIN=$(</etc/nisdomainname)
		return 0
	fi
	[[ -n ${NISDOMAIN} ]]
}

checkconfig_dns() {
	if [[ -f /etc/dnsdomainname ]] ; then
		ewarn "You should stop using /etc/dnsdomainname and use /etc/conf.d/domainname"
		export DNSDOMAIN=$(</etc/dnsdomainname)
	fi
	[[ -z ${DNSDOMAIN} ]] && return 1

	if ! touch /etc/resolv.conf 2> /dev/null ; then
		ewarn "Unable to set domain in resolv.conf (ro root?)"
		return 1
	else
		return 0
	fi
}

start() {
	local retval=0
	local retval2=0

	if checkconfig_nis ; then
		ebegin "Setting NIS domainname to ${NISDOMAIN}"
		/bin/domainname "${NISDOMAIN}"
		retval=$?
		eend ${retval} "Failed to set the NIS domainname"
	fi

	if checkconfig_dns ; then
		ebegin "Setting DNS domainname to ${DNSDOMAIN}"
		resolv=$(grep -v '^[[:space:]]*domain' /etc/resolv.conf)
		[[ ${OVERRIDE} == "1" ]] \
			&& resolv="${resolv}"$'\n'"domain ${DNSDOMAIN}" \
			|| resolv="domain ${DNSDOMAIN}"$'\n'"${resolv}"
		echo "${resolv}" > /etc/resolv.conf
		retval2=$?
		eend ${retval2} "Failed to set the DNS domainname"
	fi

	return $((retval + retval2))
}


# vim:ts=4
