#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2000
#
# 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, 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; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
#

. /usr/lib/gibraltar-bootsupport/common-definitions.sh

set -e

ping_tries=3
address_prefix="10.0."
address_netmask="24"
address_major=0
net_ifaces_saved=0

# set an ip address for every device
(cat /proc/net/dev | grep -v "|" | cut -d: -f1) |
while read realdev; do
  # it is not sensible to set an additional address on the loopback device
  if [ "$realdev" = "lo" ]; then
    continue
  fi
  echo -e "Setting IP address on device ${COLOR_BLUE}$realdev${COLOR_NORMAL}:"
  # try ip addresses on this device until an unused is found
  address_minor=1
  dev_configured=0
  while [ $dev_configured -eq 0 ]; do
    address="${address_prefix}${address_major}.${address_minor}"
    ## a valid source address is required for some hosts (mainly firewalls)
    ## to answer ping requests
    ## normally use a .1 address, but not when we try to ping the .1
    #if [ $address_minor -eq 1 ]; then
    #  srcaddress="${address_prefix}${address_major}.2"
    #else
    #  srcaddress="${address_prefix}${address_major}.1"
    #fi
    
    echo -n "  Trying address $address ... "
    # is the address in use ?
    arpreplies=`arping -c $ping_tries -I $realdev -D $address \
       | grep "reply from"` || true
    if [ -z "$arpreplies" ]; then
      # good, address is not in use
      echo -e "${COLOR_GREEN}set${COLOR_NORMAL}"
      # write a complete entry into /etc/network/interfaces
      mac_address=`ip link show dev $realdev | tail -n 1 | awk '{ print $2; }'`
      devnum=`expr "$realdev" : "eth\\(.*\\)"` || true
      devlabel="autocfg${devnum}"
      devlabel="eth${devnum}"
      if [ $net_ifaces_saved -eq 0 ]; then
        cp /etc/network/interfaces /etc/network/interfaces.orig
        net_ifaces_saved=1
      fi
      cat >> /etc/network/interfaces <<EOF

auto $realdev
iface $realdev inet scripted
	method scripted
        address ${address}/${address_netmask}
        mac $mac_address
        device $devlabel
EOF

      # and start the interface using normal ifup
      ifup $realdev

      dev_configured=1
    else
      echo "used by `echo $arpreplies | cut -d' ' -f5,5`"
      # in use, go on to the next one
      # is this the last address in this /24 subnet ?
      if [ $address_minor -lt 254 ]; then
        address_minor=`expr $address_minor + 1`
      else
        # if yes, go on to the next network
        address_minor=1
        address_major=`expr $address_major + 1`
      fi
    fi
  done
  
  if [ $dev_configured -eq 1 ]; then
    address_major=`expr $address_major + 1`
  fi
done

exit 0
