#!/bin/sh

##################################################################################
##    Phone control scripts, based on http://wiki.maemo.org/Phone_control	##
##    Copyright (C) 2010 Mohammad Abu-Garbeyyeh					##
##										##
##    This program is free software; you can redistribute it and/or modify	##
##    it under the terms of the GNU General Public License as published by	##
##    the Free Software Foundation; either version 2 of the License, or		##
##    (at your option) any later version.					##
##										##
##    This program is distributed in the hope that it will be useful,		##
##    but WITHOUT ANY WARRANTY; without even the implied warranty of		##
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the		##
##    GNU General Public License for more details.				##
##										##
##    You should have received a copy of the GNU General Public License		##
##    along with this program; if not, write to the Free Software		##
##    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA	##
##################################################################################

##################################################################################

# Version number, used for -v below
VERSION=0.1
scriptname=phone-control

# Defining prefix for dbus-send commands

prefix=run-standalone.sh

# Defining usage text

usage() {
	cat <<EOF

Usage: phone-control [OPTIONS]

Options:
    -v				Display version
    -h				Show this help message.
    --call [number]		Call the number specified
    --answercall		Answer an incoming call (if present)
    --endcall			End an ongoing call (if present)
    --imei			Print the IMEI (serial) number
    --imsi			Print the IMSI number
    --cellstrength		Show cellular signal strength
    --speakeron			Turn loudspeaker on
    --speakeroff		Turn loudspeaker off
    --vibrateon  [pattern]	Vibrate using the pattern specified
    --vibrateoff [pattern]	Stop vibrating the pattern specified
    --listvibrapatterns		Lists default vibration patterns
    --ledon  [pattern]		Activate the LED pattern specified
    --ledoff [pattern]		Deactivate the LED pattern specified
    --listledpatterns		Lists default LED patterns
    --profileset [profile]	Change the current profile to the one specified
    --email [address]		Send an email to address specified
    --syncemail			Send and receive email
    --mp-play [/path/file]	Open the file specified in the media player.
    --mp-pause			Pause media player playback
    --mp-stop			Stop media player playback
    --mp-resume			Resume media player playback
    --mp-next			Skip to the next track
    --mp-prev			Skip to the previous track
    --notify [message]		Show a one-line notification
    --dialog [message]		Show a multi-line dialog (requires attention)
    --lock			Lock the device with a lock code
    --unlock			Unlock the device (useful over SSH)
    --keylock			Lock screen and keys
    --keyunlock			Unlock screen and keys
    --cellon			Enable cellular modem
    --celloff			Disable cellular modem
    --cellmode [2G/3G/Dual]	Set cellular modem mode
    --bton			Turn bluetooth on
    --btoff			Turn bluetooth off
    --wifion			Turn Wi-Fi on (ifconfig up)
    --wifioff			Turn Wi-Fi off (ifconfig down)
    --loadwifi			Load Wi-Fi kernel module (doesn't affect power)
    --unloadwifi		Unload Wi-Fi kernel module (doesn't affect power)
    --browse [url]		Open the specified url in the browser
    --volume [0-100]		Set volume level
    --dashboard			Open dashboard
    --fmtxon			Turn on the FMTX
    --fmtxoff			Turn off the FMTX
    --fmtxname			Set FMTX RDS station name
    --fmtxinfo			Set FMTX RDS info text
    --capture [/path/file]	Capture an image and save it to the file specified
    --frontcap [/path/file]	Capture an image from the front cam and save it to the file specified
EOF
}
##################################################################################

##################################################################################
#Defining commands								 #
##################################################################################

version() {
	echo "$scriptname $VERSION"
}

profileset() {
$prefix dbus-send --type=method_call --dest=com.nokia.profiled /com/nokia/profiled com.nokia.profiled.set_profile string:"$profilevalue"
}

# mce related

vibrateon() {
$prefix dbus-send --system --print-reply --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_vibrator_pattern_activate string:"$vibrapattern"
}

vibrateoff() {
$prefix dbus-send --system --print-reply --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_vibrator_pattern_deactivate string:"$vibrapattern"
}

ledon() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_led_pattern_activate string:"$ledpattern"
}

ledoff() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_led_pattern_deactivate string:"$ledpattern"
}

# Email related

email() {
$prefix dbus-send --type=method_call --dest=com.nokia.modest /com/nokia/modest com.nokia.modest.MailTo string:"mailto:$2"
}

