#!/bin/sh
#\
exec seqSh "$0" ${1+"$@"}

#
# This script receives the name of a FITS image on the command-line as
# a first argument. If an RTD session is already running, the image is
# displayed in it. Otherwise a new RTD is launched to display the
# images. There is no need to start up an rtdServer beforehand.
#
# Created:  P. Biereichel
# Modified: N. Devillard

set file [lindex $argv 0]
if {"$file" == ""} {
  puts "Usage: $argv0 filename"
  exit 1
}

#
# 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 RTD was started from.
#

set dirname [file dirname $file]
if {"$dirname" == "."} {
  set pathname "[pwd]/$file"
} else {
  set pathname [file join $dirname $file]
}

if { ! [file isfile $pathname] } {
  puts "cannot find file $file: aborting RTD load"
  exit 1
}

#
# Get libraries from VLTROOT
#

lappend auto_path $env(VLTROOT)/lib/rtd
set rtd_fd [connect_to_rtd]
#
# Cannot connect to an RTD? Launch one.
#
if {$rtd_fd == 0} {
  puts "cannot connect to rtd: launching a new RTD session"
  exec $env(VLTROOT)/bin/rtd $pathname &
} else {
#
# Display image in already existing RTD session
#
  puts "displaying $pathname"
  puts $rtd_fd "config -file $pathname"
}

