#!/bin/sh
# chkconfig: 2345 21 79
### BEGIN INIT INFO
# Provides:          uim
# Required-Start:    $local_fs $network
# Required-Stop:     $local_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start uim at boot time
# Description:       Enable user-side of the BT TI Wilink initilization.
# X-Start-Before:    x
# X-Stop-After:      x
# X-Timesys-Start-Number:  21
# X-Timesys-Stop-Number:  79
### END INIT INFO


PATH=/usr/bin:/bin:/usr/sbin:/sbin
DAEMON=uim
PID_FILE=/var/run/uim.pid

case "$1" in
start)
	if [ -e $PID_FILE ]; then
		PIDDIR=/proc/$(cat $PID_FILE)
		if [ -d ${PIDDIR} -a -n "$(readlink -f ${PIDDIR}/exe 2>/dev/null)" ]; then
			echo "uim already started; not restarting."
			exit  
		else
			echo "Removing stale PID file $PID_FILE."
			rm -f $PID_FILE
		fi
	fi
	uim $(ls -d /sys/bus/platform/devices/kim.* 2>/dev/null | head -1) &
	echo $! > $PID_FILE
	;;
stop)
	if [ -f $PID_FILE ]; then
		echo -n "Stopping uim: "
		read PID < $PID_FILE
		kill $PID && \
			echo '[ OK ]' || echo '[FAIL]'
		rm -f $PID_FILE
	else
		echo "pid file not found... is uim running?"
	fi
	;;
restart)
	$0 stop
	$0 start
	;;
*)
	echo "usage: $0 [ start | stop | restart ]"
	;;
esac