syncemail() {
$prefix dbus-send --type=method_call --dest=com.nokia.modest /com/nokia/modest com.nokia.modest.SendReceive
}

# Media Player related

playfile() {
$prefix dbus-send --print-reply --dest=com.nokia.mediaplayer /com/nokia/mediaplayer com.nokia.mediaplayer.mime_open string:"file://$2"
}

mppause() {
$prefix dbus-send --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.renderer.pause
}

mpresume() {
$prefix dbus-send --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.renderer.play
}

mpstop() {
$prefix dbus-send --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.renderer.stop
}

mpnext() {
$prefix dbus-send --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.renderer.next
}

mpprev() {
$prefix dbus-send --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.renderer.previous
}

# UI related

notify() {
$prefix dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint string:"$notificationvalue"
}

dialog1() {
$prefix dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog string:"$dialogvalue" uint32:0 string:"OK"
}

dashboard() {
$prefix dbus-send /com/nokia/hildon_desktop com.nokia.hildon_desktop.exit_app_view
}

# Security related

lock() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.system_ui /com/nokia/system_ui/request com.nokia.system_ui.request.devlock_open string:"com.nokia.mce" string:"/com/nokia/mce/request" string:"com.nokia.mce.request" string:"devlock_callback" uint32:'3'
}

unlock() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.system_ui /com/nokia/system_ui/request com.nokia.system_ui.request.devlock_close string:"com.nokia.mce" string:"/com/nokia/mce/request" string:"com.nokia.mce.request" string:"devlock_callback" uint32:'0'
}

keylock() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_tklock_mode_change string:"locked"
}

keyunlock() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.mce /com/nokia/mce/request com.nokia.mce.request.req_tklock_mode_change string:"unlocked"
}

# Call/Modem related

cellon() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.phone.SSC /com/nokia/phone/SSC com.nokia.phone.SSC.set_radio boolean:true
}

celloff() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.phone.SSC /com/nokia/phone/SSC com.nokia.phone.SSC.set_radio boolean:false
}

cellmode() {
# hmm...
# 0 = Dual
# 1 = 2G
# 2 = 3G
$prefix dbus-send --system --type=method_call --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.set_selected_radio_access_technology byte:$mode
}

imsi() {
$prefix dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.SIM /com/nokia/phone/SIM Phone.Sim.get_imsi | grep string | cut -f 2 -d '"'
}

cellstrength() {
$prefix dbus-send --system --print-reply --dest=com.nokia.phone.net /com/nokia/phone/net Phone.Net.get_signal_strength
}

answercall() {
$prefix dbus-send --system --print-reply --dest=com.nokia.csd.Call /com/nokia/csd/call/1 com.nokia.csd.Call.Instance.Answer
}

endcall() {
$prefix dbus-send --system --print-reply --dest=com.nokia.csd.Call /com/nokia/csd/call com.nokia.csd.Call.Release
}

imei() {
$prefix dbus-send --system --type=method_call --print-reply --dest=com.nokia.phone.SIM /com/nokia/phone/SIM/security Phone.Sim.Security.get_imei | grep string | cut -f 2 -d '"'
}

# Bluetooth

bton() {
$prefix 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:true
}

btoff() {
$prefix 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
}

# Wi-Fi

wifion() {
if [ "`whoami`" = "root" ];
	then
		ifconfig up wlan0
	else
		echo "Run the script with sudo prefixed."
fi
}

wifioff() {
if [ "`whoami`" = "root" ];
	then
		ifconfig down wlan0
	else
		echo "Run the script with sudo prefixed."
fi
}


loadwifi() {
if [ "`whoami`" = "root" ];
	then
		echo "Loading wl12xx kernel module"
		modprobe wl12xx
	else
		echo "Run the script with sudo prefixed."
fi
}

unloadwifi() {
if [ "`whoami`" = "root" ];
	then
		echo "Unloading wl12xx kernel module"
		modprobe -r wl12xx
	else
		echo "Run the script with sudo prefixed."
fi
}

# Browser
browse() {
$prefix dbus-send --system --type=method_call --dest=com.nokia.osso_browser /com/nokia/osso_browser/request com.nokia.osso_browser.load_url string:"$browserurl"
}

# Speaker related

volume() {
$prefix dbus-send --type=method_call --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer /com/nokia/mafw/renderer/gstrenderer com.nokia.mafw.extension.set_extension_property string:volume variant:uint32:$volumevalue
}

