#!/bin/bash
#
# chkconfig: 345 04 96
# description:  Noninteractive HW detection and configuration \
#               in particular configure network devices. \
#               Used on LiveCD systems.
#
# Urs Beyerle
#

### -----------------------------------------------------------
### definitions
### -----------------------------------------------------------

# dir of mounted /$MOUNTDIR/live
MOUNTDIR=livecd

# source functions
. /$MOUNTDIR/live/liblinuxlive
. /etc/init.d/functions

# do not run, if we change runlevels
touch /var/lock/subsys/kudzu-auto

### -----------------------------------------------------------
### Get boot parameters
### -----------------------------------------------------------

# do not start network
NONET=$( cmdline_parameter nonet )

# static IP
IPADDR=$( cmdline_value ip )

# gateway
GATEWAY=$( cmdline_value gw )

# netmask
NETMASK=$( cmdline_value netmask )

# start WLAN network on boot
WLAN=$( cmdline_parameter wlan )

# do not apply any fixes (for debugging)
NOFIX=$( cmdline_parameter nofix )


### -----------------------------------------------------------
### Function start_kudzu_auto
### -----------------------------------------------------------

start_kudzu_auto() {

    ### run kudzu in quiet mode
    action $"Hardware auto-detection, please wait... " /usr/sbin/kudzu -q


    ### never start on boot the following network devices (wifi0)
    if [ ! $NOFIX ]; then
	for iface in wifi0; do
	    for file in networking/profiles/default/ifcfg \
	        	networking/devices/ifcfg \
		        network-scripts/ifcfg; do
	      if [ -e /etc/sysconfig/${file}-${iface} ]; then
		  sed -i "s/ONBOOT=.*/ONBOOT=no/" /etc/sysconfig/${file}-${iface}
	      fi
	    done
	done
    fi


    ### do not start network devices, if NONET is defined
    if [ $NONET ]; then
        # find all network devices (exclude loopback device)
	config_files=$( find /etc/sysconfig -name ifcfg-* 2>/dev/null | grep -v ifcfg-lo )
	for file in $config_files; do
	    sed -i "s/ONBOOT=.*/ONBOOT=no/" $file
	done
    fi
    

    ### define static IP and gateway, netmask
    if [ $IPADDR ]; then    
	if [ ! $GATEWAY ]; then
            # default gatway x.y.z.1
	    GATEWAY=$( echo $IPADDR | awk -F"." '{ print $1"."$2"."$3".1" }' )
	    if [ ! $NETMASK ]; then
		NETMASK=255.255.255.0
	    fi
	fi
        # create config files for eth0
	for iface in eth0; do
	    for file in networking/profiles/default/ifcfg \
	        	networking/devices/ifcfg \
		        network-scripts/ifcfg; do
	      sed -i "s/BOOTPROTO=.*/BOOTPROTO=none/" /etc/sysconfig/${file}-${iface}
	      echo "IPADDR=$IPADDR" > /etc/sysconfig/${file}-${iface}
	      echo "NETMASK=$NETMASK" > /etc/sysconfig/${file}-${iface}
	      echo "GATEWAY=$GATEWAY" > /etc/sysconfig/${file}-${iface}
	    done
	done
    fi
    
    
    ### activate Atheros WLAN
    lspci | grep -q Ethernet.*Atheros
    if [ "$?" = "0" ]; then
        # bring ath0 up
	ifconfig ath0 up > /dev/null 2>&1
        # replace wifi0 with ath0
	for file in networking/profiles/default/ifcfg \
	            networking/devices/ifcfg \
	            network-scripts/ifcfg; do
	  if [ -e /etc/sysconfig/${file}-wifi0 ]; then
	      mv /etc/sysconfig/${file}-wifi0 /etc/sysconfig/${file}-ath0
	      sed -i "s/wifi0/ath0/" /etc/sysconfig/${file}-ath0
	      grep -q Wireless /etc/sysconfig/${file}-ath0
	      [ "$?" != "0" ] && echo "TYPE=Wireless" >> /etc/sysconfig/${file}-ath0
	  fi
	done
    fi
    
    
    ### configure iwl4965 Intel WLAN Chip
    lspci | grep -q Wireless.*4965
    if [ "$?" = "0" ]; then
	find /lib/modules/ 2>&1 | grep -q iwl4965.ko
	if [ "$?" = "0" ]; then
	    grep -q iwl4965 /etc/modprobe.conf
	    if [ "$?" != "0" ]; then
		echo "alias wlan0 iwl4965" >> /etc/modprobe.conf
	    fi
	    if [ ! -e /etc/sysconfig/network-scripts/ifcfg-wlan0 ]; then
		cat > /etc/sysconfig/network-scripts/ifcfg-wlan0 <<EOF
TYPE=Wireless
DEVICE=wlan0
BOOTPROTO=dhcp
NETMASK=
DHCP_HOSTNAME=
IPADDR=
DOMAIN=
ONBOOT=no
USERCTL=no
IPV6INIT=no
PEERDNS=yes
ESSID=
CHANNEL=6
MODE=Managed
RATE=Auto
EOF
                ln /etc/sysconfig/network-scripts/ifcfg-wlan0 /etc/sysconfig/networking/devices/ifcfg-wlan0
		ln /etc/sysconfig/network-scripts/ifcfg-wlan0 /etc/sysconfig/networking/profiles/default/ifcfg-wlan0 
	    fi
	fi
    fi


    ### do not start WLAN devices on boot
    if [ ! $WLAN ]; then
        # find all network devices (exclude loopback device)
	config_files=$( find /etc/sysconfig -name ifcfg-* 2>/dev/null | grep -v ifcfg-lo )
	for file in $config_files; do
            # WLAN device?
	    grep -q TYPE=Wireless $file
	    if [ "$?" = "0" ]; then
		sed -i "s/ONBOOT=.*/ONBOOT=no/" $file
	    fi
	done
    fi     

}


### -----------------------------------------------------------
### Main
### -----------------------------------------------------------

case "$1" in
 start)
     start_kudzu_auto
     ;;

 stop)
     ;;

 *)
     echo $"Usage: $0 {start|stop}"
     exit 1
     ;;

esac