#! /bin/bash

#*********************************************************************
#
# bootinfo -- get boot information via DHCP protocol
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2022 by Thomas Lange, lange@cs.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# This program 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 2 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.
#
# A copy of the GNU General Public License is available as
# `/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************

# this script writes received information to $LOGDIR/boot.log

bootlog=$LOGDIR/boot.log
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
netdevice_info() {

    # devices that are running
    netdevices_up=$(ip -br a| awk '/UP / {if ($3) print $1}' | tr '\n' ' ')
    # netdevices is the list of ethernet devices which will be used for bootpc (maybe dhcp)
    # if not defined, use boot messages to determine network devices
    [ -n "$netdevices" ] || netdevices=$netdevices_up

    netdevices_all=$(ip -br a| grep -P -o "^eth\S+|^en\S+"|tr '\n' ' ')
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_dhcp_info() {

    if [ $boot -eq 1 ]; then
	return
    fi
    boot=1

    if [ -n "$netdevices_up" ]; then
	dhclient -lf /dev/null -cf /usr/share/fai/dhclient-fai.conf -sf /usr/share/fai/dhclient-fai-script $netdevices >>$bootlog 2> $LOGDIR/dhclient.log
	setnet
	if [ -f /run/dhclient.pid ]; then
	    kill $(cat /run/dhclient.pid)
	fi
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setnet() {

    # get network parameters
    local dummy

    # determine the name of the default network interface
    # try several times if dhclient needs some more time
    local i=0
    NIC1=$(ip route | awk '/^default/ {print $5}'|head -1)
    while [ ! "$NIC1" -a $i -lt 8 ]; do
        sleep 2
	NIC1=$(ip route | awk '/^default/ {print $5}'|head -1)
        (( i += 1 ))
    done
    if [ -z "$NIC1" ]; then
	NIC1=$(ip route | awk '/ dev / {print $3}'|head -1)
    fi

    # only write info if interface is known
    if [ -n "$NIC1" ]; then
	CIDR=$(ip --br ad sh $NIC1|awk '{print $3}')
	IPADDR=${CIDR%/*}
	GATEWAYS=$(ip route | awk '/^default/ {print $3}')

	cat >> $bootlog <<-EOF
	NIC1=$NIC1
	IPADDR=$IPADDR
	CIDR=$CIDR
	GATEWAYS=$GATEWAYS
EOF
    fi
    if [ -n "$SERVER" ]; then
	echo "SERVER=$SERVER" >> $bootlog
    fi

    if [ -n "$DOMAIN" ]; then
        # DOMAIN was specified on the kernel command line
        echo "DOMAIN=$DOMAIN" >> $bootlog
    fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_fixed_info() {

    if [ $boot -eq 1 ]; then
	return
    fi
    boot=1
    # ip contains the network parameters
    echo $ip | grep -q : || echo "Kernel parameter ip= does not contain network parameters."
    setnet
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
nic_list_from_cmdline() {

    # parse nic(s) that are specified on the kernel command line like this:  ip=etX:dhcp

    niclist=
    line=$(< /proc/cmdline)

    for item in $line; do
	case $item in

	    ip=*:dhcp)
            nic=
	    # count the : char in the argument of ip=
	    n="${item//[^:]}"
	    # if there are more than 5 :, it's an static IP configuration, do not use this nic
	    if [ ${#n} -lt 5 ]; then
		nic=$(expr match "$item" 'ip=\(.*\):dhcp')
		niclist+=" $nic"
	    fi
	    ;;
	esac
    done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

netdevice_info
cat > $bootlog <<-EOF
	netdevices_all="$netdevices_all"
	netdevices_up="$netdevices_up"
	netdevices="$netdevices"
EOF
boot=0

# no network needed
if [ "$ip" == "off" ]; then
    boot=1
fi

# if ip= parameter contains IP address, get fixed IP data, otherwise use DHCP
if [[ $ip =~ ^.*:*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} ]]; then
    get_fixed_info
fi

# if devices are specified on the kernel command line, only use those devices for DHCP
nic_list_from_cmdline
if [ -n "$niclist" ]; then
    netdevices=$niclist
fi

get_dhcp_info

# use first Ethernet interface, even if it's not used during installation
if [ -z "$NIC1" ]; then
    # ignore some names
    NIC1=$(ls /sys/class/net/ | egrep '^en' | head -1)
    echo "NIC1=$NIC1" >> $bootlog
fi

# DHCP should not overwrite the hostname if it's already set
# then remove this line from boot.log
read hname < /proc/sys/kernel/hostname
if [ -n "$hname" -o "$hname" = '(none)' ]; then
    sed -i -e '/^HOSTNAME=/d' $bootlog
fi

if ! grep -q DOMAIN= $bootlog; then
    echo 'Info: no domain name configured, using "localdomain"' >&2
    echo "DOMAIN=localdomain" >> $bootlog
fi
