#!/bin/bash

# file: $NEDC_NFC/util/shell/isip_e/isip_e.sh
#
# revision history:
#
# 20210216 (JP): first version integrated into NEDC
# 20200911 (JP): added options for running in windowed mode
#                cleaned up the code and eliminated some
#                outdated stuff
# 20040703 (JP): updated for linux
# 19971227 (JP): 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
# 19940821 (JP): 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.
#
# The isip_e utility invokes the editor, selecting the proper type of
# editor, creating a suitable window title, window index, etc.
# This is a front end for running emacs with a few convenient options set.
#------------------------------------------------------------------------------

# 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="$NEDC_NFC/util/shell/isip_e/isip_e.help";
export ISIP_HELP_FILE;

DEFAULT_TOOL="emacs"
DEFAULT_INIT_FILE="$HOME/.emacs"
OPTION_NOWINDOW="-nw";
OPTION_WINDOW="--w";

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

# check for windowed mode
#
MODE_WIN=""
for arg do
    if (test "$arg" = "$OPTION_WINDOW") then
       MODE_WIN=$arg;
       shift;
    fi
done

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

# add the windowed mode
#
if (test "$MODE_WIN" = "$OPTION_WINDOW") then
    OPTIONS="";
else
    OPTIONS="$OPTION_NOWINDOW";
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

# invoke one of a set of user-defined tools - if a display is set, exec it
#
$TOOL_NAME $OPTIONS $*

#
# end of file
