#! /bin/bash

# $Id: get-boot-info 3027 2005-11-10 22:37:16Z lange $
#*********************************************************************
#
# bootinfo -- get boot information via DHCP or BOOTP protocol
#
# This script is part of FAI (Fully Automatic Installation)
# (c) 2003-2004 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.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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=`ifconfig | perl -anF'\s+' -e 'print "$F[0]\n" if $F[0];' | grep -v "^lo"`
    # 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/'`

    # another way to determine the first four ethernet devices
    local dev devices
    devices=$(seq 0 1 4)
    for dev in $devices; do
        ifconfig eth$dev up 2>/dev/null && netdevices_all="$netdevices_all eth$dev"
    done

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

    boot=1
    dhclient -lf /dev/null $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_LOCATION=$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
dmesg | grep -q "Sending DHCP requests"  && get_dhcp_info
dmesg | grep -q "Sending BOOTP requests" && get_bootp_info

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