#!/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
    }
}

# ---------------------------------------------------------------------------
get_current_instance()
# ---------------------------------------------------------------------------
# Get Network Mode Flag
#
{
    echo "`gconftool-2 --get /apps/autodisconnect/current_instance`"
}

# ---------------------------------------------------------------------------
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
}

# ---------------------------------------------------------------------------
disable_quit()
# ---------------------------------------------------------------------------
# Disabled if it is a reconnection
#
{
    Pid=`gconftool-2 -g /apps/autodisconnect/current_instance`
    test -z "`ps | egrep \"[ ]*$Pid.*/opt/autodisconnect/autodisconnect-network\"`"
}

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

    # Log
    logentry "<Start@>"

    # Quit if it is a reconnection
    if `disable_quit`; then
        return 0
    fi

    # 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 back network mode to 2G if allowed
        [[ $g_use_2g = true ]] && {
            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 "<Stop@>"
fi

exit 0

