# -*- tcl -*-
# -- extend global pkgIndex.tcl

set   packs(_) {}
unset packs(_)


proc read-pkg-file {name} {
    global packs

    puts "Reading index $name"

    set f [open $name r]

    while {[gets $f line] >= 0} {

	if {  [regexp {^ *#}               $line]} {continue}
	if {  [regexp {^[ 	]*$}       $line]} {continue}
	if {! [regexp {^[ 	]*package} $line]} {continue}

	set pkg  [lindex $line 2]
	set vers [lindex $line 3]

	puts "\tFound package $pkg $vers"

	set packs($pkg$vers) $line
    }

    close $f
}


set gpkg [info library]/pkgIndex.tcl

if {[file exists $gpkg]} {
    read-pkg-file $gpkg
}


foreach f $argv {
    if {[file exists $f]} {
	read-pkg-file $f
    }
}


puts "Writing global index $gpkg"

set f [open $gpkg w]

puts $f "# Tcl package index file, version 1.0"
puts $f "# -*- tcl -*-"
puts $f ""

foreach p [array names packs] {
    puts "\tAdding package $p"
    puts $f "$packs($p)"
}

close $f
