#! /bin/bash

# $Id$
#*********************************************************************
#
# bootinfo -- get boot information via DHCP or BOOTP protocol
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2010 by Thomas Lange, lange@informatik.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
#*********************************************************************

# TODO
# -t timeout
# a list of devices
#

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

bootlog=$LOGDIR/boot.log
timeout=30 # bootpc timeout

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
netdevice_info() {

    # devices that are running
    netdevices_up=$(ip link show up | grep "^[1-9$]" | cut -d : -f 2 | cut -d ' ' -f 2 | grep -v "^lo" | sort | uniq)
    # 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

    # some network driver do not echo eth0,..; they are not detected
    netdevices_all=$(dmesg| perl -ne 'print $&,"\n" if m/\beth[0-9]\b/')
    tmp=$(ip link | grep "^[1-9]" | cut -d : -f 2 | cut -d ' ' -f 2 | grep "^eth")
    netdevices_all="$netdevices_all $tmp"
    netdevices_all=$(for dev in $netdevices_all; do echo $dev; done| sort | uniq)

    cat > $bootlog <<-EOF
	netdevices_all="$netdevices_all"
	netdevices_up="$netdevices_up"
	netdevices="$netdevices_up"
	EOF
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_dhcp_info() {

    boot=1
    dhclient -lf /dev/null -cf /usr/share/fai/dhclient-fai.conf -sf /usr/share/fai/dhclient-fai-script $netdevices >>$bootlog 2> $LOGDIR/dhclient.log
    killall dhclient
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_bootp_info() {

    local device
    boot=1
    # use all devices in $netdevices
    for device in $netdevices; do
	echo "Sending BOOTP request using device $device"
	echo "# --- network device $device ---" >> $bootlog
	bootpc --dev $device --timeoutwait $timeout --returniffail >> $bootlog 2>&1
    done

    # change comments, because bootpc uses ^* for comments
    perl -pi -e 's/^\*/#/' $bootlog

    cat >> $bootlog <<-'EOM'
	# define variable if T17x is defined
	[ "$T170" ] && FAI_CONFIG_SRC=$T170
	[ "$T171" ] && FAI_ACTION=$T171
	[ "$T172" ] && FAI_FLAGS=$T172
EOM
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
setnet() {

    local ips=$1
    IPADDR=${ips%%:*}
    ips=${ips#*:}
    SERVER=${ips%%:*}
    ips=${ips#*:}
    GATEWAYS=${ips%%:*}
    ips=${ips#*:}
    NETMASK=${ips%%:*}
    BROADCAST=${IPADDR%\.*}.255

    cat >> $bootlog <<-EOF
	IPADDR=$IPADDR
	SERVER=$SERVER
	NETMASK=$NETMASK
	GATEWAYS=$GATEWAYS
	BROADCAST=$BROADCAST
	EOF
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get_fixed_info() {

    boot=1
    # ip contains the network parameters
    echo $ip | grep -q : || echo "Kernel parameter ip= does not contain network parameters."
    setnet $ip
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

netdevice_info
boot=0

# when using live-initramfs
if [ -f /var/log/live.log ]; then
    grep IP-Config /var/log/live.log | grep -q "DHCP" && get_dhcp_info
    grep IP-Config /var/log/live.log | grep -q "BOOTP" && get_bootp_info
else
    dmesg | grep -q "Sending DHCP requests"  && get_dhcp_info
    dmesg | grep -q "Sending BOOTP requests" && get_bootp_info
    [ $boot -eq 0 -a "$ip" = "dhcp" ] && get_dhcp_info
fi

if [ $boot -eq 0 ]; then
    get_fixed_info
fi
