set-hostname   [plain text]


#!/bin/sh
#
# set-hostname
#

. /etc/hostconfig

get_host()
{
	ifconfig ${primary} inet 2>/dev/null | sed -n '/169\.254/d;/127\.0\.0\.1/d;/0\.0\.0\.0/d;s/^.*inet \([0-9\.]*\) netmask.*/\1/p' | while read ip_address 
	do
		host=$(/usr/bin/host ${ip_address} 2>/dev/null | sed -n 's/.* domain name pointer \([^ ]*\)[\.]$/\1/p')
		if [ ! "${host}" = "" ] ; then
			echo "${host}"
			break;
		fi
	done
}

if [ ! "${HOSTNAME:=-AUTOMATIC-}" = "-AUTOMATIC-" ] ; then
	hostname "${HOSTNAME}"
	exit 1
fi

primary=$(netstat -rn | sed -n 's/default [ ]*[^ ]*[ ]*[^ ]*[ ]*[^ ]*[ ]*[^ ]*[ ]*\([^ ]*\)[ ]*[^ ]*/\1/p')

if [ ! "${primary}" = "" ] ; then
	host=$(ipconfig getoption "${primary}" host_name)
	if [ "${host}" = "" ]; then
		host=$(get_host)
	fi
fi

if [ "${host}" = "" ]; then
	localhostname="$(/usr/sbin/scutil --get LocalHostName)"
	if [ ! "${localhostname}" = "" ]; then
		host="${localhostname}.local"
	fi
fi

if [ "${host}" = "" ]; then
	host=localhost
fi

logger -i -p daemon.notice -t set-hostname setting hostname to "${host}"
hostname "${host}"

exit 0