;+
; NAME:
;     CHECK_SCHED_STATE
; PURPOSE:
;     Support routine for the SCHEDULE.  It checks the SCHED_STATE
;     file and returns 1 if okay to proceed, or 0 if the schedule
;     should remain IDLE.  It also changes the contents of SCHED_STATE
;     file as necessary.
; CATEGORY:
;     OVRO APC SCHEDULE SUPPORT
; CALLING SEQUENCE:
;     result = check_sched_state(base)
; INPUTS:
;     base      The base widget number of the calling instance of
;                 SCHEDULE.  This will be compared to the number
;                 written into the SCHED_STATE file when ACTIVE, to
;                 see if we have control, or whether another instance
;                 of SCHEDULE has control.
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
; ROUTINES CALLED:
; OUTPUTS:
;     result    will be set to 1 if it is okay to proceed normally,
;                 or 0 (FALSE) if the calling instance of SCHEDULE
;                 must wait in IDLE mode.
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 07-Sep-2000 by Dale E. Gary
;-

function check_sched_state,base

   ; The CHECK_SCHED_STATE procedure does the following:
   ; Opens and reads the SCHED_STATE file.  If it contains the string 'IDLE',
   ; we change it to 'ACTIVE', write out a unique widget number, and
   ; return TRUE (1).  If it contains 'ACTIVE', we check whether this is
   ; because we set it, or whether it was set by another instance of
   ; SCHEDULE.  If ours, we return TRUE (1).  If another instance of
   ; SCHEDULE has control, we return FALSE (0).  If it contains 'INTREQ',
   ; we set it to 'IDLE' and return FALSE (0).  The interrupting schedule
   ; will have 1 minute to rewrite to 'ACTIVE' and take control, otherwise
   ; we will get control back.

   ON_IOERROR, bail
   lun = 0

   ; Open the SCHED_STATE file
   openu,lun,/get_lun,!defaults.workdir+'sched_state.dat'

   ; Read the contents of the file (only a single line of text)
   line = ''
   readf,lun,line
   free_lun,lun

   ; File can contain only the strings 'IDLE', 'ACTIVE nnnnn', or 'INTRQ nnnnn',
   ; where nnnnn is the (5-digit) base widget number of the active instance
   ; of SCHEDULE.  Thus, we can act according to the length of LINE.
   case strlen(line) of
      4: begin
           ; The file contains IDLE, so change to ACTIVE, with our base
           ; widget number, and return TRUE (1) indicating okay to proceed
           openw,lun,/get_lun,!defaults.workdir+'sched_state.dat'
           line = 'ACTIVE '+string(base,format='(I5.5)')
           printf,lun,line
           free_lun,lun
           return,1
         end
     11: begin
           ; The file contains INTRQ, so see if this is our interrupt request.
           thisbase = 0L
           reads,strmid(line,6,5),thisbase

           ; It is our interrupt request, so leave as is, and return FALSE (0)
           if (thisbase eq base) then return,0

           ; It is not our interrupt request, so change to IDLE and return
           ; FALSE (0) to relinquish control.
           openw,lun,/get_lun,!defaults.workdir+'sched_state.dat'
           line = 'IDLE'
           printf,lun,line
           free_lun,lun
           return,0
         end
     12: begin
           ; The file contains ACTIVE, so see if we have control.
           thisbase = 0L
           reads,strmid(line,7,5),thisbase
           if (thisbase eq base) then return,1 else return,0
         end
     else: begin
           ; This should never happen, but if it does, set to ACTIVE to
           ; take control.
           print,'CHECK_SCHED_STATE: Illegal line length in file '+!defaults.workdir+'sched_state.dat'
           openw,lun,/get_lun,!defaults.workdir+'sched_state.dat'
           line = 'ACTIVE '+string(base,format='(I5.5)')
           printf,lun,line
           free_lun,lun
           return,1
         end
    endcase

bail:
   ; There was an I/O error, which probably means the file is open by
   ; another instance of SCHEDULE (a rare, but possible occurrence), so
   ; to be safe we return FALSE (0) and wait in IDLE mode.
   if (lun ne 0) then free_lun,lun
   return,0

end