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

#
# $Log: not supported by cvs2svn $
# Revision 1.13  2000/09/01 20:14:07  emery
# Added filename existance check.
#
# Revision 1.12  2000/06/16 22:11:52  emery
# Added retry argument to burtrb to make sure all Pvs are conected.
# Split up some long sdds command lines.
#
# Revision 1.11  1999/08/18 14:23:49  borland
# Revised previous change to work even when the request file doesn't have
# ControlMode data.
#
# Revision 1.10  1999/08/18 14:17:19  borland
# Now checks for PVs with ControlMode=RO but IsReadOnly=N or n.
#
# Revision 1.9  1999/02/11 14:17:23  borland
# Checks are now consistent with how SCR works.  E.g., a read-only PV is
# one with IsReadOnly!=N or n, rather than IsReadOnly=y or Y.
#
# Revision 1.8  1998/11/20 19:37:31  borland
# Now checks more columns for invalid values (other than y, Y, n, or N).
#
# Revision 1.7  1998/11/20 19:31:57  borland
# Now more careful about checking for both cases with IsNumerical checks.
# Detects IsNumerical with value other than y/n.
#
# Revision 1.6  1998/11/13 18:05:29  borland
# Added -nowarning to an instance of sddsprocess.
#
# Revision 1.5  1998/10/13 15:30:07  borland
# Now checks for PVs with ControlType!=pv.
#
# Revision 1.4  1998/05/19 20:15:07  borland
# Now uses oagtclsh instead of generic version.
#
# Revision 1.3  1996/08/28 18:02:31  borland
# Fixed error that occured when there were no duplicate PVs.
#
# Revision 1.2  1996/08/27 18:22:52  borland
# Allow IdenticalCount on [0,1] to indicate data without duplicates, to work
# around sddssort bug.
#
# Revision 1.1  1996/08/27 15:45:08  borland
# First version.
#
#

set usage "usage: checkRequestFile <filename>"
if [llength $argv]!=1 {
    puts "$usage"
    exit
}

set tmpFile /tmp/[APSTmpString]

set filename [lindex $argv 0]
if ![file exists $filename] {
    puts stderr "Can't find file $filename"
    exit
}

puts "*** Report for file $filename ***"
puts ""

catch {exec sddsprocess $filename -pipe=out -nowarning \
         -define=param,Count,n_rows,type=long \
         | sdds2stream -pipe -param=Count} count
puts "$count PVs in file"
puts ""

set expectedColumnList [lsort \
                          [list ControlName ControlType \
                             Category Beamline IsNumerical \
                             Tolerance IsReadOnly Description \
                             IsProtected OpsIntervention]]

set columnList [APSGetSDDSNames -class column -fileName $filename]
foreach column $expectedColumnList {
    if [lsearch -exact $columnList $column]==-1 {
        puts "Column $column is absent"
    }
}
puts ""

catch {exec sddsprocess $filename -pipe=out -nowarning \
         -match=column,ControlType=pv,! \
         -define=param,Count,n_rows,type=long \
         | sdds2stream -pipe -param=Count} count
if $count {
    puts "$count PVs have invalid ControlType (not \"pv\")---can't continue."
    exit 1
}


catch {exec sddssort $filename -pipe=out -column=Category -unique=count \
         | sddsprocess -pipe \
         -define=param,Categories,n_rows,type=long -nowarning \
         | sddsprintout -pipe \
         -param=Categories -column=Category,format=%30s -title= \
         -column=IdenticalCount,label=Occurrences} result
puts $result
puts ""

catch {exec sddssort $filename -pipe=out -column=Beamline -unique=count \
         | sddsprocess -pipe \
         -define=param,Beamlines,n_rows,type=long -nowarning \
         | sddsprintout -pipe \
         -param=Beamlines -column=Beamline,format=%30s -title= \
         -column=IdenticalCount,label=Occurrences} result
puts $result
puts ""

catch {exec sddssort $filename -pipe=out -column=ControlName -unique=count \
         | sddsprocess -pipe -filter=col,IdenticalCount,0,1,! \
         -define=param,Rows,n_rows,type=long -nowarning \
         | tee $tmpFile \
         | sdds2stream -pipe -param=Rows} count
if $count {
    puts "$count PVs appear more than one time"
    catch {exec sddsprintout $tmpFile -column=ControlName} result
    puts "$result"
    puts ""
}

if [lsearch -exact $columnList Description]!=-1 {
    if [catch {exec sddsprocess -nowarning $filename -pipe=out \
                 {-match=col,Description=+none,Description=,|} \
                 -define=param,Count,n_rows,type=long \
                 | tee $tmpFile \
                 | sdds2stream -pipe -param=Count} result] {
        puts $result
    } else {
        if {$result>0} {
            puts "$result PVs have no useful description"
            puts ""
        }
    }
}

