#!/bin/sh

fc2_dist_func() {
    if ! [ -e /sbin/dhclient -a -e /sbin/iwconfig -a -e /sbin/ifconfig ] ; then
        # Required program(s) not installed
        return 7
    fi
    if ! [ -e "/etc/sysconfig/network-scripts/ifcfg-$1" ] ; then
        touch /etc/sysconfig/network-scripts/ifcfg-$1
    fi
    /sbin/dhclient -r $1
    /sbin/iwconfig $1 essid $2
    /sbin/ifconfig $1 up
    /sbin/dhclient -1 $1

    ret=$?
    echo "EXIT $ret"
    exit $ret
}

freebsd_dist_func() {
    if ! [ -e /sbin/dhclient -a -e /sbin/ifconfig ] ; then
        echo "EXIT 7"
        echo "Required program(s) not installed"
        exit 7
    fi
    /sbin/dhclient -r $1
    /sbin/ifconfig $1 ssid $2
    /sbin/ifconfig $1 up
    /sbin/dhclient -1 $1

    # dhclient returns 0 even if it fails!?
    ret=$?
    echo "EXIT $ret"
    exit $ret
}

mdk10_0_dist_func() {
    if ! [ -e /sbin/dhclient -a -e /sbin/iwconfig -a -e /sbin/ifconfig ] ; then
        # Required program(s) not installed
        return 7
    fi
    if ! [ -e "/etc/sysconfig/network-scripts/ifcfg-$1" ] ; then
        touch /etc/sysconfig/network-scripts/ifcfg-$1
    fi
    if [ -e "/sbin/ifplugd" ] ; then
        /sbin/ifplugd -k -i $1
    fi
    /sbin/dhclient -r $1
    /sbin/iwconfig $1 essid $2
    /sbin/ifconfig $1 up
    /sbin/dhclient -1 $1

    ret=$?
    echo "EXIT $ret"
    exit $ret
}

generic_dist_func() {
    DIR_LIST="/sbin /usr/sbin /usr/local/sbin"
    
    prog_dhcp="none"
    prog_dhcp_r="none"
    for dir in $DIR_LIST; do
        if [ -e $dir/dhclient ]; then
            prog_dhcp="$dir/dhclient -1"
            prog_dhcp_r="$dir/dhclient -r"
            break
        fi
        if [ -e $dir/dhcpcd ]; then
            prog_dhcp="$dir/dhcpcd -c /bin/true"
            prog_dhcp_r="$dir/dhcpcd -k"
            break
        fi
    done
    
    prog_ifconfig="none"
    for dir in $DIR_LIST; do
        if [ -e $dir/ifconfig ]; then
            prog_ifconfig="$dir/ifconfig"
            break
        fi
    done

    prog_iwconfig="none"
    for dir in $DIR_LIST; do
        if [ -e $dir/iwconfig ]; then
            prog_iwconfig="$dir/iwconfig"
            break
        fi
    done
    if [ "$prog_dhcp" = "none" -o "$prog_ifconfig" = "none"\
         -o "$prog_iwconfig" = "none" ]; then
        echo "EXIT 7"
        echo "Required program(s) not installed"
        exit 7
    fi

    if [ -e /etc/sysconfig/network-scripts\
         -a ! -e "/etc/sysconfig/network-scripts/ifcfg-$1" ] ; then
        touch /etc/sysconfig/network-scripts/ifcfg-$1
    fi

    $prog_dhcp_r $1
    $prog_iwconfig $1 essid $2
    $prog_ifconfig $1 up
    $prog_dhcp $1

    ret=$?
    echo "EXIT $ret"
    exit $ret
}

if [ -z "$1" -o  -z "$2" ] ; then
    echo "EXIT 5"
    echo "Wrong arguments"
    exit 5
fi

DISTFUNC=notsupported
DIST=`uname`
if [ "$DIST" = "FreeBSD" ] ; then
    DISTFUNC=freebsd_dist_func
elif [ -e "/etc/mandrake-release" ] ; then
    if grep -q "Mandrake Linux release 10.0" /etc/mandrake-release; then
        DISTFUNC=mdk10_0_dist_func
    fi
elif [ -e "/etc/fedora-release" ] ; then
    DIST=`cat /etc/fedora-release`
    if [ "$DIST" = "Fedora Core release 2 (Tettnang)" ] ; then
        DISTFUNC=fc2_dist_func
    fi
elif [ -e "/etc/redhat-release" ] ; then
    DISTFUNC=generic_dist_func
else
    DISTFUNC=generic_dist_func
fi

if [ "$DISTFUNC" = "generic_dist_func" ] ; then
    generic_dist_func $1 $2
else
    $DISTFUNC $1 $2
    if [ $? -eq 7 ]; then
        generic_dist_func $1 $2
    fi
fi
