#!/bin/sh

#+
# Xop
#
# NAME
# xop - starts an xop task
#
# SYNOPSIS
# xop [-e -d -w -h task]
#
# DESCRIPTION
# Starts either the xop main program or an xop task given by 
# the "task" argument. The task must recognized by xop, i.e.,
# xplot, dabax, xbragg, etc. 
#
# OPTIONS:
#     -e  starts xop in embedded mode (default)
#     -d  starts xop in developer mode 
#     -w  starts xop in developer mode using the IDL workbench
#     -h  displays the xop man page
#
# EXAMPLES:
#     xop -h       [displays help]
#     xop          [calls xop in embedded mode]
#     xop -d       [calls xop in developer mode]
#     xop xplot    [calls task xplot in embedded mode]
#     xop -e xplot [calls task xplot in embedded mode (as above)]
#-

#### THIS SECTION IS NEEDED FOR BEING USED WITH THE 
#### MACOS LAUNCHER. MUST REMAIN COMMENTED OTHERWISE
# open /Applications/Utilities/X11.app
# export DISPLAY=:0
# export XOP_TASK=xop
# export XOP_HOME=$1/Contents/Resources/xop2.3
# export XOP_WD=$XOP_HOME/tmp
#### END NEEDED FOR MACOS INSTALLER


############################################################################
#
# PLEASE CUSTOMIZE THE ENVIRONMENT VARIABLES HERE !!!!!!!!!!!!!!
#

# Define the environmental variable XOP_HOME pointing to the XOP distribution
if [ "$XOP_HOME" = "" ]; then
  XOP_HOME=/scisoft/xop2.3 ; export XOP_HOME
else
  echo "$0: Using previously defined XOP_HOME=$XOP_HOME"
fi

# The following variables are only used if you want to use XOP also
# in "development" mode. For that you nee a licensed version of idl 7.0
# They are ignored if only "embedded" mode will be used.
#
# Define IDL_DIR_DEVELOPMENT as the directory  where a licensed version of IDL 
# is installed.  Different idl installations and/or licenses are can be 
# defined in different "case" blocks. 

case `hostname` in
  myParticularHost*)
    # This is the directory of my local IDL (licensed) 
    IDL_DIR_DEVELOPMENT=/sware/com/idl-7.0/idl70 ; export IDL_DIR_DEVELOPMENT
    ;;
  *)
    # This is the directory of my local IDL (licensed) 
    IDL_DIR_DEVELOPMENT=/sware/com/idl-7.0/idl70 ; export IDL_DIR_DEVELOPMENT
    # It may need a particular place for the license file
    # LM_LICENSE_FILE= /sware/com/idl-7.0/license/license.dat ; export LM_LICENSE_FILE
    ;;
esac

#
# Define viewers (html, pdf, txt) used in displaying help files
#

# This is needed in some systems because the original IDL's one failed.
IDL_ONLINE_HELP_HTML_CMD=$XOP_HOME/idl70/bin/online_help_html_xop ; export IDL_ONLINE_HELP_HTML_CMD

# Define html browser  (valid names: darwin_open, lynx, mozilla,
# netscape, firefox, mozilla)
IDL_ONLINE_HELP_HTML_BROWSER=open ; export IDL_ONLINE_HELP_HTML_BROWSER

# Define pdf browser (valid names: darwin_open, acroread, xpdf)
# IDL_ONLINE_HELP_PDF_BROWSER=acrored ; export IDL_ONLINE_HELP_PDF_BROWSER

# Define txt browser used by xop/xdisplayfilenative.pro 
XOP_ONLINE_TXT_BROWSER=nedit ; export XOP_ONLINE_TXT_BROWSER

#
############################################################################

#
# check if DISPLAY variable is set
#
if [ "$DISPLAY" = "" ]; then
  echo "$0: DISPLAY variable not set."
  DISPLAY=:0. ; export DISPLAY
  echo "$0: DISPLAY variable set to $DISPLAY"
fi

# Define default XOP mode 
XOP_MODE=EMBEDDED ; export XOP_MODE

#
# Optional environmental variables
#

# DABAX_PATH
# DABAX_PATH=$XOP_HOME/data/dabax ; export DABAX_PATH

# DABAX_HELP
# DABAX_HELP=$XOP_HOME/doc ; export DABAX_HELP

# XOP_WD [Working Directory]
if [ "$XOP_WD" = "" ]; then
  XOP_WD=`pwd` ; export XOP_WD
fi