speakeron() {
$prefix dbus-send --type=method_call --dest=com.nokia.osso_hp_ls_controller /com/nokia/osso_hp_ls_controller com.nokia.osso_hp_ls_controller.loudspeaker.force_loudspeaker_on
}

speakeroff() {
$prefix dbus-send --type=method_call --dest=com.nokia.osso_hp_ls_controller /com/nokia/osso_hp_ls_controller com.nokia.osso_hp_ls_controller.loudspeaker.force_loudspeaker_on
}

# FMTX related

fmtxon() {
fmtx_client -p1
}

fmtxoff() {
fmtx_client -p0
}

fmtxname() {
fmtx_client -s"$fmtxnamevalue"
}

fmtxinfo() {
fmtx_client -t"$fmtxinfovalue"
}

# Camera related

capture() {
if [ -f /usr/bin/gst-launch ];
	then
	sleep 10 ; /usr/bin/gst-launch v4l2camsrc device=/dev/video0 num-buffers=1 \! video/x-raw-yuv,width=2592,height=1968  \! ffmpegcolorspace \! jpegenc \! filesink location="$capturepath"
	else 
	echo "Please install gstreamer-tools"
fi
}

frontcap() {
if [ -f /usr/bin/gst-launch ];
	then
	sleep 10 ; /usr/bin/gst-launch v4l2camsrc device=/dev/video1 num-buffers=1 \! video/x-raw-yuv,width=640,height=480  \! ffmpegcolorspace \! jpegenc \! filesink location="$capturepath"
	else
	echo "Please install gstreamer-tools"
fi
}

############################## Experimental ##############################
# GPS related
gpsreverse() {
strace -p $(pidof location-daemon) -e read=6 2>&1|grep '| 000'
}
############################## Experimental ##############################


##################################################################################

# Default Pattern List

VibraPatternList="PatternIncomingCall\nPatternIncomingMessage\nPatternPowerKeyPress\nPatternTouchscreen\nPatternChatAndEmail"

LEDPatternList="PatternError\nPatternDeviceOn\nPatternDeviceSoftOff\nPatternPowerOn\nPatternPowerOff\nPatternCommunicationCall\nPatternCommunicationIM\nPatternCommunicationSMS\nPatternCommunicationEmail\nPatternCommonNotification\nPatternWebcamActive\nPatternBatteryCharging\nPatternBatteryFull"

##################################################################################

# Parameters

if [ "$1" = "" ]; then
	usage
fi

if [ "$1" = "-v" ]; then
	version
fi

if [ "$1" = "-h" ]; then
	usage
fi

if [ "$1" = "--call" ]; then
	if [ "$2" = "" ]; then
		echo "You must enter a number, e.g: $scriptname $1 0044123456789"
	else $prefix dbus-send --system --type=method_call --print-reply --dest=com.nokia.csd.Call /com/nokia/csd/call com.nokia.csd.Call.CreateWith string:"$2" uint32:0
	fi
fi

if [ "$1" = "--answercall" ]; then
	answercall
fi

if [ "$1" = "--endcall" ]; then
	endcall
fi

if [ "$1" = "--imei" ]; then
	imei
fi

if [ "$1" = "--imsi" ]; then
	imsi
fi

if [ "$1" = "--cellstrength" ]; then
	cellstrength
fi

############################## Experimental ##############################
if [ "$1" = "--gps" ]; then
	echo "Experimental, not shown in -h"
	strace -p $(pidof location-daemon) -e read=6 2>&1|grep '| 000'
fi
############################## Experimental ##############################

if [ "$1" = "--speakeron" ]; then
	speakeron
fi

if [ "$1" = "--speakeroff" ]; then
	speakeroff
fi

