#!/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)
#     -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)]
#-

#### THIS SECTION IS NEEDED FOR BEING USED WITH THE 
#### MACOS LAUNCHER. MUST REMAIN COMMENTED OTHERWISE
# open /Applications/Utilities/X11.app
# export DISPLAY=localhost:0.
# export XOP_TASK=xop
# export XOP_HOME=$1/Contents/Resources/xop2.3
# export XOP_WD=$XOP_HOME/tmp
# $1/Contents/xop2.3
#### 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
  export XOP_HOME=/scisoft/xop2.3
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 6.4
# 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) 
    export IDL_DIR_DEVELOPMENT=/sware/com/idl-7.0/idl70
    ;;
  *)
    # This is the directory of my local IDL (licensed) 
    export IDL_DIR_DEVELOPMENT=/sware/com/idl-7.0/idl70
    # It may need a particular place for the license file
    # export LM_LICENSE_FILE= /sware/com/idl-7.0/license/license.dat
    ;;
esac

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

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

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

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

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

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

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

# Define default XOP mode 
export XOP_MODE=EMBEDDED

#
# Optional environmental variables
#

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

# DABAX_HELP
# export DABAX_HELP=$XOP_HOME/doc

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


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

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

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

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

# XPLOT_DEFAULTS. Xplot defaults file
# export XPLOT_DEFAULTS=

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

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

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

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


#
# Set IDL_PATH
#

case $XOP_MODE in
  EMBEDDED ) 
    export IDL_DIR=$XOP_HOME/idl70
    export IDL_PATH="<IDL_DEFAULT>"
    ;;
  DEVELOPER )
    export IDL_DIR=$IDL_DIR_DEVELOPMENT
    ;;
  ?)
    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)  
    XOP_COMMAND="$IDL_DIR/bin/idl $XOP_HOME/maind.pro"
    echo "Executing: $XOP_COMMAND"
    echo " "
    $XOP_COMMAND
    ;;
  *)
    echo "Mode $XOP_MODE not recognized."
    exit 2
    ;;
esac

exit 0
