#!/bin/ash

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

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

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

    # Auto 2G Mode
    g_use_2g="`gconftool-2 -g /apps/autodisconnect/param_use_2g`"

    # 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

            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
}

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

    # Get interface
    interface="$ICD_CONNECTION_TYPE"

    # Log
    logentry "<post-down-start>"	    

    #charging_status="`lshal -u '/org/freedesktop/Hal/devices/bme' | grep battery.rechargeable.is_charging | awk '{print $3}'`"
    #if [[ $g_running_options != 2 -o $charging_status = false ]]; then
        
        # Switch back network mode to 2G if allowed
        [[ "$g_idle_network_mode_type" != "none" -a "$interface" = "GPRS" ]] && {
            switch_2G
        }

    #fi 
}

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

# Start AutoDisconnect

g_running_options="`gconftool-2 -g /apps/autodisconnect/param_running_options`"

#if [[ "$g_running_options" != 0 ]]; then
    main
    logentry "<post-down-stop>"
#fi

exit 0