if [ "$1" = "--vibrateon" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a pattern"
		echo -e "Default patterns are:\n"
		echo -e $VibraPatternList
	else
		vibrapattern=$2
		vibrateon
	fi
fi

if [ "$1" = "--vibrateoff" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a pattern"
		echo -e "Default patterns are:\n"
		echo -e $VibraPatternList
	else
		vibrapattern=$2
		vibrateoff
	fi
fi

if [ "$1" = "--listvibrapatterns" ]; then
	echo -e $VibraPatternList
fi

if [ "$1" = "--listledpatterns" ]; then
	echo -e $LEDPatternList
fi

if [ "$1" = "--ledon" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a pattern"
		echo -e "Default patterns are:\n"
		echo -e $LEDPatternList
	else
		ledpattern=$2
		ledon
	fi
fi

if [ "$1" = "--ledoff" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a pattern"
		echo -e "Default patterns are:\n"
		echo -e $LEDPatternList
	else
		ledpattern=$2
		ledoff
	fi
fi

if [ "$1" = "--profileset" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a profile to set"
		# Thanks to Ford_Prefect on #maemo for the "cut" tip :)
		echo "Current profile:" `dbus-send --type=method_call --print-reply --dest=com.nokia.profiled /com/nokia/profiled com.nokia.profiled.get_profile | grep "string" |  cut -f 2 -d '"'`
		echo "Available profiles:"
		$prefix dbus-send --type=method_call --print-reply --dest=com.nokia.profiled /com/nokia/profiled com.nokia.profiled.get_profiles | grep "string" |  cut -f 2 -d '"'
	else
	profilevalue=$2
	profileset
	fi
fi

if [ "$1" = "--email" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify an email address"
	else
	email
	fi
fi

if [ "$1" = "--syncemail" ]; then
	syncemail
fi

if [ "$1" = "--mp-play" ]; then
	mpplay
fi

if [ "$1" = "--mp-pause" ]; then
	mppause
fi

if [ "$1" = "--mp-stop" ]; then
	mpstop
fi

if [ "$1" = "--mp-resume" ]; then
	mpresume
fi

if [ "$1" = "--mp-next" ]; then
	mpnext
fi

if [ "$1" = "--mp-prev" ]; then
	mpprev
fi

if [ "$1" = "--notify" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a message"
	else
		notificationvalue=$2
		notify
	fi
fi

if [ "$1" = "--dialog" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a message"
	else
		dialogvalue=$2
		dialog1
	fi
fi

if [ "$1" = "--lock" ]; then
	lock
fi

if [ "$1" = "--unlock" ]; then
	unlock
fi

if [ "$1" = "--keylock" ]; then	
	keylock
fi

if [ "$1" = "--keyunlock" ]; then
	keyunlock
fi

if [ "$1" = "--cellon" ]; then
	cellon
fi

if [ "$1" = "--celloff" ]; then
	celloff
fi

if [ "$1" = "--cellmode" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a cellular mode mode"
		echo "e.g: $scriptname $1 2G"
		echo "You can set 2G, 3G, or Dual"
	fi
	
	if [ "$2" = "2G" ]; then
		mode=1
		cellmode
	fi

	if [ "$2" = "3G" ]; then
		mode=2
		cellmode
	fi

	if [ "$2" = "Dual" ]; then
		mode=0
		cellmode
	fi
fi

if [ "$1" = "--bton" ]; then
	bton
fi

if [ "$1" = "--btoff" ]; then
	btoff
fi

if [ "$1" = "--browse" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a URL"
		echo "e.g: $scriptname $1 http://talk.maemo.org"
	else
		browserurl=$2
		browse
	fi
fi

if [ "$1" = "--volume" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a volume level from 0-100"
		echo "e.g: $scriptname $1 50"
	else
		volumevalue=$2
		volume
	fi
fi

if [ "$1" = "--dashboard" ]; then
	dashboard
fi

if [ "$1" = "--fmtxon" ]; then
	fmtxon
fi

if [ "$1" = "--fmtxoff" ]; then
	fmtxoff
fi

if [ "$1" = "--fmtxname" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify an RDS station name"
		echo "e.g: $scriptname $1 maemo.org"
	else
		fmtxnamevalue=$2
		fmtxname
	fi
fi

if [ "$1" = "--fmtxinfo" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify RDS info text"
		echo "e.g: $scriptname $1 maemo.org"
	else
		fmtxinfovalue=$2
		fmtxinfo
	fi
fi

if [ "$1" = "--capture" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a path to save the image"
		echo "e.g: $scriptname $1 /home/user/MyDocs/image123.jpg"
	else
		capturepath=$2
		capture
	fi
fi

if [ "$1" = "--frontcap" ]; then
	if [ "$2" = "" ]; then
		echo "You have to specify a path to save the image"
		echo "e.g: $scriptname $1 /home/user/MyDocs/image123.jpg"
	else
		capturepath=$2
		frontcap
	fi
fi

if [ "$1" = "--wifion" ]; then
	wifion
fi

if [ "$1" = "--wifioff" ]; then
	wifioff
fi

if [ "$1" = "--loadwifi" ]; then
	loadwifi
fi

if [ "$1" = "--unloadwifi" ]; then
	unloadwifi
fi

##################################################################################