# XOP_BINARIES. Where the application's binaries sit down
# XOP_BINARIES=$XOP_HOME/bin.`$XOP_HOME/xop_uname` ; export XOP_WD

# XOP_BINARIES_DEBUG. Debugging flag to test the xop-spawn'ed processes.
# XOP_BINARIES_DEBUG=1 ; export XOP_BINARIES_DEBUG

# XOP_ANNOUNCE. If this flag is set, then start the "announce" window
# when starting XOP
# XOP_ANNOUNCE=1 ; export XOP_ANNOUNCE

# XOP_DEFAULTS_DIR. To write/read defaults input files
# XOP_DEFAULTS_DIR=$HOME/.xop ; export XOP_DEFAULTS_DIR

# XPLOT_DEFAULTS. Xplot defaults file
# XPLOT_DEFAULTS= ; export XPLOT_DEFAULTS

# XOP_BARTITLE. To redefine the XOP bar title.
XOP_BARTITLE='XOP 2.3 Beta' ; export XOP_BARTITLE

# XOP_PROJECT project files to be loaded ($XOP_HOME/ifc/$XOP_PROJECT.*)
# (default=xop) 
# XOP_PROJECT=xop ; export XOP_PROJECT

#
# Parses the command line to define the XOP_MODE ('EMBEDDED' [default],
# 'USER' or 'DEVELOPER') and XOP_TASK (the task to be launched 
# [default=xop]).
#
# If the local distribution do not allow all the modes, customize the
# error messages.
#

# This overwrites the defaults
while getopts edhw c
do 
  case $c in
    e) 
      XOP_MODE=EMBEDDED ; export XOP_MODE
      ;;
    d)
      XOP_MODE=DEVELOPER ; export XOP_MODE
      ;;
    w)
      XOP_MODE=DEVELOPER_ENVIRONMENT ; export XOP_MODE
      ;;
    h)
      nroff -man $XOP_HOME/xop.1 | more 
      exit 0
      ;;
    ?)
      echo "Usage: xop [-e -d -w -h task]"
      exit 2
      ;;
  esac
done
shift `expr $OPTIND - 1`

#
# Define task
#
if [ "$XOP_TASK" = "" ]; then
  if [ "$1" = "" ]; then
    XOP_TASK=xop ; export XOP_TASK
  else
    # Used wit MacOS launcher
    XOP_TASK=$1 ; export XOP_TASK
  fi
fi


#
# Set IDL_PATH
#

case $XOP_MODE in
  EMBEDDED ) 
    IDL_DIR=$XOP_HOME/idl70 ; export IDL_DIR
    IDL_PATH='<IDL_DEFAULT>' ; export IDL_PATH
    ;;
  DEVELOPER* )
    IDL_DIR=$IDL_DIR_DEVELOPMENT ; export IDL_DIR
    IDL_PATH='<IDL_DEFAULT>:+$XOP_HOME/src_idl/:+$XOP_HOME/extensions/' ; export IDL_PATH
    ;;
  ?)
    echo "IDL mode ($IDL_MODE) not valid."
    exit 2
    ;;
esac

#
# Display options
#
echo "$0 environment variables:"
echo "XOP_HOME: $XOP_HOME"
echo "XOP_MODE: $XOP_MODE"
echo "XOP_TASK: $XOP_TASK"
echo "IDL_DIR: $IDL_DIR"
echo "IDL_PATH: $IDL_PATH"
echo "DISPLAY: $DISPLAY"
echo " "

#
# Start xop
#
echo "Starting XOP task: $XOP_TASK in $XOP_MODE mode."
case $XOP_MODE in
  EMBEDDED) 
    XOP_COMMAND="$IDL_DIR/bin/idl -em=$XOP_HOME/xop.sav"
    echo "Executing: $XOP_COMMAND"
    echo " "
    $XOP_COMMAND
    ;;
  DEVELOPER)  
    IDL_STARTUP=$XOP_HOME/maind.pro ; export IDL_STARTUP
    XOP_COMMAND=$IDL_DIR/bin/idl
    echo "Executing: $XOP_COMMAND"
    echo " "
    $XOP_COMMAND
    ;;
  DEVELOPER_ENVIRONMENT)  
    IDL_STARTUP=$XOP_HOME/maind.pro ; export IDL_STARTUP
    XOP_COMMAND=$IDL_DIR/bin/idlde
    echo "Executing: $XOP_COMMAND"
    echo " "
    $XOP_COMMAND
    ;;
  *)
    echo "Mode $XOP_MODE not recognized."
    exit 2
    ;;
esac

exit 0
