#!/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)]

APSStandardSetup

set args $argv
set inputFile ""
set outputFile ""
set startTime ""
set stopTime ""
set csvoutput 0
APSStrictParseArguments {inputFile outputFile startTime stopTime csvoutput}

proc ConvertPVstoReadbackNames {pvList} {
    puts "Converting PV names to data logger readback names"

    if {[catch {exec sddsprocess /home/helios/oagData/dataLoggerConfig/timeSeries.config -pipe=out \
                  "-print=column,MonitorFile,/home/helios/oagData/%s/%s.mon,subDirectory,rootname" \
                  "-print=column,LoggingConditions,%s,extraArguments" \
                  "-match=column,GroupName=*" \
                  "-match=column,monitorProgram=sddsglitchlogger,!,monitorProgram=sddspvaglitchlogger,!,&" \
                  "-filter=column,doRun,1,1,doOnePvPerFileRun,1,1,|" | \
                  sdds2stream -pipe=in -col=MonitorFile} monitorFileList]} {
        puts stderr "Error: $monitorFileList"
        exit 1
    }

    set nameList ""
    foreach monitorFile $monitorFileList {
        if {[catch {exec sddsquery $monitorFile -col} results]} {
            puts stderr "$results\n$monitorFile"
            exit 1
        }
        if {[lsearch -exact $results "ControlName"] == -1} {
            continue
        }
        if {[lsearch -exact $results "ReadbackName"] == -1} {
            if {[catch {exec sdds2stream $monitorFile -col=ControlName,ControlName} results]} {
                puts stderr "$results\n$monitorFile"
                exit 1
            }
        } else {
            if {[catch {exec sdds2stream $monitorFile -col=ControlName,ReadbackName} results]} {
                puts stderr "$results\n$monitorFile"
                exit 1
            }
        }
        append nameList "[split $results \n] "
    }
    global pvReadbacks
    foreach pv $pvList {
        if {[string range $pv end-3 end] == ".VAL"} {
            set pv1 [string range $pv 0 end-4]
        } else {
            set pv1 $pv
        }
        set results [lsearch -index 0 -all -inline $nameList $pv1]
        set rbNameList ""
        foreach result $results {
            lappend rbNameList [lindex $result 1]
        }
        set rbNameList [lsort -unique $rbNameList]
        set pvReadbacks($pv) $rbNameList
	puts $pvReadbacks($pv)
    }
}

proc FindPVDataFiles {args} {
    set ReadbackName ""
    set StartTime [clock scan "Jan 01 00:00:00 1990"]
    set EndTime [clock seconds]
    set startDateList ""
    set endDateList ""
    set SampleIntervals "1 2 4 8 16 32 64 128 256"
    APSStrictParseArguments {ReadbackName StartTime EndTime SampleIntervals startDateList endDateList}

    if {[llength $startDateList]} {
        set StartTime [clock scan "[lindex $startDateList 2]/[lindex $startDateList 3]/[lindex $startDateList 0]"]
    }
    if {[llength $endDateList]} {
        set EndTime [clock scan "23:59:59 [lindex $endDateList 2]/[lindex $endDateList 3]/[lindex $endDateList 0]"]
    }
    
    set fileList ""
    set ReadbackName [join [split $ReadbackName / ] + ]
    set year [clock format $StartTime -format %Y]
    set month [clock format $StartTime -format %m]
    set endyear [clock format $EndTime -format %Y]
    set endmonth [clock format $EndTime -format %m]
    while {($year < $endyear) || (($year == $endyear) && ($month <= $endmonth))} {
        append fileList "[lsort [glob -nocomplain /home/helios/oagData/logging/Variable/[string index ${ReadbackName} 0]/${ReadbackName}/log-${year}-${month}.{gz,xz}]] "
        append fileList "[lsort [glob -nocomplain /home/helios/oagData/logging/\{[join $SampleIntervals ,]\}/${ReadbackName}/log-${year}-${month}.{gz,xz,????}]] "
        if {$month == 12} {
            set month 01
            incr year
        } else {
            scan $month %d month
            incr month
            set month [format %02d $month]
        }
    }
    return $fileList
}

