#!/bin/ash

#
# App : AutoDisconnect
# Url : https://garage.maemo.org/projects/autodisconnect/
# Version: 0.4.8
# Author: Aymeric Brisse <aymeric.brisse@gmail.com>
# License: GNU General Public License
#

# ---------------------------------------------------------------------------
# FUNCTIONS
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
readparameters()
# ---------------------------------------------------------------------------
{
    # Enable logging
    g_logging=true

    # Auto 2G Mode
    g_idle_network_mode_type="`gconftool-2 -g /apps/autodisconnect/param_idle_network_mode_type`"
    g_idle_network_mode_duration="`gconftool-2 -g /apps/autodisconnect/param_idle_network_mode_duration`"

    # Where to store the logfile. This is cleared when it goes over 50k.
    logfile=/var/log/autodisconnect.log
}

# ---------------------------------------------------------------------------
logentry()
# ---------------------------------------------------------------------------
# Send a string to this function to append it to the log file
#
# Arg 1 - The text to log
{
    [[ "$g_logging" = true ]] && {
        echo -e "[`date +%T`][$$][$interface] $1" >> $logfile
    }
}

# ---------------------------------------------------------------------------
switch_3G()
# ---------------------------------------------------------------------------
# Switch network mode to 3G
#
# No args
{
    # Allow dbus to work
    [[ -e /tmp/dbus-info ]] && eval `cat /tmp/dbus-info`

    export DBUS_SESSION_BUS_ADDRESS \
           DBUS_SESSION_BUS_PID \
           DBUS_SESSION_BUS_WINDOWID

    if [[ "`dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_selected_radio_access_technology | awk '/b/ {print $2}'`" -eq 1 ]]; then

        # Check if any call is active
        if [[ -z "`dbus-send --type=method_call --print-reply --dest=org.freedesktop.Telepathy.ConnectionManager.ring /org/freedesktop/Telepathy/Connection/ring/tel/ring org.freedesktop.DBus.Properties.Get string:org.freedesktop.Telepathy.Connection.Interface.Requests string:Channels | grep /org/freedesktop/Telepathy/Connection/ring/tel/ring/`" ]]; then

            if [[ "$g_idle_network_mode_type" = "3g" ]]; then
                network_mode_id=2 # 3G
            else
                network_mode_id=0 # Dual Mode
            fi
            
            dbus-send --system --type=method_call \
                --dest=com.nokia.phone.net /com/nokia/phone/net \
                Phone.Net.set_selected_radio_access_technology byte:$network_mode_id

            # Wait for the interface to be again available
            sleep $g_idle_network_mode_duration

            logentry "switched to $g_idle_network_mode_type mode"  

        fi

    fi
}

# ---------------------------------------------------------------------------
main()
# ---------------------------------------------------------------------------
# Main entry point
#
{
    # Read parameters
    readparameters

    # Get interface (IPV6 support)
    interface="`echo $ICD_CONNECTION_TYPE | cut -c0-4`"

    # Log
    logentry "<pre-if-up-start>"
        
    # Switch network mode to 3G if allowed
    [[ "$g_idle_network_mode_type" != "none" -a "$interface" = "GPRS" ]] && {
        switch_3G
    }
}

# ---------------------------------------------------------------------------
# RUNTIME
# ---------------------------------------------------------------------------

# Start AutoDisconnect

main
logentry "<pre-if-up-stop>"

exit 0

