#!/bin/sh -e

# debirf module: x0_work_around_dhclient_hostname

# Copyright (C) 2018 Gunter Miegel coinboot.io
#
# This file is part of Coinboot.
#
# Coinboot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# We want to set the hostname on clients over DHCP.
# The function set_host_name() /sbin/dhclient-script only sets the hostname given by
# the DHCP server when the current hostname is empty, '(none)' or 'localhost'.

# Write intermediate hostname 'localhost' to /etc/hostname
echo "localhost" > "${DEBIRF_ROOT}/etc/hostname"

cat <<'EOF' > "${DEBIRF_ROOT}/etc/dhcp/dhclient-enter-hooks.d/dhcp_hostname"
hostname $(echo $new_ip_address | sed 's/\./-/g')

echo $(hostname) > /etc/hostname
echo "127.0.1.1 $(hostname)" >> /etc/hosts
EOF

# Write /etc/hosts accordingly
cat <<EOF > "${DEBIRF_ROOT}/etc/hosts"
127.0.0.1 localhost
EOF