#execution starts here
foreach name {inputFile outputFile startTime stopTime} {
    if ![string length [set $name]] {
	puts stderr "Error1: $name not provided!"
	exit 1
    }
}

if {$stopTime=="now"} {
    set stopTime [clock format [clock seconds] -format %Y%m%d-%H%M%S]
    puts $stopTime
}
#time format is yearmonthday-hourminutesseconds
foreach type {start stop} {
    set time [set ${type}Time]
    set ${type}Year [string range $time 0 3]
    set ${type}Month [string range $time 4 5]
    set ${type}Day [string range $time 6 7]
    set ${type}Hour [string range $time 9 10]
    set ${type}Minute [string range $time 11 12]
    set ${type}Second [string range $time 13 14]
}
set start  ${startYear}${startMonth}${startDay}-${startHour}${startMinute}${startSecond}
set stop ${stopYear}${stopMonth}${stopDay}-${stopHour}${stopMinute}${stopSecond}
puts "start time: $start"
puts "stop time:  $stop"
if [string compare $startTime $start]!=0 {
    puts stderr "Error2: startTime format is wrong, it should be YYYYmmDD-HHMMSS"
    exit 1
}

if [string compare $stopTime $stop]!=0 {
    puts stderr "Error2: stopTime format is wrong, it should be YYYYmmDD-HHMMSS"
    exit 1
}
set starttime [clock scan $start -format %Y%m%d-%H%M%S]
set stoptime [clock scan $stop -format %Y%m%d-%H%M%S]

set year $startYear
set fileList ""
set sampleIntervals "1 2 4 8 16 32 64 128 256"
set pvList [exec sdds2stream -col=ControlName $inputFile]

set dataFileList ""
set tmpRoot /tmp/[APSTmpString]

puts $starttime
puts $stoptime

ConvertPVstoReadbackNames $pvList
foreach pv $pvList {
    set fileList ""
    set index 0
    foreach readback $pvReadbacks($pv) {
	set files [FindPVDataFiles -ReadbackName $readback -StartTime $starttime -EndTime $stoptime]
	if ![llength $files] {
	    continue
	}
	incr index
	if [llength $files]>1 {
	    if [catch {eval exec sddscombine $files -pipe=out -merge \
			   | sddssort -pipe -col=Time -unique \
			   | sddsprocess -pipe -filter=column,Time,$starttime,$stoptime  \
			   -redefine=col,$pv,$readback \
			   | sddsconvert -pipe=in -retain=col,Time,$pv  $tmpRoot.$pv.$index } result] {
		puts stderr "Error processing data for $pv: $result"
		exit 1
	    }
	} else {
	   eval  exec cp $files $tmpRoot.$pv.$index
	}
	lappend fileList $tmpRoot.$pv.$index
    }
    if ![llength $fileList] {
	puts stdout "No data loggers for $pv!"
	continue
    }
    puts "getting data for $pv, $tmpRoot.$pv ..."
    if [llength $fileList]>1 {
	if [catch {eval exec sddscombine $fileList -pipe=out -merge \
		       | sddssort -pipe -col=Time -unique \
		       | sddsprocess -pipe=in -filter=column,Time,$starttime,$stoptime $tmpRoot.$pv } result] {
	    puts stderr "Error processing data for $pv: $result"
	    exit 1
	}
    } else {
	exec cp $fileList $tmpRoot.$pv
    }
    file delete -force $fileList
    lappend dataFileList $tmpRoot.$pv
}
if [llength $dataFileList]>1 {
    if [catch {eval exec sddsxref $dataFileList $outputFile -equate=Time -take=* -nowarnings } result] {
	puts stderr "Error combining output files: $result"
	exit 1
    }
} elseif [llength $dataFileList]==1 {
    exec cp $dataFileList $outputFile
} else {
    puts stderr "no log data found for the provided pvs."
    exit 1
}

APSAddToTmpFileList -ID log -fileList $dataFileList
puts "output data in sdds file: $outputFile"
if $csvoutput {
    exec sdds2spreadsheet $outputFile $outputFile.csv
    puts "output csv data in file: $outputFile.csv"
}

exit 0

