#! /bin/bash
#######################################################################
# xeditor
# Version: 1.2
# This script is used by worker to call a X editor
# Several editors are searched with vi in a xterm as fallback
# but the env variable WORKER_XEDITOR can force another editor
#
# written by Ralf Hoffmann 2005
# License: GPL V2 or later
# Worker: http://www.boomerangsworld.de/worker
#######################################################################

XEDITOR="xterm"
SEARCH_EDITORS="nedit gedit gvim jedit emacs xemacs xedit"
USE_EDITOR=""
LINE_ARG_TYPE="unknown"

if [ -z "$WORKER_XEDITOR" ]; then
  for e in $SEARCH_EDITORS; do
    p=$(which $e 2>/dev/null)
    if [ -x "$p" ]; then
      USE_EDITOR="$p"
      case "$e" in
        nedit)
          LINE_ARG_TYPE="nedit"
	  ;;
        emacs|xemacs|gvim)
          LINE_ARG_TYPE="emacs"
	  ;;
      esac
      break
    fi
  done
else
  USE_EDITOR="$WORKER_XEDITOR"
fi

LINE_NUMBER=""
if [ "$1" = "-line" ]; then
  LINE_NUMBER="$2"
  shift
  shift
fi

if [ -n "$USE_EDITOR" ]; then
  if [ -n "$LINE_NUMBER" ]; then
    case "$LINE_ARG_TYPE" in
      nedit)
        "$USE_EDITOR" -line $LINE_NUMBER "$@"
        ;;
      emacs)
        "$USE_EDITOR" +$LINE_NUMBER "$@"
        ;;
      *)
        "$USE_EDITOR" "$@"
        ;;
    esac
  else
    "$USE_EDITOR" "$@"
  fi
else
  "$XEDITOR" -e vi "$@"
fi
