#! /usr/bin/env tclsh
#
# <20220319.1428.07>
#
# This routine looks at all (well most) of filerunners packages and checks if 
# pkgIndex.tcl revision matches the package provide revision.

# We do this by finding the package index pkgIndex.tcl parsing the name(s) from it
# and issuing package require commands for each.
# We also honor the '.notwindows' and '.notunix' flags and don't go there depending
# on the platform run on.
#
# If there is an error and the pkIndex.tcl was auto generated pkgIndex.tcl
# (i.e. by pkg_mkIndex), we generate a new one.
#
# If the pkIndex.tcl was not auto generated and the versions do not match
# an error is thrown.
#
# We expect one or two parameters, the directory to start our search in and
# a list of directories we must not go to.
#

#
# lets try out server package
set notHere [lassign [regsub {\\} $argv {/}] startHere]

if {$startHere == {}} {set startHere "../packages"}


set notFlag [expr {$::tcl_platform(platform) == "windows" ? ".notwindows" : ".notunix"}]

set orgPwd [pwd]
puts "NotHere $notHere but Here $startHere"
proc searchDirs {dirs} {
  foreach dir $dirs {
    if {$dir ni $::auto_path} {
      lappend ::auto_path $dir
    }
  }
  foreach dir $dirs {
    if {[file exists $dir/$::notFlag] || $dir in $::notHere} {
      puts "skipping $dir ($::notFlag)"
      continue
    }
    searchFiles $dir
    foreach newDir [glob -nocomplain -types d $dir/*] {
      searchDirs $newDir
    }
  }
}
# proc getLine {} {
#   set rtn [lindex $::pkgIFile [incr ::indx]]
#   if {$rtn == {}} {return " "}
#   return $rtn
# }

# proc searchFilesX {dir} {
#   puts -nonewline "looking for '$dir/pkgIndex.tcl'"
#   set found 0
#   foreach pkgI [glob -nocomplain $dir/pkgIndex.tcl] {
#     puts "... found."
#     incr found
#     set ::pkgIFile [split [read [set fid [open $pkgI r]]] \n]
#     close $fid
#     set ::indx -1
#     set failsafe -1
#     while {$::indx < [llength $::pkgIFile] && [incr failsafe] < 1000} {
#       set line {}
#       while {[string index [append line " " [getLine]] end] == {\\}} {}
#       # look for package ifneeded and package provide
#       if {![regexp { *package +ifneeded +([^ ]+) +([^ ]+) .*} $line -> name ver]} {continue}
#       puts "found 'ifneeded' line $name $ver"
#       cd $dir
#       puts "Require $name $ver"
#       package forget $name
      
#       set r [catch {package require $name $ver} er]
#       package forget $name
#       if {$r != 0} {
# 	set errInfo $::errorInfo
# 	set rr [catch {package require $name} err]
# 	puts "'package require $name $ver' fails: $er $::errInfo"
# 	if {$rr == 0} {
# 	  puts "found 'package $name $err' instead."
# 	}
# 	if {[string match "*pkg_mkIndex*" $::pkgIFile]} {
# 	  puts "Trying to rebuild the '$dir/pkgIndex.tcl' file."
# 	  puts "do it here"
# 	} else {
# 	  puts "'$dir/pkgIndex.tcl' not built by 'pkg_mkIndex'. Please fix this manually."
# 	}
#       }
#       cd $::orgPwd
#       continue
#     }
#     if {$failsafe > 999} {
#       puts "failsafe $failsafe"
#     }
#   }
#   if {!$found} {
#     puts ".. notfound."
#   }
# }
proc searchFiles {dir} {
  # puts -nonewline "looking for $dir/pkgIndex.tcl"
  if {![file exists $dir/pkgIndex.tcl]} {
    # puts " ..not found"
    return}
  # puts " ..found."
  set pkgIFile [read [set fid [open $dir/pkgIndex.tcl r]]] 
  close $fid
  if {[string match {*This file is generated by the "pkg_mkIndex" command*} $pkgIFile]} {
    lappend ::remake $dir/pkgIndex.tcl
  } else {
    lappend ::NotRemake  $dir/pkgIndex.tcl
  }
}
  
searchDirs $startHere

if {[info exists remake]} {
  puts "Remakeing these:"
  foreach fi $remake {
    pkg_mkIndex [file dir $fi]
    puts "$fi"
  }
}
if {[info exists NotRemake]} {
  puts "These are not standard. Will NOT remake these:"
  foreach fi $NotRemake {
    puts "$fi"
  }
}