if [lsearch -exact $columnList IsProtected]!=-1 {
    catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsProtected=+n,! \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs are protected"
        puts ""
    }
}
if [lsearch -exact $columnList IsReadOnly]!=-1 {
    catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsReadOnly=+n,! \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs are read-only"
        puts ""
    }
    if [lsearch -exact $columnList ControlMode]!=-1 {
        catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsReadOnly=+n \
                 -match=column,ControlMode=RO \
                 -define=param,Count,n_rows,type=long \
                 | sdds2stream -param=Count -pipe} count
        if $count {
            puts stderr "$count PVs have ControlMode=RO but IsReadOnly=n or N"
            puts ""
        }
    }
}

if {[lsearch -exact $columnList IsProtected]!=-1 && [lsearch -exact $columnList IsReadOnly]!=-1} {
    catch {exec sddsprocess -nowarning $filename -pipe=out \
             -match=col,IsProtected=+n,!,IsReadOnly=+n,!,& \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs are protected and read-only"
        puts ""
    }
}


catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsNumerical=+y \
         -filter=column,Tolerance,-1e300,0 \
         -define=param,Count,n_rows,type=long \
         | sdds2stream -param=Count -pipe} count
if $count {
    puts "$count PVs are numerical but have 0 or negative tolerance"
    puts ""
}

if [lsearch -exact $columnList IsReadOnly]!=-1 {
    catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsNumerical=+y \
             -filter=column,Tolerance,-1e300,0 \
             -match=column,IsReadOnly=+y \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs are classified numerical read-only but have 0 or negative tolerance"
        puts ""
    }
}

catch {exec sddsprocess -nowarning $filename -pipe=out -match=column,IsNumerical=+y,! \
         -filter=column,Tolerance,-1e300,0,! \
         -define=param,Count,n_rows,type=long \
         | sdds2stream -param=Count -pipe} count
if $count {
    puts "$count PVs are classified nonnumerical but have positive tolerance"
    puts ""
}

foreach item {IsNumerical IsReadOnly IsProtected OpsIntervention} {
    if [lsearch $columnList $item]==-1 continue
    catch {exec sddsprocess -nowarning $filename -pipe=out \
             -match=column,$item=+y,$item=+n,|,! \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs have $item equal to something other than y, Y, n, or N."
        puts ""
    }
}

if {[lsearch -exact $columnList IsProtected]!=-1 && \
      [lsearch -exact $columnList OpsIntervention]!=-1} {
    catch {exec sddsprocess -nowarning $filename -pipe=out \
             -match=col,IsProtected=+n,!,OpsIntervention=+n,!,& \
             -define=param,Count,n_rows,type=long \
             | sdds2stream -param=Count -pipe} count
    if $count {
        puts "$count PVs are protected and manual-only"
        puts ""
    }
}

set snapshot /tmp/[APSTmpString]
set logfile  /tmp/[APSTmpString]
if [catch {exec burtrb -f $filename -l $logfile -r 10 \
         | sddsconvert -pipe -binary \
         | sddsxref -pipe=in $filename $snapshot } result] {
    puts "Error making snapshot: $result"
    puts ""
} else {
    puts "Snapshot successfully made."
    puts ""
}

if {[file exists $logfile] && [file size $logfile]==0} {
    file delete $logfile
}
if [file exists $logfile] {
    catch {exec cat $logfile} result
    puts $result
    file delete $logfile
}

set snapshotPV /tmp/[APSTmpString]
set snapshotNPV /tmp/[APSTmpString]
exec sddsprocess -nowarning $snapshot -match=column,ControlType=pv,! \
  $snapshotNPV -define=param,Count,n_rows,type=long
exec sddsprocess -nowarning $snapshot -match=column,ControlType=pv \
  $snapshotPV

catch {exec sdds2stream -param=Count $snapshotNPV} count
puts "$count PVs are bogus (not found by CA)"
if $count {
    catch {exec sddsselect $filename $snapshotPV -match=ControlName -invert -pipe=out \
             | sddsprintout -column=ControlName -pipe} result
    puts $result
    puts ""
} else {
    puts ""
}
file delete $snapshotNPV

exec mv $snapshotPV $snapshot

catch {exec sddsprocess $snapshot -pipe=out -nowarning \
         -match=col,IsNumerical=+y -numbertest=col,ValueString,invert,strict \
         -define=param,Count,n_rows,type=long \
         | tee $tmpFile \
         | sdds2stream -pipe -param=Count} count
puts "$count PVs apparently incorrectly classified as numerical"
if $count {
    catch {exec sddsprintout -column=ControlName -column=ValueString $tmpFile} result
    puts $result
    puts ""
} else {
    puts ""
}

catch {exec sddsprocess $snapshot -pipe=out -nowarning \
         -match=col,IsNumerical=+n -numbertest=col,ValueString,strict \
         -define=param,Count,n_rows,type=long \
         | tee $tmpFile \
         | sdds2stream -pipe -param=Count} count
puts "$count PVs apparently incorrectly classified as nonnumerical"
if $count {
    catch {exec sddsprintout -column=ControlName -column=ValueString $tmpFile} result
    puts $result
    puts ""
}

catch {file delete $tmpFile}
