#!/bin/sh
# $NetBSD: script,v 1.2 2004/10/22 14:00:23 peter Exp $

if [ "$2" = "both" -o "$2" = "boot" ]; then
	. /etc/rc.conf
	# $hostname is not set in rc.conf and /etc/myname exists
	if [ -z "$hostname" ] && [ -f "/etc/myname" ]; then
		echo "$1" >/etc/myname
	# $hostname is set in rc.conf, edit it
	elif [ -n "$hostname" ]; then
		sed "s/hostname=.*/hostname=\"$1\"/" < /etc/rc.conf \
		    > /etc/rc.conf.new
		mv /etc/rc.conf.new /etc/rc.conf
	# Nothing is set, so set the new hostname in rc.conf
	else
		echo "hostname=\"$1\"" >>/etc/rc.conf
	fi	
fi
if [ "$2" = "both" -o "$2" = "now" ]; then
	hostname $1
fi
if [ $? -eq 0 ]; then
	echo "Hostname has been changed to $1"
else
	echo "Failed to change hostname to $1"
fi
