#!/bin/sh
# check out de http://bazaar.launchpad.net/~ltsp-upstream/ltsp/ltsp-trunk/annotate/head%3A/client/initramfs/scripts/nfs-bottom/ltsp
# set hostname to ltsp if none is received from dhcp

read HOSTNAME < /proc/sys/kernel/hostname
if [ "${HOSTNAME}" = "(none)" ]; then
    HOSTNAME_BASE="ltsp"
    # get networking configuration
    . /tmp/net-*.conf
    if [ -f ${rootmnt}/etc/lts.conf ] && [ -x ${rootmnt}/usr/bin/getltscfg ]; then
        # get defaults from lts.conf
        eval $(chroot ${rootmnt} /usr/bin/getltscfg -a)
    fi
    case $HOSTNAME_EXTRA in
        mac)
            HOSTNAME_EXTRA=$(ip link show $DEVICE | awk '/ether/{print $2}' | cut -c 9- | tr -d ':')
            ;;
        ip|"")
            HOSTNAME_EXTRA=$(echo "$IPV4ADDR.$IPV4NETMASK" | awk -F "." '{ print (($1%(256-$5)*256+$2%(256-$6))*256+$3%(256-$7))*256+$4%(256-$8) }')
            ;;
    esac
    echo "$HOSTNAME_BASE$HOSTNAME_EXTRA" > /proc/sys/kernel/hostname
fi
cat /proc/sys/kernel/hostname > ${rootmnt}/etc/hostname || true
