#! @SH@

# file: $ISIP_IFC/util/misc/isip_e/isip_e.sh
# version: $Id: isip_e.sh 9611 2004-07-03 18:06:03Z picone $
#
# The isip_e utility invokes the editor, selecting the proper type of
# editor, creating a suitable window title, window index, etc.
#
# 07/03/04 (joe picone): updated for linux
#
# 12/27/97 (joe picone): added a source of the environment for x11r6
# 			 this has been a constant battle - xrsh on r6 does not
#			 appear to pick up the environment
#
# 8/21/94 (joe picone): added a command line option
#  it was discovered that xon likes to start a csh independent
#  of the type of shell you use. Hence, syntax like:
#
#	xon EDITOR=emacs isip_e
#
#  doesn't work. this made launching different tools from a twmrc
#  file very difficult. an option was added, "-tool foo", so that
#  a command line option exists for selecting the tool.
#

# make sure there is an isip environment - must be done at the top
#
if (test -z "$ISIP") then
    . $HOME/.bashrc;
fi

# important definitions
#
PROGRAM_NAME="isip_e";

ISIP_HELP_FILE="$ISIP_DEVEL/util/misc/isip_e/isip_e.help";
export ISIP_HELP_FILE;

DEFAULT_TOOL="emacs"
DEFAULT_INIT_FILE="$HOME/.emacs"
OPTION_NOWINDOW="-nw";
OPTION_WINDOW_NAME="-name ";
OPTION_TOOL_NAME="-tool";

TOOL_0="emacs";
TOOL_0_NAME="emacs";
TOOL_0_OPTIONS="";

TOOL_1="mule";
TOOL_1_NAME="mule";
TOOL_1_OPTIONS="";

TOOL_2="emacs";
TOOL_2_NAME="emacs_2";
TOOL_2_OPTIONS="";

TOOL_3="emacs";
TOOL_3_NAME="emacs_3";
TOOL_3_OPTIONS="";

TOOL_4="emacs";
TOOL_4_NAME="emacs_4";
TOOL_4_OPTIONS="";

# execute a generic help function:
#  force the shellscript to exit if a help option is encountered.
set -e;
isip_function_help_0 $*;
set +e;

# check for a command line option:
#  if found, save the value and shift it off
#
if (test "$1" = "$OPTION_TOOL_NAME") then
    shift;
    EDITOR=$1; export EDITOR;
    shift;
fi

# define the editor initialization file:
#
INIT_FILE="$ISIP_EDITOR_INIT_FILE";
if (test ! "$INIT_FILE") then
    INIT_FILE="$DEFAULT_INIT_FILE";
fi

# start building the generic command line options:
#  check if x is running
#
if (test ! "$DISPLAY") then
    OPTIONS="$OPTION_NOWINDOW";
else
    OPTIONS="";
fi

# add the init file
#
if (test -f "$INIT_FILE") then
    OPTIONS="$OPTIONS -l $INIT_FILE";
fi

# establish a tool
#
if (test ! "$EDITOR") then
    TOOL_NAME="$DEFAULT_TOOL";
else
    TOOL_NAME="$EDITOR";
fi

# append the window name option (must be last)
#
if (test "$DISPLAY") then
    OPTIONS=$OPTIONS" $OPTION_WINDOW_NAME";

    # create a new window name
    #
    WINDOW_NAME=`isip_function_window_name_0 $TOOL_NAME`;

else
    WINDOW_NAME="";
fi

# invoke one of a set of user-defined tools - if a display is set, exec it
#
if (test -z "$DISPLAY") then
    if (test "$TOOL_NAME" = "$TOOL_0_NAME") then
	$TOOL_0 $TOOL_0_OPTIONS $OPTIONS"$WINDOW_NAME" $*
    elif (test "$TOOL_NAME" = "$TOOL_1_NAME") then
	$TOOL_1 $TOOL_1_OPTIONS $OPTIONS"$WINDOW_NAME" $*
    elif (test "$TOOL_NAME" = "$TOOL_2_NAME") then
	$TOOL_2 $TOOL_2_OPTIONS $OPTIONS"$WINDOW_NAME" $*
    elif (test "$TOOL_NAME" = "$TOOL_3_NAME") then
	$TOOL_3 $TOOL_3_OPTIONS $OPTIONS"$WINDOW_NAME" $*
    elif (test "$TOOL_NAME" = "$TOOL_4_NAME") then
	$TOOL_4 $TOOL_4_OPTIONS $OPTIONS"$WINDOW_NAME" $*
    else
	echo "error($PROGRAM_NAME): $TOOL_NAME is an unknown tool";
	exit $ISIP_ERROR;
    fi
else
    if (test "$TOOL_NAME" = "$TOOL_0_NAME") then
	exec $TOOL_0 $TOOL_0_OPTIONS $OPTIONS"$WINDOW_NAME" $* &
    elif (test "$TOOL_NAME" = "$TOOL_1_NAME") then
	exec $TOOL_1 $TOOL_1_OPTIONS $OPTIONS"$WINDOW_NAME" $* &
    elif (test "$TOOL_NAME" = "$TOOL_2_NAME") then
	exec $TOOL_2 $TOOL_2_OPTIONS $OPTIONS"$WINDOW_NAME" $* &
    elif (test "$TOOL_NAME" = "$TOOL_3_NAME") then
	exec $TOOL_3 $TOOL_3_OPTIONS $OPTIONS"$WINDOW_NAME" $* &
    elif (test "$TOOL_NAME" = "$TOOL_4_NAME") then
	exec $TOOL_4 $TOOL_4_OPTIONS $OPTIONS"$WINDOW_NAME" $* &
    else
	echo "error($PROGRAM_NAME): $TOOL_NAME is an unknown tool";
	exit $ISIP_ERROR;
    fi
fi

#
# end of file
