#!/bin/ash

#
# App : AutoDisconnect
# Url : https://garage.maemo.org/projects/autodisconnect/
# Version: 0.4
# 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`][$$][Network] $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

        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

            # Check if any call is active
            dbus-send --system --type=method_call \
                --dest=com.nokia.phone.net /com/nokia/phone/net \
                Phone.Net.set_selected_radio_access_technology byte:2

            # Wait 3 seconds for the interface to be again available
            sleep 3

            logentry "switched to 3G"  

        fi

    fi
}

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

    # Log
    logentry "<@Start>"

    # Get the interface is not possible. See https://bugs.maemo.org/show_bug.cgi?id=7733
    # So it applies here for every interface

    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 network mode to 3G if allowed
        [[ "$g_use_2g" = true ]] && {
            switch_3G
        }
    fi 
}

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

# Start AutoDisconnect

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

if [[ "$g_running_options" != 0 ]]; then
    main
    logentry "<@Stop>"
fi

exit 0

