#!/bin/sh
#
# net -  program to switch network profiles 
# version 0.4
#
# Copyright (C) 2005 hdluc@yahoo.com
#
# 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.
#
######################################################################
# variables
APPNAME="Hd Network Switcher"
USER=zaurus
ID=`id -u`
OPIE_SH=/opt/QtPalmtop/bin/opie-sh
SUDO=/usr/bin/sudo
CONFIGDIR=/etc/sysconfig/netconf
HOTPLUG=/etc/hotplug
DEFCONF=$HOTPLUG/net.conf
CARD=eth0
CONFIG=$1

# functions
display_error () 
{
    echo "ERROR: $1"
    if [ "$ID" = "0" ]; then 
        su $USER -c "$OPIE_SH -m -t \"$APPNAME\" -M \"$1\" -g -e >/dev/null 2>&1"
    else
        $OPIE_SH -m -t "$APPNAME" -M "$1" -g -e >/dev/null 2>&1
    fi
}

# start here
if [ "$CONFIG" = "-gui" ]; then
    if [ ! -x $OPIE_SH ]
      then
          echo "ERROR: opie-sh not found."
          exit 1
    fi
else
    OPIE_SH=/bin/false
fi

echo " $APPNAME"
if [ "$ID" != "0" ]; then 
    if [ ! -x $SUDO ]; then
        display_error "You have to be root to run this or install sudo."
        exit 1
    else
        echo "WARNING: using sudo (make sure $USER has NOPASSWD for ifconfig and dhcpcd)"

    fi
fi

if [ "$CONFIG" = "resume" ] || [ "$CONFIG" = "-resume" ]; then
    if [ "$ID" = "0" ]; then 
        rmmod usb-monitor
        insmod usb-monitor
    else
        sudo rmmod usb-monitor
        sudo insmod usb-monitor
    fi
    echo "network resumed"
    exit 0
fi

ETH=`ifconfig -a|grep $CARD`
if [ "$ETH" = "" ]; then 
    display_error "network interface $CARD not found! (Plug cable in?)"
    exit 1
else
    echo "... checking network interface [OK]"
fi

if [ "$CONFIG" = "" ]; then 
    PNAME=`basename $0`
    echo "syntax: $PNAME <configname> or $PNAME -gui"
    exit 1
fi

if [ "$CONFIG" = "refresh" ] || [ "$CONFIG" = "-refresh" ]; then 
    if [ "$ID" = "0" ]; then 
        su $USER -c "qcop QPE/Network \"up()\""
    else
        qcop QPE/Network "up()"
    fi
    echo "refreshed network status"
    exit 0
fi

if [ "$CONFIG" = "-gui" ]; then 
    M="Please select a network profile:" 
    if [ "$ID" = "0" ]; then 
       CONFIG=`su $USER -c "ls -1 $CONFIGDIR | cut -d. -f1 | $OPIE_SH -i -t \"$APPNAME\" -L \"$M\" -l -g 2>/dev/null"` 
    else
       CONFIG=`ls -1 $CONFIGDIR | cut -d. -f1 | $OPIE_SH -i -t "$APPNAME" -L "$M" -l -g 2>/dev/null` 
    fi
    if [ "$CONFIG" = "" ]; then 
        display_error "config ($CONFIG) is invalid!"
        exit 1
    fi
fi
if [ -f $CONFIGDIR/$CONFIG.conf ]; then
    echo "... loading network profile $CONFIG.conf from $CONFIGDIR [OK]"
    . $CONFIGDIR/$CONFIG.conf
else
    display_error "$CONFIGDIR/$CONFIG.conf not found!"
    exit 1
fi

if [ -f $HOTPLUG/net.func ]; then
    echo "... loading library net.func [OK]"
    . $HOTPLUG/net.func
else
    display_error "$HOTPLUG/net.func is missing!"
    exit 1
fi

echo "... resetting interface $CARD [OK]"
net_if_down
net_if_up
rm $DEFCONF 2>/dev/null
ln -s $CONFIGDIR/$CONFIG.conf $DEFCONF 2>/dev/null
IP=`ifconfig -a|grep inet|cut -d: -f2|awk '{print $1}'`
RES1="The network has been configured for [$CONFIG] on interface $CARD"
RES2="Your IP address is $IP"
RESULT="<h1>$APPNAME</h1><hr><h3>$RES1<br>$RES2</h3><hr>"
echo $RES1
echo $RES2
if [ "$ID" = "0" ]; then 
    su $USER -c "$OPIE_SH -m -t \"$APPNAME\" -M \"$RESULT\" -g >/dev/null 2>&1"
else
    $OPIE_SH -m -t "$APPNAME" -M "$RESULT" -g >/dev/null 2>&1
fi
