#!../src/xotclsh
#!/usr/local/bin/xotclsh
# $Id: s.make.xotcl 1.28 01/04/04 20:04:54+02:00 neumann@mohegan.wu-wien.ac.at $ 
### inEachDir changes now to each directory
### install clears tgarget directory before installing
### object file added (for better -n processing)

set xotclsh [pwd]/../src/xotclsh

Object make

#
# shared lib add files for pkgIndex.tcl
#
make proc mkIndex {name} {
  puts stderr "+++ mkIndex in [pwd]"
  set fls {}
  foreach f [glob -nocomplain *tcl] {
    set F [open $f]; set c [read $F]; close $F
    if {[string match "*package provide*" $c]} { lappend fls $f }
  }

  set so [glob -nocomplain *[info sharedlibextension]]
  # loading libxotcl into xotclsh crashes on some systems
  set p [lsearch -exact $so libxotcl[info sharedlibextension]]
  if {$p != -1} {
    set so [lreplace $so $p $p]
    #puts stderr "new so=<$so>"
  }
  set fls [concat $fls $so]
  
  if {$fls != ""} {
    if {[file exists pkgIndex.tcl]} {
      file delete -force pkgIndex.tcl
    }
   puts stderr "[pwd]: call eval pkg_mkIndex -direct . $fls"
    if {[catch {eval pkg_mkIndex -direct . $fls} errs]} {
      puts stderr "!!! $errs"
    }
   puts stderr "[pwd] done"
  }

  foreach addFile [glob -nocomplain *.add] {
    if {[file exists $addFile]} {
      puts stderr "+++ Appending addfile $addFile to pkgIndex.tcl in $name"
      set OUT [file open pkgIndex.tcl a]
      set IN [file open $addFile] 
      puts -nonewline $OUT [read $IN]
      close $IN; close $OUT
    }
  }
  #puts stderr "+++ mkIndex name=$name, pwd=[pwd] DONE"
}
make proc install {name} {
  [self] instvar target
  set pkg [file tail $name]
  regexp {^[.]/(.*)$} $name _ name
  puts stderr "Installing $pkg in '$target/$name"
  if {$pkg != "." && [file isdirectory $target/$name]} {
    file delete -force $target/$name
  }
  file mkdir $target/$name
  set fls [glob -nocomplain *add *tcl *[info sharedlibextension]]
  if {$fls != ""} {
    eval file copy -force $fls $target/$name
  }
}
make proc test {name} {
  foreach f [glob -nocomplain *.test] {
    puts stderr "Running TEST $f in [pwd]"
    if {[catch {puts stderr [exec $::xotclsh $f]\n} errMsg]} {
      puts "Test $f FAILED\n$errMsg\n"
    } else {
      puts "Test $f SUCCEEDED\n"
    }
  }
} 

make proc getMakefileVar name {
  return [lindex [exec grep ^$name.*= \
    [file dirname [info script]]/../src/Makefile] end]
}
make proc target {} {
  if {![[self] exists target]} {
    set prefix [[self] getMakefileVar prefix]
    set XOLIB  [[self] getMakefileVar INST_XOLIBPKG]
    regsub -all {\$\(prefix\)} $XOLIB $prefix XOLIB
    [self] set target $XOLIB
  }
}
make proc inEachDir {path cmd} {
  #puts stderr "inEachDir $path  [file isdirectory $path]"
  if { [file isdirectory $path] 
       && ![string match *CVS $path]
       && ![string match *SCCS $path]
       && ![string match *Attic $path]
       && ![string match *dbm* $path]
    } {
    set olddir [pwd]
    cd $path
    eval make $cmd $path
    set files [glob -nocomplain *]
    cd $olddir
    foreach p $files { [self] inEachDir $path/$p $cmd }
    #puts stderr "+++ change back to $olddir"
  }
}
make proc in {path cmd} {
  if {[file isdirectory $path] && ![string match *CVS $path]} {
    set olddir [pwd]
    cd $path
    eval make $cmd $path
    cd $olddir
  }
}
make proc clean {name} {
  # pkgIndex.tcl 
  eval file delete core [glob -nocomplain *[info sharedlibextension] config.* *~ #*#]
}

### tcl file-command
rename file tcl_file
Object file

rename open file::open
proc open {f {mode r}} { file open $f $mode }
#file proc open {f {mode r}} { ::open $f $mode }


file array set destructive {
  atime 0       attributes 0  copy 1       delete 1      dirname 0
  executable 0  exists 0      extension 0  isdirectory 0 isfile 0
  join 0        lstat 0       mkdir 1      mtime 0       nativename 0
  owned 0       pathtype 0    readable 0   readlink 0    rename 1
  rootname 0    size 0        split 0      stat 0        tail 0
  type 0        volumes 0     writable 0
}
foreach subcmd [array names file::destructive] {
  file proc $subcmd args {
    #puts stderr " [pwd] call: '::tcl_file [self proc] $args'"
    eval ::tcl_file [self proc] $args
  }
}
### minus n option
Class make::-n
foreach f [file info commands] {
  if {$f == "unknown" || $f == "next" || $f == "self"} continue
  if {![info exists file::destructive($f)] || $file::destructive($f)} {
    #puts stderr destruct=$f
    make::-n instproc $f args {
	puts "--- [pwd]:\t[self proc] $args"
    }
  } else {
    #puts stderr nondestruct=$f
    make::-n instproc $f args {
      set r [next]
      #puts "??? [self proc] $args -> {$r}"
      return $r
    }
  }
}

### command line parameters
if {![info exists argv] || $argv == ""} {set argv -all}
if {$argv == "-n"} {set argv "-n -all"}

Class Script
Script proc create args {
  eval lappend args $::argv
  eval next $args
}
Script instproc unknown args {
  puts stderr "$::argv0: Unknown option ´-$args´ provided"
}

Script instproc n {} {file mixin make::-n}
Script instproc all {} {
  make inEachDir . mkIndex
  make in ../src mkIndex
  make in ../src/lib mkIndex
}
Script instproc test {} {
  make inEachDir . test
  make inEachDir ../apps test
}
Script instproc install {} {
  make instvar target
  make target
  #set target [make set target]
  if {[file isdirectory $target]} {
    #file delete -force $target
  }
  file mkdir $target
  make inEachDir . install
  if {[file isdirectory $target/apps]} {
    file delete -force $target/apps
  }
  puts stderr **************************[pwd]-$target
  set ot $target
  append target /apps
  make inEachDir ../apps install
  puts stderr **************************[pwd]-$target
  set target $ot
  #file copy -force ../apps  $target/apps
  make inEachDir [make set target] mkIndex
}
Script instproc target {path} {
  make set target $path
}
Script instproc install.index {} {
  make target
  make inEachDir [make set target] mkIndex
}
Script instproc clean {} {
  make inEachDir . clean
  make inEachDir ../apps clean
}
Script create main

puts stderr "+++ make.xotcl finished."
#if {[set tcl_platform(platform)] == "windows"} {
#  exit
#}
