#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set env(EPICS_CA_MAX_ARRAY_BYTES) 10000000
#big array needs for FPGA turn history

APSStandardSetup
set args $argv
set subSector ""
set monFile ""
set outputFile ""
set timeout 600
set turnHistory turn
set scalarFile ""
set cleanup 0
APSStrictParseArguments {subSector monFile outputFile timeout turnHistory scalarFile cleanup}

proc ReArm {args} {
    global srMode subSector turnHistory logFile
    if {$srMode==1 || $srMode==2} {
	#re-arm FPGA bpms in user operation mode (1) or "SUPLEMENTAL TIME" (2)
	after 1000
	if [catch {APSArmFPGABpm -subSector $subSector -type $turnHistory -application collectFPGABpmHistory-${subSector}-${turnHistory}History } result] {
	    puts stderr "Error in arming $subSector with type $turnHistory: $result"
	    catch {exec echo "[exec date] APSArmFPGABpm -subSector $subSector -type $turnHistory -application collectFPGABpmHistory-${subSector}-${turnHistory}History" >> $logFile}
	    catch {exec echo "[exec date] Error in arming $subSector with type $turnHistory: $result" >> $logFile}
	    exit 1
	}
    }
}

set logFile /tmp/oagMpsDumpLog

set argList {subSector monFile outputFile scalarFile}
foreach arg $argList {
    if ![string length [set $arg]] {
	puts stderr "Error [set $arg] not provided."
	exit 1
    }
}

if ![file exist $monFile] {
    puts stderr "Error(collectFPGABpmHistory): monitor file $monFile does not exist!"
    exit 1
}

switch $turnHistory {
    "turn" {set armPV ${subSector}:TurnHistoryIsArmed }
    "motion" {set armPV ${subSector}:SlowBeamHistoryIsArmed}
    default {
        puts stderr "invalid turnHistory type provided, must be turn or motion"
        exit 1
    }
}

set timeout0 [expr [clock seconds] + $timeout]
#wait for the bpm being disarmed
while {[clock seconds]<$timeout0} {
    set armed 0
    if [catch {APScavget -list=$armPV -pend=30 -printErrors} result] {
	puts stderr "Error in reading $armPV: $result"
	exit 1
    }
    if $result {
	set armed 1
	after 1000
    } else {
	break
    }
}

if [catch {exec cavget -list=S:DesiredMode -pend=30 -printErrors -num} srMode] {
    puts stderr "Error in read S:DesiredMode: $srMode"
    catch {exec echo "[exec date] Error in read S:DesiredMode (for $subSector): $srMode" >> $logFile}
    #  exit 1
}

if $armed {
    puts stderr "$armPV is still armed after $timeout seconds, no data will be collected."
    exit 1
}
#collecting data
if [catch {exec sddswmonitor -precision=single -pendIO=80 $monFile $outputFile -scalars=$scalarFile -erase } result] {
    if [catch {ReArm} result1] {
	catch {exec echo "[exec date] collectFPGABpmHistory(error in re-arming $subSector after collecting data failed: $result1" >> $logFile }
    }
    puts stderr "collectFPGABpmHistory(error in collecting data for $subSector): $result"
    catch {exec echo "[exec date] collectFPGABpmHistory(error in collecting data for $subSector): $result" >> $logFile }
}

if [catch {ReArm} result] {
    catch {exec echo "[exec date] collectFPGABpmHistory(error in re-arming $subSector after collecting data: $result" >> $logFile }
}

if {$cleanup} {
    catch {file delete $monFile $scalarFile}
}

exit 0
