#!/bin/bash
#_SH_INSERT_SAO_COPYRIGHT_HERE_(2007,2012)_
#_SH_INSERT_GPL_LICENSE_HERE_
#

usage() 
{
   echo "-- sherpa"
   echo "  usage: sherpa [-x] [-n] [-b] [-rcfile file] [-norcfile] <file>"
   echo "     -x       launch sherpa shell in separate display terminal"
   echo "     -n       do not print banner"
   echo "     -b       batch mode"
   echo "     -rcfile  load a user specifiable preference file"
   echo "     -norcfile do not load any user specifiable preference file (overrides -rcfile)"
   echo "     <file>   Python command file to execute"
   exit 1
}

banner()
{
   default_versionstr="Sherpa 4.6.2"
   echo "-----------------------------------------------------"
   echo "Welcome to Sherpa: CXC's Modeling and Fitting Package"
   echo "-----------------------------------------------------"
   if [ "x${ASCDS_INSTALL}" != "x" ]; then
       if [ -r ${ASCDS_INSTALL}/VERSION ]; then
	   numfields=`head -n 1 $ASCDS_INSTALL/VERSION | awk '{print NF}'`
	   if [ $numfields = "6" ]; then
	       echo "CIAO 4.6 Sherpa version 2 `awk '{print $3" "$4" "$5" "$6}' ${ASCDS_INSTALL}/VERSION`"
	   else
	       echo $default_versionstr
	   fi
       else
	   echo $default_versionstr
       fi
   else
       echo $default_versionstr
   fi
   echo ""
}

printbanner="yes" xterminal= cmd= batch=
scriptFile=
argList=
rcfile=
xwin="no"

while :
do
   case "$1" in
      -x) xwin="yes";;
      -b) batch="yes"; argList="$argList -b";;
      -n) printbanner="no"; argList="$argList -n";;
      -rcfile) shift; rcfile="$1";
	  # Setting the rcfile on the command line ALWAYS overrides
	  # the SHERPARC environment variable setting (if any)
	  # verify that an rcfile was included
	  if test "x$rcfile" = "x" ; then
	     echo "Sherpa ERROR: The -rcfile switch takes one parameter. Please specify new rcfile."
             exit -1
	  else
             first_char=`expr "$rcfile" : '\(.\).*'`
             if test "$first_char" = "-"; then
                echo "Sherpa ERROR: The -rcfile switch takes one parameter. Please specify new rcfile."
                exit -1
             fi
	  fi
	  if [ ! -e "$rcfile" ]; then
	     echo "Sherpa ERROR: rcfile $rcfile does not exist."
             exit -1
	  fi
	  SHERPARC=$rcfile
	  export SHERPARC ;;
      -norcfile) NOSHERPARC=true; export NOSHERPARC;;
      -l) shift ; lang="$1";
	  if test "$lang" = "python"; then
	     # print deprecation warning, go on to next option
	     echo "Sherpa WARNING: -l is no longer an option; Sherpa now supports Python only."
	     echo ""
	  elif test "$lang" = "slang"; then
	     # print S-Lang no longer supported, exit
	     echo "Sherpa ERROR: Sherpa no longer offers a S-Lang interface."
	     exit -1
	  else
	     # If -l followed by any other string, or no
	     # string, print usage and exit.
	     echo "Sherpa ERROR: -l is no longer an option; Sherpa now supports Python only."
	     echo ""
	     usage 
	  fi
	  ;;
      --) shift; break;;
      -*) usage "bad argument $1";;
      *)
	  len=`expr "$1" : '.*'`
	  if test $len -eq 0; then
             # zero length entry- reached end of argument list processing
             break;
          else
             # support at most 1 script file
             if test "x$scriptFile" = "x"; then
                scriptFile="$1";
             else
                echo "Sherpa ERROR: Expected a single file script: $scriptFile -vs- $1"
                exit -1
             fi
          fi
          ;;
   esac
   shift
done

# now that we've processed the arg list- we can run xterm -e if specified
if test "$xwin" = "yes"; then
   xterm -e $0 $argList $scriptFile;
   exit $?
fi

# If CIAO_SCRIPT_LANG, SHERPA_SCRIPT_LANG, or ~/.sherpa.rc specify
# S-Lang, warn the user S-Lang is no longer supported, and exit.
if test "$SHERPA_SCRIPT_LANG" = "slang"; then
   echo "Sherpa ERROR: Sherpa no longer offers a S-Lang interface."
   echo "SHERPA_SCRIPT_LANG variable is no longer used."
   exit -1
fi

if test "$CIAO_SCRIPT_LANG" = "slang"; then
   echo "Sherpa ERROR: Sherpa no longer offers a S-Lang interface."
   echo "CIAO_SCRIPT_LANG variable is no longer used."
   exit -1
fi

if test -s $HOME/.sherpa.rc ; then
   a=`grep shell $HOME/.sherpa.rc | grep -v '#' | tail -1 | grep -i slang`
   if [ $? = 0 ]; then
      echo "Sherpa ERROR: Sherpa no longer offers a S-Lang interface."
      echo "The 'shell' option in $HOME/.sherpa.rc is no longer used."
   exit -1
   fi
fi

# setup the users home directory with all the Sherpa, ChIPS config files
if test "$batch" = "yes"; then
    sherpa_py "batch"
else
    sherpa_py
fi

cmd=""

# if we have an additional argument treat it as a file for execfile
if test "x$scriptFile" != "x"; then
    if [ ! -r "$scriptFile" ]; then
        echo "Sherpa ERROR: script file $scriptFile either does not exist or cannot be read"
        exit -1
    fi
    cmd="${cmd}execfile('$scriptFile');"
fi

# make sure we use the CIAO version of python
CIAO_PYTHON=${CIAO_APP_PYTHON}
CIAO_IPYTHON=${CIAO_APP_IPYTHON}
export CIAO_PYTHON
export CIAO_IPYTHON


if test "$batch" = "yes"; then
   if test "x$cmd" = "x"; then
      cmd="quit()"
   fi 
   ipython --profile sherpa -c "\"${cmd}\""
else
   if test "$printbanner" = "yes"; then
      banner
   fi
   # python shell options should follow a -- w/ any sherpa command file last 
   if test "x$cmd" = "x"; then
      ipython --profile sherpa
   else
      ipython --profile sherpa -i -c "\"${cmd}\""
   fi
fi
