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

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

if [ -f $HOME/.kinput2rc ] ; then

  if [ -f $LOCK ] ; then
    PID=`cat $LOCK`
  fi

  case $1 in
    stop)
      if [ ! -z "$PID" ] ; then
        if ps $PID > /dev/null 2>&1 ; then
          kill $PID
        fi
      fi
      rm $LOCK
      exit 0
      ;;
    restart)
      $0 stop
      shift
      exec $0 $*
      ;;
  esac

  source $HOME/.kinput2rc
  case $SERVER in
    canna|wnn|sj3)

      if [ ! -z $PID ] ; then
        if ps $PID > /dev/null 2>&1 ; then
          echo kinput2 is running on $PID
          exit 1;
        else
          rm $LOCK
        fi
      fi

      echo $$ > $LOCK
      exec kinput2-$SERVER $OPTS $*
      ;;

    *)echo invalid ~/.kinput2rc ; exit 1
      ;;
  esac

else

  # initialize user's rcfile (~/.kinput2rc)
  if [ -f /usr/sbin/cannaserver ] ; then
    SERVEROPTS="-cannaserver localhost"
    cat << END  > $HOME/.kinput2rc
# SERVER: canna / wnn / sj3
SERVER=canna
END

  elif [ -f /usr/bin/Wnn4/jserver ] ; then
    SERVEROPTS=""
    cat << END  > $HOME/.kinput2rc
# SERVER: canna / wnn / sj3
SERVER=wnn
END

  elif [ -f /usr/bin/sj3serv ] ; then
    SERVEROPTS=""
    cat << END > $HOME/.kinput2rc
# SERVER: canna / wnn / sj3
SERVER=sj3
END

  else
    exit
  fi

  echo creating ~/.kinput2rc

  cat << END >> $HOME/.kinput2rc

# 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 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 canna>
#    -canna                         use Canna (Iroha) as the conversion engine
#    {-cannaserver|-cs} <hostname>[:n] specify cannaserver host
#    -cannafile <cannafile>         specify canna customize 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="$SERVEROPTS"
END

  exec $0

fi
