#!/bin/bash
# 
#  Copyright (C) 2007,2008,2012  Smithsonian Astrophysical Observatory
#
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#



function CheckDS9 
{
    
    tmpVal="`echo \"$2\" | sed -e 's/[\] / /g'`"

    if [ ! -e "$tmpVal" ] 
	then
	echo "$1: imager '$2' not found"
	exit 1
    elif [  ! -x "$tmpVal"  -o ! -f "$tmpVal" ]  
	then	
	echo "$1: Can not execute '$2'"
	exit 1	 
    fi
}


function GetPath
{
    if [ $# -ne 1 ] 
	then
	return 1
    fi

    tmpVal=`echo "$1" | sed -e "s/^[/].*$//"`
    
    if [ "$tmpVal" = "" ] 
	then
        tmpPath=`dirname "$1"`
    else
	tmpPath="`pwd`/$1"
	tmpPath=`dirname "$tmpPath"`
    fi 

    echo "$tmpPath"

}




DS9=ds9
OBSVIS_STARTUP_SCRIPT=obsvis.tcl
DS9_PATH=""
OBSVIS_VERSION=1.2
DS9_REQUIRED_VERSION=7.0
CIAO_INIT_SCRIPT=`GetPath $0`/setup_obsvis.sh
OBSVIS_MODE=user
OBSVISHOME=$HOME
#initialize some variables

#OBSVIS_PKG_PATH=${ASCDS_INSTALL}/lib/tcltk/packages/obsvis

if [ -f $CIAO_INIT_SCRIPT ] 
then
    . $CIAO_INIT_SCRIPT
fi

export OBSVIS_PKG_PATH
export OBSVIS_VERSION
export DS9_REQUIRED_VERSION
export OBSVISHOME

tmpParams=""
tmpDebug=""
tmpSourceParams=""

# check command line options and intercept those that are obsvis specific

if [ $# -gt 0 ] 
    then

    while [ $# != 0 ]
      do
      if [ $1 = "-source" ]
	  then
	  shift
	  tmpV="`echo \"$1\" | sed -e 's/ /\\\\\ /g'`"
	  echo "$tmpV"
	  tmpSourceParams="${tmpSourceParams} -source \"$tmpV\""
	  echo $tmpSourceParams
      elif [ $1 = "-debug" ] 
	  then
	  tmpDebug=$1
      elif [ $1 = "-imager" ] 
	  then
	  if [ $# -gt 1 ] 
	      then
	      shift
	  else
	      echo "$0: Option '-imager' requires ds9 path"
	      exit 1
	  fi
	  DS9_PATH=$1
      elif [ $1 = "-package" ] 
	  then
	  if [ $# -gt 1 ] 
	      then
	      shift
	  else
	      echo "$0: Option '-package' requires ds9 path"
	      exit 1
	  fi
	  OBSVIS_PKG_PATH=$1 
      elif [ $1 = "-home" ] 
	  then
	  if [ $# -gt 1 ] 
	      then
	      shift
	  else
	      echo "$0: Option '-home' requires a path"
	      exit 1
	  fi
	  OBSVISHOME=$1
      elif [ $1 = "-admin" ] 
	  then
	  OBSVIS_MODE=admin
      elif [ $1 = "--help" -o  $1 = "-help" ]  
	  then
	  echo "usage: $0 [ds9_options] [-imager imager_path] [-package packag_path] [-help] [-v]"
	  echo ""
	  echo "    ds9_options         - see ds9 help for command line options"
	  echo ""
	  echo "    -imager imager_path - use imager_path as imager in obsvis"
	  echo ""
	  echo "    -package package_path - use path as location of obsvis tcl/tk package"
	  echo ""
	  echo "    -v                  - display obsvis version on stdout"
	  exit 0
      elif [ $1 = "-v" -o $1 = "-version" ]
	  then
	  echo "obsvis version $OBSVIS_VERSION"
	  exit 0
      else 
	  tmpParams="${tmpParams} \"$1\" "
      fi
      shift
    done
fi


# check if there is ds9 in users path

if [ "$DS9_PATH" = "" ] 
    then

    DS9_PATH=`type -p  $DS9`

    
    # if no ds9 in users path check if ciao environment variable for ds9 
    # has been set and use it as imager path

    if [ "$DS9_PATH" = "" ] 
	then
	if [ "${ASCDS_IMAGER_PATH}" != "" ] 
	    then
	    DS9_PATH=${ASCDS_IMAGER_PATH}/$DS9
	else
	    DS9_PATH=$DS9 
	fi
    fi
fi

# check if imager is ok

CheckDS9 "$0" "${DS9_PATH}"

# initialize package path for tcl/tk


if [ "$OBSVIS_PKG_PATH" = "" ] 
    then
    OBSVIS_PKG_PATH=`GetPath $0`
fi

if [ ! -f "${OBSVIS_PKG_PATH}/${OBSVIS_STARTUP_SCRIPT}" ] 
    then
    echo "$0: '${OBSVIS_PKG_PATH}/${OBSVIS_STARTUP_SCRIPT}' not found"
    exit 1
fi


export OBSVIS_PKG_PATH
export OBSVIS_MODE

PATH="${PATH}:${OBSVIS_PKG_PATH}"

export PATH

#fix for ds9/xpa problem with socket 5137
export XPA_METHOD=local

# execute ds9 with obsvis

DS9_PATH="`echo \"$DS9_PATH\" | sed -e \"s/[\] / /g\"`"
TMP_STARTUP_SCRIPT="`echo \"${OBSVIS_PKG_PATH}/${OBSVIS_STARTUP_SCRIPT}\" | sed -e \"s/ /\\\\\ /g\"`"



eval "exec \"$DS9_PATH\" -source \"$TMP_STARTUP_SCRIPT\"  ${tmpParams} ${tmpSourceParams} ${tmpDebug}"

