#!/bin/sh

#
# This script receives the name of a FITS image on the command-line as
# a first argument. If an ds9 session is already running, the image is
# displayed in it. Otherwise a new ds9 is launched to display the
# images. There is no need to start up ds9 beforehand.
#
# The programs ds9, xpaaccess and xpaset are expected to be available
# in the PATH and are compatible in terms of the XPA protocol.
#
# Created:  N. Kornweibel

file=$1
if [ "$file" = "" ] ; then
  echo "Usage: $0 filename"
  exit 1
fi

#
# Build absolute path name for the file to load and check that
# the file exists. The absolute path name is needed since we may
# have changed our working directory from where ds9 was started.
#

dirname=`dirname $file`
if [ "$dirname" = "." ] ; then
  pathname="`pwd`/$file"
else
  pathname=$file
fi

saorunning=`xpaaccess ds9`
#
# Either:
#   Cannot connect to an ds9? Launch one.
#   It is started with the specified file
# Or:
#   Send the image to the existing ds9 display.
#
if [ "$saorunning" = "no" ] ; then
  exec ds9 $pathname & 
else
  xpaset ds9 fits < $file 
fi
