#!/bin/sh

#+
# Xop
#
# NAME
# xop - starts an xop task
#
# SYNOPSIS
# xop [-e -u -d -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)
#     -u  starts xop in user mode
#     -d  starts xop in developer mode 
#     -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)]
#-

############################################################################
#
# PLEASE CUSTOMIZE!!!!!!!!!!!!!!
#

# Define the environmental variable XOP_HOME pointing to the XOP distribution
XOP_HOME=/users/me/xop2.0

# Just for the beta version
# XOP_VERSION=2.0beta1
# export XOP_VERSION

# Define default XOP mode 
XOP_MODE=EMBEDDED			# Mode

# The following variables are only used if you want to use XOP also
# in "development" mode. They can be ignored if only "embedded" mode
# will be used.
# Define IDL_DIR version number for starting IDL in "development" mode.
# (If you want to run XOP from different machines, you can optionally
# redefine the IDL_DIR variable (See below).)
IDL_DIR_DEVELOPMENT=/usr/local/rsi/idl_5.2
IDL_DIR_USER=/usr/local/rsi/idl_5.2

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

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

#
# Define the environmental variable XOP_RC pointing to the file that extends
# the UNIX path (used by the IDL SPAWN procedure).
# (To be removed in XOP 2.0)
#
if [ "$XOP_RC" = "" ]; then
  XOP_RC=$XOP_HOME/setxoprc
fi


#
# Optional environmental variables
#

# XOP_WEB_BROWSER. To define a web broser
#XOP_WEB_BROWSER="netscape -install"
#export XOP_WEB_BROWSER

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

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

# XOP_IFC
#XOP_IFC=~/xop_ifc
#export XOP_IFC

# XOP_WD [Working Directory]
#XOP_WD=~/xop
#export XOP_WD

# XOP_USER_HOME. If this variable is set, then xop will execute 
# a user-idl-initialization file
#XOP_USER_HOME=$XOP_HOME
#export XOP_USER_HOME

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

# 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_VERSION. TO redefine the XOP version.
#XOP_VERSION='2.0 at home'
#export XOP_VERSION

# 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


#
# 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 eudh c
do 
  case $c in
    e) 
      XOP_MODE=EMBEDDED
      ;;
    u)
      XOP_MODE=USER
      ;;
    d)
      XOP_MODE=DEVELOPER
      ;;
    h)
      nroff -man $XOP_HOME/xop.1 | more 
      exit 0
      ;;
    ?)
      echo "Usage: xop [-e -u -d -h task]"
      exit 2
      ;;
  esac
done
shift `expr $OPTIND - 1`

#
# Define task
#
if [ "$1" = "" ]; then
  XOP_TASK=xop
else
  XOP_TASK=$1
fi


#
# Set IDL_PATH
#

case $XOP_MODE in
  EMBEDDED ) 
    IDL_DIR=$XOP_HOME/idl_5.2
    ;;
  USER )
    IDL_DIR=$IDL_DIR_USER
    ;;
  DEVELOPER )
    IDL_DIR=$IDL_DIR_DEVELOPMENT
    ;;
  ?)
    echo "IDL mode ($IDL_MODE) not valid."
    exit 2
    ;;
esac

#
# Redefine the IDL_DIR environmental variable to be host specific.
# This is not required for the EMBEDDED mode, but may be needed for 
# other modes, especially using XOP from different platforms with 
# non "standard" IDL installations.
#
# Define other default variables

if [ "$XOP_MODE" != "EMBEDDED" ]; then
  case `hostname` in
    dummy_rain)
      IDL_DIR=/usr/local/rsi/idl_5
      LM_LICENSE_FILE=$IDL_DIR/../license.dat
      export LM_LICENSE_FILE
      ;;
    *)
      ;;
  esac
fi

#
# export variables to be accessed by XOP trough the "GetEnv" function.
#
export XOP_HOME
export XOP_RC
export XOP_MODE
export XOP_TASK
export IDL_DIR

#
# Display options
#
echo "XOP_HOME: $XOP_HOME"
echo "XOP_MODE: $XOP_MODE"
echo "XOP_TASK: $XOP_TASK"
echo "IDL_DIR: $IDL_DIR"

#
# Start xop
#
echo "Starting XOP task: $XOP_TASK in $XOP_MODE mode."
case $XOP_MODE in
  EMBEDDED) 
    XOP_COMMAND="$XOP_HOME/xopexec_`$XOP_HOME/xop_uname`"
    echo "Executing: $XOP_COMMAND"
    $XOP_COMMAND
    ;;
  USER) 
    XOP_COMMAND="$IDL_DIR/bin/idl $XOP_HOME/mainu.pro"
    echo "Executing: $XOP_COMMAND"
    $XOP_COMMAND
    ;;
  DEVELOPER)  
    XOP_COMMAND="$IDL_DIR/bin/idl $XOP_HOME/maind.pro"
    echo "Executing: $XOP_COMMAND"
    $XOP_COMMAND
    ;;
  *)
    echo "Mode $XOP_MODE not recognized."
    exit 2
    ;;
esac

exit 0
