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

#
# $Log: not supported by cvs2svn $
# Revision 1.1  2000/03/02 19:00:04  soliday
# First version of CreateLinacPVTableSourceFile.
# The output of this program should be placed at
# /home/helios/oagData/deviceConfig/linac/pvTable.tcl
#
#
#

# set the path to pick up OAG libraries
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 CVSRevisionAuthor "\$Revision: 1.2 $ \$Author: soliday $"
APSStandardSetup

# set output file name
if {[llength $argv] != 1} {
    puts stderr "usage: CreateLinacPVTableSourceFile <output>\n[join [split $CVSRevisionAuthor $] ""]"
    exit 1
}
set outputFile [lindex $argv 0]

# get list of all global variables so that we can discard them later
set initialVarList [info vars]

# use a modified version of APSLinkToLINACPVs
# so that it won't look for the pvTable.tcl file
set apsDeviceConfigDir /home/helios/oagData/deviceConfig
proc APSLinkToLINACPVsModified {} {
    global apsDeviceConfigDir
    set fileList [glob -nocomplain $apsDeviceConfigDir/linac/*.pvTable] 
    if [llength $fileList] {
        if [catch {APSLinkToDevicePVs -fileList $fileList} result] {
            return -code error "APSLinkToLINACPVs: $result"
        }
    }
}

# read through the .pvTable files and set the global variables
if [catch {APSLinkToLINACPVsModified} result] {
    puts "error: $result"
    exit 1
}

# get list of all global variables
set finalVarList [info vars]

# create list of global variables that we will ignore
set ignoreVarList "APSErrorText apsTmpFileListArray apsTmpStringCount apsUniqueName auto_index auto_oldpath dp_atexit_callbacks dp_atexit_inited initialVarList result"

# create list of global variables that will be appended to rather then set
set lappendVarList "apsLinkToDevicePVsFileList"

# open output file
if [catch {open $outputFile w} fID] {
    puts "error: $fID"
    exit 1
}

# write source code to output file
foreach var [lsort $finalVarList] {
    if {([lsearch -exact $initialVarList $var] == -1) &&
	([lsearch -exact $ignoreVarList $var] == -1)} {
	puts "* $var"
	puts $fID "global $var"
	if {[array exists $var]} {
	    puts $fID "array set $var [list [array get $var]]"
	} else {
	    if {[lsearch -exact $lappendVarList $var] == -1} {
		puts $fID "set $var [list [set $var]]"
	    } else { 
		puts $fID "lappend $var [set $var]"
	    }
	}
    }
}

# close output file
catch {close $fID}
