#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_staticip,v 1.6 2005/04/11 14:14:39 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005           SUSE GmbH, Nuernberg, Germany
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Authors: Marcel Ritter, Juergen Henge-Ernst
#
#############################################################################
#
# make_staticip
# Configures static IP adresses from current DHCP setup
#
# TODO: Does not work in SLES8 (and above environments)
# TODO: Let us check, if we need this program any longer
#
# Sections: --
# Tags:     --

test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir

while [ $# -gt 0 ]
do
   case $1 in
       -h | --help | -? ) usage
                          exit 1
                          ;;
       -fqhn ) export fqhn=$2; shift
            ;;

   esac
   shift
done   

. $alice_dir/lib/alicerc

WELCOME

NUM=0
DEFAULT_GW_SET=0
HOSTNAME_SET=0
CHANGE=0

route_conf=/etc/route.conf

# remove the dhcpcd.rc.config, so the new RC_CONFIG_SET will not find the settings there
dhcpcd_conf=/etc/rc.config.d/dhcpcd.rc.config

NETCONFIG_INT=""

test -f ${route_conf} && mv ${route_conf} ${route_conf}.aliceold
test -f ${dhcpcd_conf} && mv ${dhcpcd_conf} ${dhcpcd_conf}.aliceold

for netdev in eth0 eth1 eth2 eth3; do

  #
  # location of this file changed from one release to the next,
  # to make it more future-proof, let's search /var for it
  #

  for f in $(find /var -name dhcpcd-$netdev.info); do
	echo -n "Searching dev $netdev ... "

	if [ -f $f ]; then
		echo "found"
		#
		# this file was created by the DHCP client daemon
		#
		. $f

		#
		# Change network setup in /etc/rc.config
		#
		RC_CONFIG_SET IPADDR_$NUM "$IPADDR"
		RC_CONFIG_SET NETDEV_$NUM "$netdev"
		RC_CONFIG_SET IFCONFIG_$NUM "$IPADDR broadcast $BROADCAST netmask $NETMASK up"
		NETCONFIG_INT="$NETCONFIG_INT _$NUM"
		if [ "$HOSTNAME_SET" = "0" ]; then
			RC_CONFIG_SET FQHOSTNAME "$HOSTNAME.$DOMAIN"
			HOSTNAME_SET=1
		fi

		echo "$NETWORK	0.0.0.0	$NETMASK	$netdev" >> ${route_conf}
		NUM=$[ $NUM +1 ];

		CHANGE=1
	fi
  done
done

#
# apply changes only if there are correct
# values supplied by the DHCP server
#
if [ "$CHANGE" = "1" ]; then
	if [ -n $GATEWAY ]; then
		echo "default $GATEWAY" >> ${route_conf}
	fi

	RC_CONFIG_SET NETCONFIG "$NETCONFIG_INT"
fi

GOOD_BYE 
