#!/bin/sh
# usage: kinput2 [stop|restart] <original kinput2 args>

LOCK=$HOME/.kinput2.PID
CONVERSIONCLIENT=kinput2
export CONVERSIONCLIENT

if [ -f $HOME/.kinput2rc ] ; then
  [ -f $LOCK ] && PID=`cat $LOCK`
  case $1 in
  stop)
    [ -n "$PID" ] && ps $PID &> /dev/null && kill $PID ; rm $LOCK ; exit 0 ;;
  restart)
    $0 stop ; shift ; exec $0 "$@" ;;
  esac
  . $HOME/.kinput2rc
  case $SERVER in
  canna|wnn|sj3)
    if [ -n "$PID" ] ; then
      if ps $PID &> /dev/null ; then
        echo "kinput2 is running on $PID" ; exit 1
      else
        rm $LOCK
      fi
    fi
    echo $$ > $LOCK ; exec /usr/X11R7/libexec/kinput2 -$SERVER $OPTS "$@"
    ;;
  *)
    echo "invalid ~/.kinput2rc" ; exit 1
    ;;
  esac
else
  # initialize user's rcfile (~/.kinput2rc)
  echo "creating ~/.kinput2rc"
  cat <<- "EOF"  > $HOME/.kinput2rc
	# SERVER: canna / wnn / sj3
	SERVER=canna
	
	# OPTS: options for kinput2
	#   -bc                             backward compatible mode
	#   -font <font>                    ASCII font to be used
	#   -kanjifont <font>               KANJI font to be used
	#   -kanafont <font>                KANA font to be used
	#   -/+kinput                       activate/deactivate kinput protocol family
	#   -/+ximp                         activate/deactivate Ximp protocol
	#   -/+xim                          activate/deactivate X Input Method protocol
	#   -tbheight <number>              specify pop-up shell's titlebar height
	#   -background <color>             background color
	#   -foreground <color>             foreground color
	#   -rv                             reverse video mode
	#   -display <display>              specify display
	#   -version                        print version information and exit
	# <for canna>
	#   -canna                          use Canna (Iroha) as the conversion engine
	#   {-cannaserver|-cs} <hostname>[:n] specify cannaserver host
	#   -cannafile <cannafile>          specify canna customize file
	# <for wnn>
	#   -wnn                            use Wnn as the conversion engine
	#   -jserver <hostname>             specify jserver host
	#   -ccdef <ccdeffile>              specify character conversion def. file
	#   -wnnenvname <name>              specify Wnn environment name
	#   -wnnenvrc4 <wnnenvrcfile>       specify Wnn environment file for Wnn4
	#   -wnnenvrc6 <wnnenvrcfile>       specify Wnn environment file for Wnn6
	#   -wnnenvrc <wnnenvrcfile>        specify Wnn environment file
	# <for sj3>
	#   -sj3                            use Sj3 as the conversion engine
	#   -sj3serv <hostname>             specify first sj3serv host
	#   -sj3serv2 <hostname>            specify second sj3serv host
	#   -sj3user <user>                 specify user name connect to sj3serv
	#   {-rcfile|-sjrc} <file>          specify resource definition file
	#   {-sbfile|-sjsb} <file>          specify symbol table file
	#   {-rkfile|-sjrk} <file>          specify roma-kana coversion definition file
	#   {-hkfile|-sjhk} <file>          specify hira-kata coversion definition file
	#   {-zhfile|-sjzh} <file>          specify zen/han coversion definition file
	OPTS="-cannaserver localhost"
	EOF
  exec $0
fi
