#!/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`"

    # 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_2G()
# ---------------------------------------------------------------------------
# Switch network mode to 2G
#
# 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}'`" -ne 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

            # 2G
            dbus-send --system --type=method_call \
                --dest=com.nokia.phone.net /com/nokia/phone/net \
                Phone.Net.set_selected_radio_access_technology byte:1

            logentry "switched to 2G"

        fi

    fi
}

# ---------------------------------------------------------------------------
no_gprs_interface_is_running()
# ---------------------------------------------------------------------------
# Returns true if no GPRS interface is running
#
{
    test -z "`grep gprs /proc/net/route`"
}

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

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

    # Log
    logentry "<post-down-start>"	    
        
    # Switch back network mode to 2G if allowed and if no other GPRS connection is active
    [[ "$g_idle_network_mode_type" != "none" -a "$interface" = "GPRS" ]] && `no_gprs_interface_is_running` && {
        switch_2G
    }
}

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

# Start AutoDisconnect

main
logentry "<post-down-stop>"

exit 0

