#!/bin/sh

# ---------------------------------------------------------------------------
# PARAMETERS
# ---------------------------------------------------------------------------

# Check if AutoDisconnect is enabled
[[ `gconftool-2 -g /apps/autodisconnect/param_enabled` = false ]] && exit 0

# Waiting for possible closing connections
sleep 10

# How often to test for inactivity
g_samplerate="`gconftool-2 -g /apps/autodisconnect/param_interval`"
g_samplerate=$((g_samplerate*60))

# Enable logging
g_logging="true"

# Notifications
g_notifications="`gconftool-2 -g /apps/autodisconnect/param_notifications`"

# Number of bytes allowed to be received without disconnect
g_dontcountbytespermin=`gconftool-2 -g /apps/autodisconnect/param_min_bytes`

# Interfaces to check
g_enable=
[[ `gconftool-2 -g /apps/autodisconnect/param_connection_wlan` = true ]] && g_enable="wlan "$g_enable
[[ `gconftool-2 -g /apps/autodisconnect/param_connection_gprs` = true ]] && g_enable="gprs "$g_enable

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

# ---------------------------------------------------------------------------
# DON'T MODIFY ANYTHING BELOW THIS POINT
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# GLOBALS
# ---------------------------------------------------------------------------

# Number of bytes to ignore for each time period
g_dontcountbytes=$((g_dontcountbytespermin*g_samplerate))

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

# ---------------------------------------------------------------------------
init()
# ---------------------------------------------------------------------------
{
    # 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

    # Don't let log file get too big
    if [[ -w "$logfile" ]]; then
        size=`stat $logfile | grep Size | awk '{ print $2; }'`
        [[ "$size" -gt 50000 ]] && :>$logfile
    fi
}

# ---------------------------------------------------------------------------
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`] $1" >> $logfile
    }
}

# ---------------------------------------------------------------------------
osnotify()
# ---------------------------------------------------------------------------
# Send a string to this function to generate a system popup
#
# Arg 1 - The text to display
{
    [[ "$g_notifications" == "true" ]] && {
		/usr/bin/dbus-send --type=method_call \
			--dest=org.freedesktop.Notifications \
			/org/freedesktop/Notifications \
			org.freedesktop.Notifications.SystemNoteInfoprint \
			string:"$1"
	}
}

# ---------------------------------------------------------------------------
disconnect()
# ---------------------------------------------------------------------------
# Disconnect current connection
#
# No args
{
    dbus-send --system --dest=com.nokia.icd \
        /com/nokia/icd_ui \
        com.nokia.icd_ui.disconnect \
        boolean:true
}

# ---------------------------------------------------------------------------
disable_quit()
# ---------------------------------------------------------------------------
{
    # Disable for some interfaces
	if echo "$g_enable" | grep -qs "$short_interface" 2>/dev/null; then
		osnotify "Auto-disconnect enabled for $short_interface"
		logentry "Auto-disconnect enabled for $short_interface"
	else
		#osnotify "Auto-disconnect disabled for $short_interface"
		logentry "Auto-disconnect disabled for $short_interface"
		exit 0
	fi
}

# ---------------------------------------------------------------------------
main()
# ---------------------------------------------------------------------------
# Main entry point
{
    init

    # Log monitor startup to file & notify user via popup
    logentry "Auto-disconnect starting"
    	
	interface=$IFACE
	
	if [ -z "$interface" ]; then
		# gprs0 generally
		interface=`grep gprs /proc/net/route | tail -1 | cut -f1`
	fi

	short_interface=`echo "$interface" | tr -d 0-9`

    # Quit if this interface doesn't have to be checked
    disable_quit
	
	#interface=`grep -v tun /proc/net/route | tail -1 | cut -f1`
	packets_before=`grep $interface /proc/net/dev | awk '{ print $2 }'`
	
	#PERFORM MAIN MONITORING LOOPS
	while true;
	do
		sleep $g_samplerate

		packets_after=`grep $interface /proc/net/dev | awk '{ print $2 }'`

		if [ "$packets_after" -lt "$((packets_before+g_dontcountbytes))" ]; then
			disconnect
			osnotify "Connection $interface closed due to inactivity"
			logentry "$interface closed. Bytes received : $((packets_after-packets_before))"
			exit 0
		else			
			logentry "$interface: Bytes received : $((packets_after-packets_before))"
			packets_before=$packets_after   
		fi

	done
	
}

# Start
main

exit 0


