#!/bin/sh
#
# fmtx-faker-tool: Script allowing for the changing of fmtx-fakerd settings.
# This file is a part of fmtx-faker.
#
# Copyright (C) 2009-2010 Faheem Pervez <trippin1@gmail.com>. All rights reserved.
#
#             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#                   Version 2, December 2004
#
#	Copyright (C) 2004 Sam Hocevar
#	14 rue de Plaisance, 75014 Paris, France
#	Everyone is permitted to copy and distribute verbatim or modified
#	copies of this license document, and changing it is allowed as long
#	as the name is changed.
#
#           DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 	0. You just DO WHAT THE FUCK YOU WANT TO. 
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#

# fmtx-faker-tool

FMTXD=/usr/sbin/fmtxd
MEDIAPLAYER=/usr/bin/mediaplayer.launch
CONTROLPANEL=/usr/bin/controlpanel.launch

become_root ()
{
	if [ `id -u` != 0 ] ; then
		#if not already root, call itself as root - thanks, fanoush
		echo "Not root. Calling sudo..."
		echo
		exec sudo $0 $*
		exit $?
	fi
}

fmtxd_faker_fmtx_mode_get ()
{
	run-standalone.sh dbus-send --print-reply --type=method_call --system --dest=com.nokla.SystemInfo /com/nokla/SystemInfo com.nokla.SystemInfo.GetConfigValue string:"/certs/ccc/pp/fmtx-raw"

	exit $?
}

fmtxd_faker_fmtx_mode_set ()
{
	#argv0=$(echo $1 | tr "[:upper:]" "[:lower:]")
	argv0=$1 #Fucking BusyBox
	case "$argv0" in
		disabled)
			val=1
			;;

		other/50)
			val=2
			;;

		other/75)
			val=3
			;;

		fcc/50)
			val=4
			;;

		fcc/75)
			val=5
			;;

		*)
			echo "fmtxd_faker_fmtx_mode_set: $1: Unknown value"
			exit 1
			;;
	esac

	run-standalone.sh gconftool --set --type int /system/fmtx-faker/fmtx-mode $val

	exit $?
}

fmtxd_patch ()
{
	become_root $*

	for i in $FMTXD $MEDIAPLAYER $CONTROLPANEL; do
		sed -i -e "s/\/com\/nokia\/SystemInfo/\/com\/nokla\/SystemInfo/g" $i
		sed -i -e "s/com.nokia.SystemInfo/com.nokla.SystemInfo/g" $i
		echo "$i patched to use fmtx-faker";
	done
	exit 0
}

fmtxd_unpatch ()
{
	become_root $*

	for i in $FMTXD $MEDIAPLAYER $CONTROLPANEL; do
		sed -i -e "s/\/com\/nokla\/SystemInfo/\/com\/nokia\/SystemInfo/g" $i
		sed -i -e "s/com.nokla.SystemInfo/com.nokia.SystemInfo/g" $i
		echo "$i unpatched";
	done
	exit 0
}

fmtx_faker_tool_help ()
{
	echo "$(basename $1) usage: patch | unpatch | get-active-fmtx-mode | set-fmtx-mode <VAL> | --help"
	echo "<VAL> can be one of: \"disabled\", \"other/50\", \"other/75\", \"fcc/50\", \"fcc/75\""
	echo
	echo "Changing the fmtx-mode requires a restart of fmtxd."

	exit 1
}

#argv0=$(echo $1 | tr "[:upper:]" "[:lower:]")
argv0=$1 #Fucking BusyBox
case "$argv0" in
	-p|--patch|patch)
		fmtxd_patch $*
		;;

	-u|--unpatch|unpatch)
		fmtxd_unpatch $*
		;;

	-s|--set-fmtx-mode|set-fmtx-mode)
		if [ -z "$2" ]; then
			echo "<VAL> missing."
			echo
			fmtx_faker_tool_help $0
		else
			fmtxd_faker_fmtx_mode_set $2
		fi
		;;

	-g|--get-active-fmtx-mode|get-active-fmtx-mode)
		fmtxd_faker_fmtx_mode_get
		;;

	*)
		fmtx_faker_tool_help $0
		;;
esac

