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

# $Log: not supported by cvs2svn $
# 
#

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 rootname SRBPLD
set install 0
set start 0
set usage {usage: makeSRBPLDOffsetAndLImitReqFile [-output <filename> | -install <1|0>]}
set output ""
set args $argv
set recordFile /home/helios/oagData/pvdata/iocRecNamesOAG.sdds

if {[APSStrictParseArguments {output install start}] || \
      (![string length $output] && !$install) || \
      ([string length $output] && $install)} {
    puts stderr "Error for $argv0 in [pwd]: $usage"
    exit 1
}

cd /home/helios/oagData/SCR/requestFiles

if [file exist $rootname.req] {
    set lastFile [file readlink $rootname.req]
} else {
    set lastFile ""
}

if !$install {
    if [file exists $output] {
        puts stderr "Error for $argv0 in [pwd]: $output exists"
        exit 1
    }
} else {
    if ![string length $lastFile] {
        set lastFile $rootname.req-0000
    }
    set output [APSNextGenerationedName -name $lastFile -separator - -newFile 1]
}

#there are 35 IDs, 18 BPLDs, 1, 3, 5, ... 35
#for BM, there are additional sectors 37 and 39
set configFile /home/helios/oagData/sr/BPLD/BPLD_Config/config.sdds
set BPLDLimits /home/helios/oagData/sr/BPLD/BPLD_Config/BMLimits.sdds
set nameList [exec sdds2stream -col=Name $BPLDLimits]
set xLimits [exec sdds2stream -col=xLimit $BPLDLimits]
set yLimits [exec sdds2stream -col=yLimit $BPLDLimits]
foreach name $nameList x $xLimits y $yLimits {
    set limit($name.X) [expr $x*1000]
    set limit($name.Y) [expr $y*1000]
    puts "$x $y $limit($name.X) $limit($name.Y)"
}

set tmpRoot /tmp/[APSTmpString]
puts $tmpRoot

set fileList1 ""
set fileList2 ""
for {set sector 1} {$sector<40} {incr sector 2} {
    if [catch {exec sddsprocess $configFile $tmpRoot.$sector -filter=col,BPLDSector,$sector,$sector } result] {
	puts stderr "Error1: $result"
    }
    APSAddToTmpFileList -ID bpld -fileList $tmpRoot.$sector
    set deviceList [exec sdds2stream -col=DeviceName $tmpRoot.$sector]
    set indexList [exec sdds2stream -col=BPLDIndex $tmpRoot.$sector]
    
    set pvList ""
    set valList ""
    set descList ""
    set unitsList ""
    set beamList ""
    set cateList ""
    foreach device $deviceList index $indexList {
	set name [string range $device 3 end]
	foreach plane {X Y} {
	    #BM limit
	    lappend pvList S[format %02d $sector]-LMPS:BM_${plane}${index}_TH-SP
	  #  puts "$limit($name.$plane)"
	    lappend valList $limit($name.$plane)
	    lappend descList "$device $plane Limit"
	    lappend unitsList um
	    lappend beamList S[format %02d $sector]
	    lappend cateList "BM_BPLD_Limit"

	    #BM offset
	    lappend pvList  S[format %02d $sector]-LMPS:BM_${plane}${index}_Offset-SP
	    lappend valList 0
	    lappend descList "$device $plane Offset"
	    lappend unitsList urad
	    lappend beamList S[format %02d $sector]
	    lappend cateList "BPLD_Offset"
	}
    }
    #ID limit PVs
    foreach ID {ID1 ID2} {
	foreach pl {H V} offset {1000 500} {
	    foreach nm {1 2 3} shape {Diamond1 Diamond2 Rect} {
		lappend pvList S[format %02d $sector]-LMPS:BPLD:${ID}:AIOL-${pl}${nm}-SP
		lappend valList $offset
		lappend descList "S[format %02d $sector] $ID $pl $shape Offset Limit"
		lappend unitsList um
		lappend beamList S[format %02d $sector]
		lappend cateList "ID_BPLD_Limit"
	    }
	    #angle 
	    lappend pvList S[format %02d $sector]-LMPS:BPLD:${ID}:AIAL_${pl}-SP
	    lappend valList 250
	    lappend descList "S[format %02d $sector] $ID $pl Rect Angle Limit"
	    lappend unitsList urad
	    lappend beamList S[format %02d $sector]
	    lappend cateList "ID_BPLD_Limit"
	}
    }
    
    if [catch {exec sddsmakedataset -pipe=out \
		   -col=ControlName,type=string -data=[join $pvList ,] \
		   -col=ValueString,type=string -data=[join $valList ,] \
		   -col=Description,type=string "-data=[join $descList ,]" \
		   -col=ReadbackUnits,type=string "-data=[join $unitsList ,]" \
		   -col=Beamline,type=string -data=[join $beamList ,] \
		   -col=Category,type=string "-data=[join $cateList ,]" \
		   | sddsprocess -pipe \
		   -define=col,Tolerance,0 \
		   -print=col,ControlType,pv \
		   -print=col,IsNumerical,y,type=character \
		   -print=col,OpsIntervention,n,type=character \
		   -print=col,IsReadOnly,n,type=character \
		   -print=col,IsProtected,n,type=character \
		   | tee $tmpRoot.$sector.set \
		   | sddsconvert -pipe=in -del=col,ValueString $tmpRoot.$sector.pv } result] {
	return -code error "Erro creating BPLD req file for sector $sector: $result"
    }
    lappend fileList1 $tmpRoot.$sector.pv
    lappend fileList2 $tmpRoot.$sector.set
}

if [catch {eval exec sddscombine $fileList1 -merge $output } result] {
    puts stderr "Error creating output1: $result"
    exit 1
}
if $start {
    if [catch {eval exec sddscombine $fileList2 -merge $output.set } result] {
	puts stderr "Error creating output2: $result"
	#exit 1
    }
    if [catch {exec sddsprocess $output.set /home/oxygen/DIAG/oagData/SCR/requestFile/$rootname.req.set \
		   "-match=col,Beamline=S01,Beamline=S03,|,Beamline=S05,|"  } result] {
	puts stderr "Error creating lab set file: $result"
	#exit 1
    }
}
if $install {
    catch {exec rm $rootname.req}
    exec ln -s $output $rootname.req
}

if {0} {
#generate lab one
if [catch {exec sddsprocess $rootname.req /home/oxygen/DIAG/oagData/SCR/requestFile/$rootname.req \
	       "-match=col,Beamline=S01,Beamline=S03,|,Beamline=S05,|" } result] {
    puts stderr "Error creating lab request: $result"
    exit 1
}
}
exit 0
    
