if [ -z "$HOSTNAME" ] || [ "$HOSTNAME" = "localhost" ] || [ "$HOSTNAME" = "(none)" ]; then
    HOSTNAME_BASE=${HOSTNAME_BASE:-ltsp}
    case "$HOSTNAME_EXTRA" in
        mac)
            HOSTNAME_EXTRA=$(ip link show $DEVICE | awk '/ether/{print $2}' | tr ':' '-')
            ;;
        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
    HOSTNAME="$HOSTNAME_BASE$HOSTNAME_EXTRA"
fi

hostname "$HOSTNAME"
echo "$HOSTNAME" > /etc/hostname

cat <<EOF > /etc/hosts
127.0.0.1 localhost
127.0.0.2 $HOSTNAME
$SERVER server

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF

if [ -f /etc/hosts.ltsp ]; then
    cat /etc/hosts.ltsp >> /etc/hosts
fi
env | sed -n '/^HOSTS_[0-9][0-9]=/s///p' >> /etc/hosts
