#!/bin/csh -f
#+
#
# en e n
#
# moves the telescope east e and north n arcsecs relative to base,
# its current position.
#
#-
#
# 20051122 - MB: added -noask flag to wfao
# 20060926 - jlyke: added long workaround to FSM faults caused by 
#                   large moves (max following error exceeded)
# 20080606 - jlyke/rdc: fixed issue that turned wait4ao on incorrectly
# 20180527 - jlyke: workaround for TCSU unable to handle 0 0 offsets
# 20210626 - jlyke: Force FSM moves to hope fix the offsetting issues; 
#                   Did not add this part to the breakup of large moves
# 20210901 - jlyke: Force TSS moves similar to FSM, add both to breakup
#                   of large moves, and break up moves into 3 modify's
#

# Boiler plate for "-h" support for command autohelp.

if ("$1" == "-h") then
    help $0
    exit $status
endif

# Boilerplate for syncheck.
# Note that the boiler plate should be transparent for all usages,
# but to make correct use of syncheck you will need to specify the
# correct pattern.

 set noglob
 set CheckStatus = `syncheck -command $0 $* -pattern float float` 
 unset noglob

if ("$CheckStatus" != "OK") then
    help $0
    exit 1
endif

# End of help/syncheck boiler plate.

# set max size of a single move (must be integer)
set threshold = 90
# define zero for comparisons
set format = "%.3f\n"
set z = 0
set zero = `printf "$format" $z`
set negz = `echo -$zero`


if (${#argv} == 2) then
  # check for 0 0 offsets and exit
  set ratotal = `printf "$format" $1`
  set dectotal = `printf "$format" $2`
  
  if ( $ratotal == "$zero" || $ratotal == "$negz" ) then
    if ( $dectotal == "$zero" || $dectotal == "$negz" ) then
      # both offsets are zero...skip
      exit
    endif
  endif

  set dmstat = `show -s ao -terse aodmstat`
  set dtstat = `show -s ao -terse aodtstat`
  set aofmmove = `show -s ao -terse aofmmove`

  if ( $dtstat == "closed" && $aofmmove == "true" ) then
    # moving in steps requires wait4tt or wait4ao to be on
    wait4ao save
    if ( $dmstat == "closed" ) then
      wait4ao on
    else
      wait4tt on
    endif


    #################################################
    ### handle negative numbers
    #################################################
    set rasign = `echo $ratotal | cut -c 1`
    set decsign = `echo $dectotal | cut -c 1`

    if ( $rasign == - ) then
        set rasign = -1.0
        set ratotal = `math -1 x $ratotal`
    else
        set rasign = 1.0
    endif

    if ( $decsign == - ) then
        set decsign = -1.0
        set dectotal = `math -1 x $dectotal`
    else
        set decsign = 1.0
    endif

    #################################################
    ### csh requires integers for comparison
    ### build up offset lists
    #################################################
    set raint = `echo $ratotal | cut -f 1 -d .`
    set decint = `echo $dectotal | cut -f 1 -d .`

    if ( $raint < $threshold ) then
        set raoff = `math $rasign x $ratotal`
    else
        set raoff = ""
        while ( $raint > $threshold ) 
            set raoff = ( $raoff `math $rasign x $threshold` ) 
            set ratotal = `math $ratotal - $threshold`
            set raint = `echo $ratotal | cut -f 1 -d .`
        end
        set raoff = ( $raoff `math $rasign x $ratotal` )
    endif

    if ( $decint < $threshold ) then
        set decoff = `math $decsign x $dectotal`
    else
        set decoff = ""
        while ( $decint > $threshold ) 
            set decoff = ( $decoff `math $decsign x $threshold` ) 
            set dectotal = `math $dectotal - $threshold`
            set decint = `echo $dectotal | cut -f 1 -d .`
        end
        set decoff = ( $decoff `math $decsign x $dectotal` )
    endif

    #################################################
    ### now we need lists of equal length
    #################################################
    while ( ${#raoff} != ${#decoff} ) 
    if ( ${#raoff} > ${#decoff} ) then
        while ( ${#raoff} != ${#decoff} )
            set decoff = ( $decoff 0.0 )
        end
    else
        while ( ${#raoff} != ${#decoff} )
            set raoff = ( $raoff 0.0 )
        end
    endif
    end

    #################################################
    ### now send the moves
    #################################################
    wfao -noask
    while ( ${#raoff} > 0 ) 
        modify -s dcs silent raoff=$raoff[1] 
        modify -s dcs silent decoff=$decoff[1] 
        modify -s dcs silent rel2curr=t
        sleep 2
        modify -s ao obfmgoim=1
        modify -s ao obtsgo=1
        wfao
        shift raoff
        shift decoff
    end
    wait4ao restore
    #################################################
    ### else to if ( $dtstat == "closed" ...
    #################################################
  else
    wfao -noask
    modify -s dcs silent raoff=$1 
    modify -s dcs silent decoff=$2 
    modify -s dcs silent rel2curr=t
    sleep 2
    waitfor -s dcs axestat=tracking
    modify -s ao obfmgoim=1    
    modify -s ao obtsgo=1
  endif
else
  echo "Usage: en e n"
endif