#!/bin/sh
if [ `id -u` != 0 ] ; then
#if not already root, call itself as root
        exec sudo gainroot <<EOF
exec $0 $*
EOF
        exit $?
fi

BASEDIR=`dirname $0`
OSVER=$(cut -d _ -f 2 /etc/osso_software_version)
[ "$OSVER" = "DIABLO" ] && OSVER=2008SE 

infoprint(){
if [ "$OSVER" = "2008SE" ] ; then
    DBUS_SESSION_BUS_ADDRESS='unix:path=/tmp/session_bus_socket' dbus-send >/dev/null 2>&1 --session --print-reply --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteInfoprint "string:$*" &
else
    dbus-send >/dev/null 2>&1 --system --print-reply --dest=com.nokia.statusbar /com/nokia/statusbar com.nokia.statusbar.system_note_infoprint "string:$*" &
fi
}

bt_check_and_enable(){
#currently OS2008 version only
case $OSVER in
	2008*)	
		if [ "$($BASEDIR/dbus-bluez-adapter-mode)" = "off" ] ; then
		    infoprint "Bluetooth is off, enabling ..."
		    $BASEDIR/dbus-bluez-adapter-mode connectable		
		fi
	 ;;
esac
}


if [ "$1" = "scan" ] ; then
	if [ "$OSVER" = "2008SE" -a "`which hcitool`" = "" ] ; then
		bt_check_and_enable
		./dbus-bluez-scan
		exit
	else
		hcitool scan
		exit
	fi
fi

case $OSVER in
    200[78]*) IMSWITCH=no ;;
    2006*) IMSWITCH=yes ;;
esac
# default key repeat values (in miliseconds)
KEYREPEAT_DELAY=600 # delay before first key repeat
KEYREPEAT_PERIOD=80 # repeat speed

[ -f ${0}.conf ] && . ${0}.conf  

if [ "$BDADDR" = "" ] ; then
	infoprint "Bluetooth keyboard not configured"
	echo "Bluetooth keyboard not configured."
	echo "Edit ${0}.conf and set BDADDR, KBTYPE and PORTNUM variables."
	echo "You can use 'btkbd scan' to find keyboard address.  See README."
	exit 0
fi
KERNELVERSION=`cut -d ' ' -f 3 /proc/version`
KBDDOUTPUT=/dev/null
KBDD_LOCKFILE=/tmp/kbdd.lock
if [ "$DEBUG" = "yes" ] ; then
	KBDDFLAGS="-d"
	KBDDOUTPUT=/tmp/kbdd.log
fi

kbdd_cleanup(){
case $OSVER in
    2008*) $BASEDIR/dbus-bluez-rfcomm release $BDADDR ;;
    200[67]*) rfcomm release rfcomm${PORTNUM} ;;
esac
rmmod uinput
}

KBDDPID=`pidof kbdd`
if [ -f $KBDD_LOCKFILE ] || [ "$KBDDPID" != "" ] ; then
	if [ -z "$KBDDPID" ] ; then
		infoprint "Keyboard already disconnected"
		kbdd_cleanup
	else
		#kbdd is running, turn it off
		kill $KBDDPID
		sleep 1
	fi
	[ "$DEBUG" = "yes" ] && [ -f $KBDDOUTPUT ] && rm $KBDDOUTPUT
	[ -f $KBDD_LOCKFILE ] && rm $KBDD_LOCKFILE
else
	echo 1 > $KBDD_LOCKFILE
	bt_check_and_enable
	infoprint "Searching for keyboard ..."
        case $OSVER in
    		2008*) PORT=`$BASEDIR/dbus-bluez-rfcomm bind $BDADDR` ;;
    		200[67]*) PORT=rfcomm${PORTNUM} ; sdptool >/dev/null browse ${BDADDR} ;;
	esac
	if  [ $? -gt 0 ] ; then
		infoprint "Connection to ${BDADDR} failed"
		[ -f $KBDD_LOCKFILE ] && rm $KBDD_LOCKFILE
		exit 0
	fi
	#rfcomm release rfcomm${PORTNUM}
	insmod $BASEDIR/uinput.ko.${KERNELVERSION}
        case $OSVER in
    		200[67]*) rfcomm bind ${PORT} ${BDADDR} ;;
	esac
	(
	# raise priority
	sleep 3 ; P=`pidof kbdd` ; [ -z "$P" ] || renice -2 $P
	#change key repeat rate
	cd /sys/class/input
	for dev in input* ; do
		if [ "$(cat $dev/name)" = "SerKBD" ] ; then
			cd $dev
			for ev in event* ; do
				cp -a /dev/input/$ev /tmp/$$-$ev
				#this works because /tmp is shared with initfs
				chroot /mnt/initfs evrepeat /tmp/$$-$ev $KEYREPEAT_DELAY $KEYREPEAT_PERIOD
				rm /tmp/$$-$ev
			done
		fi
	done
	) &
(
	[ "$IM_SWITCH" = "yes" ] && maemo-gtk-im-switch xim
	echo 0 > $KBDD_LOCKFILE
	infoprint "Keyboard connected"
	${BASEDIR}/kbdd  ${KBDDFLAGS} -t ${KBTYPE} -p /dev/${PORT}
	infoprint "Keyboard disconnected"
	[ "$IM_SWITCH" = "yes" ] && maemo-gtk-im-switch osso-input-method
	kbdd_cleanup
	[ -f $KBDD_LOCKFILE ] && rm $KBDD_LOCKFILE
) </dev/null >${KBDDOUTPUT} 2>&1 &
fi
