#! /bin/sh
#
# Startup script for Internet Setup
# Author: Walter Guerra
#         Andre Rodrigues

delete_default_routes() {
    route del default
    while [ $? -eq 0 ]; do
        route del default 
    done
}

add_dns() {
    echo "# Automatically generated by pc-connectivity-manager" > /etc/resolv.conf
    while [ "$1" != "" ]; do
        echo "nameserver $1" >> /etc/resolv.conf
        
        shift
    done
}

start_internet() {
    # usage:
    # nm-internet-setup start iface router dns1 dns2 ...
    
     
    # if iface is wlan, just kill udhcpc on the interface and the system runs it again
    if [ $1 == "Wlan" ]
    then
        if [ -e /tmp/pc-connectivity-manager.route ]
        then    
        
            # Delete all default routes
            delete_default_routes
            
            # check if there is a connected wlan
            dbus-send --system --reply-timeout=1000 --print-reply --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.get_ipinfo >/dev/null 2>/dev/null
             if [ $? -eq 0 ] # If there is a wlan
            then
                # Get the network ID
                NETWORK_ID=`dbus-send --system --reply-timeout=1000 --print-reply --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.get_ipinfo | head -n 2 | tail -n 1 | cut -d\" -f2`
                
                # Disconnect
                dbus-send --system --dest=com.nokia.icd /com/nokia/icd_ui com.nokia.icd_ui.disconnect boolean:true
                
                sleep 2
                dbus-send --type=method_call --print-reply --system --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:$NETWORK_ID uint32:0 > /dev/null 2>/dev/null
            fi
            
            rm /tmp/pc-connectivity-manager.route
        fi

    else
        
        # Delete all default routes
        delete_default_routes
                     
        # if iface is usb or bluetooth, connect to dummy_iap if it has no wlan connection 
        if [ $1 != "Wlan-adhoc" ] 
        then 
            # check if there is a connected wlan, if not, connect to the dummy IAP
            dbus-send --system --reply-timeout=1000 --print-reply --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.get_ipinfo >/dev/null 2>/dev/null
            if [ $? -gt 0 ] # There is not a connected wlan
            then
                dbus-send --type=method_call --print-reply --system --dest=com.nokia.icd /com/nokia/icd com.nokia.icd.connect string:dummy_iap uint32:0 > /dev/null 2>/dev/null
            fi
    
        fi
        
        # If not, add the default route ...
        route add default gw $2
   
        echo $2 > /tmp/pc-connectivity-manager.route
             
        # Add the dns
        shift 
        shift
        add_dns $@        
    fi     
}

stop_internet() {
    echo "stop"
}

share_internet() {
    # usage:
    # nm-internet-setup start_share out_iface in_iface [in_iface]

    # load iptable_nat modules
    if [ `lsmod | grep iptable_nat | wc -l` -eq 0 ] 
    then
        insmod /lib/modules/2.6.28-maemo2/net/netfilter/nf_conntrack.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/nf_conntrack_ipv4.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/nf_nat.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/nf_nat_snmp_basic.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/iptable_nat.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/ipt_MASQUERADE.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/ipt_REDIRECT.ko
        insmod /lib/modules/2.6.28-maemo2/net/ipv4/netfilter/ipt_NETMAP.ko
    fi
    
    iptables --flush
    iptables --flush -t nat
    echo 1 > /proc/sys/net/ipv4/ip_forward
	
    iptables -P FORWARD ACCEPT
    
    # internet connection
    iptables --table nat --append POSTROUTING --out-interface $1 -j MASQUERADE

    # share internet    
    shift
    for i in $@;
    do
        iptables --append FORWARD --in-interface $i -j ACCEPT 
    done
	  
}

stop_share() {
    iptables --flush
    iptables --flush -t nat
    echo 0 > /proc/sys/net/ipv4/ip_forward
}

case $1 in
start)
    shift
    start_internet $@
    ;;
stop)
    stop_internet
    ;;
start_share)
    shift
    share_internet $@
    ;;
stop_share)
    stop_share
    ;;
esac



