#!/bin/sh

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

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

# ---------------------------------------------------------------------------
readparameters()
# ---------------------------------------------------------------------------
{
    # How often to test for inactivity (min)
    g_samplerate="`gconftool-2 -g /apps/autodisconnect/param_bluetooth_interval`"

    # Enable logging
    g_logging="true"

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

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

    # Convert min interval to sec interval
    g_samplerate=$((g_samplerate*60))
}

# ---------------------------------------------------------------------------
init()
# ---------------------------------------------------------------------------
{
    # Log monitor startup to file
    logentry "<Start>"

    # 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 +%T`][$$][Bluetooth] $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 --type=method_call \
        --dest=org.bluez $(dbus-send --system --print-reply \
        --dest=org.bluez / org.bluez.Manager.ListAdapters | \
        awk -F'"' '/at/ {print $2}') org.bluez.Adapter.SetProperty \
        string:Powered variant:boolean:false
}

# ---------------------------------------------------------------------------
readbluetooth()
# ---------------------------------------------------------------------------
# Check Bluetooth Status
#
# No args
{
	bluetooth_status_old=$bluetooth_status
	bluetooth_connected_old=$bluetooth_connected
	
    if [[ -n "`hciconfig | grep RUNNING`" ]]; then
		bluetooth_status=true
		if [[ "`hcitool con | wc -l`" -gt 1 ]]; then
			bluetooth_connected=true
		else
			bluetooth_connected=false
		fi
	else
		bluetooth_status=false
		bluetooth_connected=false
	fi	
}


# ---------------------------------------------------------------------------
main()
# ---------------------------------------------------------------------------
# Main entry point
#
{
    # Init -> dbus & log
    init
	
	readbluetooth
	
	#PERFORM MAIN MONITORING LOOPS
	while true
	do
		sleep $g_samplerate

        readparameters
		readbluetooth

        # Check the traffic volume
		if [[ "$g_check_bluetooth" = true -a "$bluetooth_status" = true -a "$bluetooth_status_old" = true -a "$bluetooth_connected" = false -a "$bluetooth_connected_old" = false  ]]; then
			disconnect
			bluetooth_status=false
			osnotify "AutoDisconnected has disabled the inactive Bluetooth"
			logentry "bluetooth closed (inactive)"
		else			
			logentry "bluetooth checked"
		fi

	done
	
}

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

# Start AutoDisconnect

main

exit 0

