#!/bin/sh
#
# Start/stop/restart the key daemon.
. /etc/rc.conf
. /etc/rc.d/functions

keystune(){
  for keycode in "${KEYCODES[@]}"; do
    key=`echo $keycode | cut -d:  -f1`
    code=`echo $keycode | cut -d: -f2`
		setkeycodes $key $code
  done
}

# source application-specific settings
CONFIG=
KEYCODES=
DEVICE=
[ -f /etc/conf.d/esekeyd ] && . /etc/conf.d/esekeyd

PID=`pidof -o %PPID /usr/sbin/esekeyd`
case "$1" in
  start)
    stat_busy "Starting esekeyd"
		[ "$KEYCODES" ] && keystune
    [ -z "$PID" ] && /usr/sbin/esekeyd $CONFIG $DEVICE &>/dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon esekeyd
      stat_done
    fi
    ;;
  stop)
    stat_busy "Stopping esekeyd"
    [ ! -z "$PID" ]  && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm_daemon esekeyd
      stat_done
    fi
    ;;
  restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"  
esac
exit 0
