#!/bin/csh -f
#+
# wfi -- wait for new DEIMOS image to be written to disk
#
# Purpose:
#	With no argument, wait for a new image to be written to disk 
#	in the output directory, and print the name of the image to STDOUT.
#	With an argument, loop until the named image is written to disk.
#
# Usage:
#	wfi [-nowait] [-silent] [-timeout nsec] [image] 
# 
# Arguments:
#	-nowait = do not wait if image not in progress; return immediately
#	-silent = do not print image name
#	-timeout = wait up to nsec seconds for specified image to appear
#	image = name of the image for which to wait.  This should be 
#		the TAIL of the filename only (omit directory component)
# 
# Output:
#	Full image name is written to stdout.
# 
# Restrictions:
#	- If started during image readout, will not return the current image
# 
# Exit values:
#	0 = normal completion
#	1 = wrong number of arguments
#
# Example:
#	1) Wait until next image has been written to disk:
#		wfi
# 
#	2) Wait until image d0813_0032.fits has been written to disk,
#	   checking each time a new image appears.
#		wfi d0813_0032.fits
# 
#	3) Wait until image d0813_0032.fits has been written to disk,
#	   checking every second for up to 60 seconds:
:
#		wfi -t 60 d0813_0032.fits
# 
# 	4) Wait only if an image is being taken...
#		wfi -n
#
#-
# Modification history:
#	2001-Jul-25	GDW	Original version
#	2002-Aug-13	GDW	Extensive mods
#	2002-Oct-04	GDW	Removed exposip checks
#	2014-Jun-30	GDW	Add -timeout option
#-----------------------------------------------------------------------

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd [-silent] image"
set verb = 1
set image = default

# parse flags...
while ( $#argv > 0 )

  # check for -nowait flag...
  if ( "$1" =~ \-n* ) then
    set nowait=1
    shift
    continue
  endif

  # check for silent flag...
  if ( "$1" =~ \-s* ) then
    set verb = 0
    shift
    continue
  endif

  # check for timeout flag...
  if ( "$1" =~ \-t* ) then
    shift
    set timeout = $1
    shift
    continue
  endif

  # exit flag check if no flags remain...
  break

end

# verify args...
if ( $#argv > 1 ) then
    echo "$usage"
    exit 1
endif

# parse args...
if ( $#argv >= 1 ) then
    set image = $1
    shift
endif

# in no-wait mode we return immediately if no image is in progress...
if ( $?nowait ) then
    set buf = ( `show -terse -s deiccd eraseip exposip pauseip wcrate wdisk` )
    if ( "$buf" !~ *true* ) exit 0
endif

if ( $?timeout && "$image" != "default" ) then

    # in timeout mode we wait up to nsec seconds for the specified image...
    @ i = 0
    while (1)
	@ i++
	set new = `lastimage -tail`
	if (  "$image" == "$new" ) break
	if (  $i >= $timeout ) exit 1
	sleep 1
    end

else

    # loop until (a) the specified image is written,
    # or (b) a new image is written... 
    @ i = 0
    while (1)
	@ i++
	set new = `lastimage -tail`
	if ( "$image" == "$new" ) break
	if ( "$image" == "default" && $i > 1 ) break
	waitfor -s deiccd wcrate = t
	waitfor -s deiccd wcrate = f
	waitfor -s deiccd wdisk = t
	waitfor -s deiccd wdisk = f
    end

endif

# echo name if desired...
if ( $verb ) then
    lastimage
endif
exit