#!/bin/csh -f
#+
# wffcs -- wait for FCS to lock on target
#
# Purpose:
#	Wait for FCS to begin tracking.
#
# Usage:
#	wffcs [-t timeout]
# 
# Arguments:
#	timeout = number of seconds to wait for lock
# 
# Output:
#	None
# 
# Restrictions:
# 
# Exit values:
#	0 = normal completion
#	1 = wrong number of arguments
#	2 = FCSTRACK script is not running
#	3 = timed out
#
# Example:
#	1) Wait until FCS starts tracking
#		wffcs
#-
# Modification history:
#	2003-May-02	GDW	Original version
#	2003-May-04	GDW	Changed to use FCSTRACK instead of FCSSTA
#	2003-Oct-29	GDW	Added fcs_status check
#	2009-Oct-28	GDW	Added timeout
#-----------------------------------------------------------------------

set buf = $0
set cmd = $buf:t
set usage = "Usage: $cmd"

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

  # check for -no_init 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 > 0 ) then
    echo "$usage"
    exit 1
endif

# verify that fcstrack is running and attempting to track...
fcs_status
if ( $status ) then
    exit 2
endif

# wait for FCS to track...
if ( $?timeout ) then
    waitfor -s deifcs -t $timeout FCSTRACK = "on target"
    if ( $status != 0 ) exit 3
else
    waitfor -s deifcs FCSTRACK = "on target"
endif

exit
