#! /usr/bin/env tclsh

# <20160627.1731.12>
# clean up the inotify issue.  We have two possible cases:
# 1. This arch has a binary for inotify or
# 2. it does not.
#
# This bit of code trims the packages directory to correctly represent
# Which inotify (if any) is to be used on this (the running) platform
#
# First figure out where the code is...
# This code is in .../filerunner/Makefiles for development/distribution
# For actual running it must be in the .../filerunner directory or it
# will fail.  This is to prevent accidental removal of inotify code
# from the distribution tree, i.e. it should only work on INSTALLED
# filerunner directories.


proc clean-file {f} {
  if [file exists $f] {
    return [file dir [file norm $f/*]]
  }
  return {}
}

set me [clean-file $::argv0]
lappend possible [file dirname $me]
if { [catch {info script} out] == 0 } {
  set out [clean-file $out]
  if { [catch {file dirname $out} it] == 0} {

    set tail [file tail $out]
    #      puts "start with [info script]"
    #      puts "found $it"
    lappend possible $it 
  }
}
lappend possible [pwd]
#puts "looking in $possible"
set success 0
foreach testfile $possible  {
  #    puts "testing $testfile"
  if { [file exists $testfile/$tail]  == 1 } {
    set HomeDir "$testfile/packages"
    if {[file exists $HomeDir]} {
      set success 1
      break
    }
  }
}

if {!$success} {
  puts "Could not find the required filerunner directory."
  puts "Looked in these locations: $possible"
  puts "Try cd-ing to the filerunner directory and running."
  puts "Done here."
  exit 1
}
#puts "found $HomeDir & I am $me"
if {[file dirname $HomeDir] != [file dirname $me]} {
  puts "Really bad news here.  The found directory ([file dirname $HomeDir])
is not where I am running from([file dirname $me]).
I am NOT going there."
  exit 1
}
#exit
if {$::tcl_platform(os) != "Linux"} {
  # In this case the whole of the inotify thing should just be 
  # removed...
  file delete -force $HomeDir/inotify
} else {
  set type $::tcl_platform(machine)
  if {$type in [glob -nocomplain \
		    -path $HomeDir/inotify/ -type d -tails "*"]} {
    cd $HomeDir/inotify/$type
    # we have the required code... move it and delete the rest
    foreach file [glob -nocomplain  "*"] {
      file delete $HomeDir/inotify/$file
      file rename $file $HomeDir/inotify/$file
    }
    foreach dir [glob -nocomplain -type d \
		     -path $HomeDir/inotify/ "*"] {
      file delete -force $dir
    }
    # We have a good inotify so delete the build stuff
    cd [file dirname $me]/Makefiles
    foreach file [concat [glob -nocomplain *-inotify-*]  build_inotify] {
      puts "deleteing $file"
      catch {file delete $file}
    }
    cd ..
    catch "file delete [file dirname $me]/Makefiles"
  } else {
    # No binary for this arch, delete the whole thing
    # Do NOT use force here as we may have already done our
    # thing so the dir will not be empty.  Not using force keep it around.
    if {[file exists $HomeDir/inotify]} {
      cd $HomeDir/inotify/ 
      foreach dir [glob -nocomplain -type d *] {
	file delete -force $dir
	lappend shipDirs $dir
      }
      if {[info exists shipDirs] && $shipDirs != {} } {
	set fid [open [file dirname $me]/shipInotifyArchs w]
	puts $fid "set ::shipInotifyArchs [list $shipDirs]"
	close $fid
      }
    }
    catch {file delete $HomeDir/inotify}
    }
}
# Self distruct
file delete -force $me
exit 0
