#!/bin/sh
. /boot.conf
test "$LOG_START" && echo "<*> Executing '$0 $*'"

if ! test "$HOSTNAME"; then
    HOSTNAME=`hostname`
fi
if test "$HOSTNAME" = "(none)"; then
    # Get hostname from MAC addr of any iface
    # 2>/dev/null: suppress "no /proc/xxx" message
    macs=`ifconfig -a 2>/dev/null \
    | grep -F 'Link encap:Ethernet  HWaddr' \
    | cut -b39-55 \
    | xargs`
    for mac in $macs; do
	#echo DEBUG "mac $mac"
	i=0; while test "${MAC_LIST[$i]}"; do
	    #echo DEBUG "chk ${MAC_LIST[$i]}"
	    if test "${MAC_LIST[$i]}" = "$mac"; then
		HOSTNAME="${HOSTNAME_LIST[$i]}"
		break 2
	    fi
	let i+=1; done
    done
    if test "$HOSTNAME" = "(none)"; then
	# not found
	HOSTNAME="stranger"
	echo    "* Hostname is not specified. Reverting to default '$HOSTNAME'"
	echo -n "  Please fix this ASAP. Press <Enter>: "; read junk
    fi
fi
test "$LOG_STAGE" && echo "* Setting hostname: '$HOSTNAME'"
hostname "$HOSTNAME"

exit 0
