#!/bin/bash
#
# /etc/rc.d/init.d/net - start/stop network interfaces
#
# 2007-02-18 Written by GROG! <Uber[dot]GROG[at]Gmail[dot]Com>
#
# Run via symlink, eg net.eth0 -> net
#                  eg net.office.wlan0 -> net
#                  eg net.home.eth0 -> net
#
# conf files need to be named following the pattern:
#      string.interface.conf
# where string can be a descriptive text string that DOES NOT contain any periods ('.').

#set -x # TESTING

# Comment out the following exit line to enable this script.
#exit 0

# Source function library.
. /etc/rc.d/init.d/functions

IFC=/sbin/ifconfig
IWC=/sbin/iwconfig
ROUTE=/sbin/route
DHCPD=/sbin/dhcpcd
CONFIGDIR=/etc/sysconfig/netconfig/

case $(/bin/uname -a) in
    *2.6.*) CARD=/sbin/pccardctl ;;
         *) CARD=/sbin/cardctl ;;
esac

IF=${0##*/}
IF=${IF#*.}
CONFIGFILE=$CONFIGDIR/$IF.conf
IF=${IF##*.}

if [ -f $CONFIGFILE ]; then
    . $CONFIGFILE || exit 1
else
    . $CONFIGDIR/none.conf || exit 1
fi

case "$1" in

    start) if [ -n "$($IFC | grep $IF)" ]; then
                msg "ERROR - Interface $IF already running"
                exit 1
           else
                if ! $IFC $IF >/dev/null 2>&1; then
                    $CARD eject && sleep 1 && $CARD insert && sleep 2

                    if ! $IFC $IF >/dev/null 2>&1; then
                        msg "ERROR - Interface $IF unavailable"
                        exit 1
                    fi
                fi
           fi

           msg -n "Starting $IF: "

           if $IFC $IF $IPADDR up; then
               case "$WIRELESS" in y*|Y*)
                    [ -n "$KEY" ] && KEY="key $KEY"
                    [ -n "$CHANNEL" ] && CHANNEL="channel $CHANNEL"

                    $IWC $IF essid $ESSID mode $MODE $KEY $CHANNEL power on || exit 1
               esac

               case "$DHCP" in
                   y|Y) $DHCPD -i $IF || exit 1 ;;
                     *) $ROUTE add default gw $GATEWAY || exit 1
                        echo "nameserver ${NAMESERVER:-$GATEWAY}" > /etc/resolv.conf
                        ;;
               esac

               if $IFC $IF >/dev/null 2>&1; then
                    msg OK
                    [ -x /etc/rc.d/init.d/ssh ] && /etc/rc.d/init.d/ssh start
               fi
           else
               $CARD eject
               msg "ERROR - error starting interface"
               exit 1
           fi

           ;;

     stop) if [ -z "$($IFC | grep $IF)" ]; then
                msg "ERROR - Interface $IF not running"
                exit 1
           else
                msg -n "Stopping $IF: "

                if ! $IFC $IF down; then
                    msg "ERROR - Stopping interface $IF failed"
                    exit 1
                else
                    if $CARD eject; then
                        case "$DHCP" in y|Y)
                            killall dhcpcd
                        esac

                        msg OK

                        [ -x /etc/rc.d/init.d/ssh ] && /etc/rc.d/init.d/ssh stop
                    else
                        exit 1
                    fi
               fi
           fi

	       msg
           ;;

    restart) $0 stop
             sleep 1
             $0 start
             ;;

    *) echo "Usage: $0 (start|stop|restart)"
       exit 1
       ;;

esac

exit 0

