#! /usr/bin/env tclsh
# -*- tcl -*- 

#
# This file uses "freewrap" to wrap a filerunner for windows.  It produces
# a stand alone windows .exe file.  Tcl/Tk/wish are not needed in this case.
# You will need to adjust the following paths to match where you have 
# filerunner and freewrap
#
# we get the version from the input...
#
# Parms on input:
# 
# 1) version     default "test"
# 2 & 3 types    if 32, build a 32-bit windows version
#                if 64, build a 64-bit windows version
#                if both, build both versions
#                type (if provided) will be appended to the version
########################################################
#     This is the boiler plate code                    #
########################################################
# This bit of code figures out where the rest of the   #  
# routines are on the assumption that they are in the  #
# same directory as the initial code file.             #
# If 'setIt' is 1 or not coded auto_path is set in any #
# case the resulting dir is returned to the caller     #
# A second dir is also returned, which is the same     #
# with a leading C:/ removed as required by Wrap code  # 
# for windows. If not windows the two will be the same.#
#                                                      #
# To function correctly this code MUST be called prior #
# to completion of the 'source' command that brings it #
# in. Also, since it is used to set up auto_path it    #
# can not be auto loaded.  It may be sourced, but      #
# again the from where issue is there.                 #
#                                                      #
# It is best if this is just merger with the using     #
# code in a location prior to its call.                #
#                                                      #
proc setAutoPath {{setIt 1}} {
  set it [info script]
  set it [expr {$it == "" ? "[pwd]/*" : $it}]
  set it [file dir [file dir [file norm $it/*]]]
  # Wrap code requires we not have the drive letter...
  if {$setIt} {
    lappend ::auto_path $it [regsub {^[a-zA-Z]:/} $it {/}]
  }
  return [list $it [regsub {^[a-zA-Z]:/} $it {/}]]
}
##########################################################
#            End of boiler plate code                    #
setAutoPath
lappend ::auto_path [file norm ../packages] [file norm ../packagesStd]

set types [lassign $argv VERSION] 
if {$VERSION == "" } { 
  set VERSION "test"
}

proc bigMess {} {
  puts \
"For this reason we will not make the 'windows' version of filerunner.
If you would like to make a 'windows' version of filerunner the
following two programs must be on your system:
freewrap and freewrap.exe
freewrap or a link to it must be in your PATH enviornment variable.
This code finds freewrap.exe by searching in your file system starting
at fw/.. where 'fw' is the directory that contains the freewrap program.
If more than one is found, (such as one for 32 & 64 bit machines) you
may fource the selection by putting a link (or the actual desired 
version) in fw/.. . Otherwise, the code will attempt to use the one
where 'n32' appears in the path.  If no such is found, you must put
a link in fw/.. .
You may also specify which you want by putting '32' or '64' or '32 64'
in the runstring. Again we look for n32 & n64 in the path names."
}

foreach type $types {
  if {$type != "32" && $type != "64"} {
    puts "Only types 32 & 64 recognized but found $types"
    puts "Fix this"
    exit 1
  }
}

set FRDIR [file norm ".."]
set WRAPLIST [file norm wrapfilelist.txt]
set RELDIR [file norm "$FRDIR/release-$VERSION"]

# what environment??
if {$tcl_platform(platform) eq "windows"} {
  lappend auto_path [file norm ../packages/mswindows]
  load_now_auto_execok
  set MSW 1
} else {
  set MSW 0
}


# In an effort to remove special directories we will attempt to find the
# freewrap program and its ".exe" companion by examining the PATH useing 
# auto_execok
puts "looking for freewrap"

set freewrap [auto_execok freewrap]
# puts "$MSW $freewrap"
if {$freewrap == {}} {
  puts "It appears that \"freewrap\" is not in your path." 
  bigMess
  exit 0
}
# run down any links...
#
set freewrap [file dirname [file norm $freewrap/c]]
puts "found $freewrap"
# now back off and look for freewrap.exe

set look [file dirname [file dirname $freewrap]]
set rdir $look
set dirs {}
puts "looking in $rdir for possible exe files"
while {$rdir != {}} {
  set rdir [lassign $rdir this]
  lappend dirs $this
  set rdir [concat $rdir [glob -nocomplain -type d -directory $this *]]
}

set possible {}
foreach dir $dirs {
  if {[catch {glob -tails -type f -directory $dir freewrap.exe} files] == 0} {
    lappend  possible $dir/$files
  }
}
if {$possible == {}} {
  puts "Could not find \"freewrap.exe\" in \"$look/*\"."
  bigMess
  exit 1
}

puts "possible $possible"

set this {}
if {$types == {}} {
  if {"$look/freewrap.exe" in $possible} {
    set this [file dirname [file norm $look/freewrap.exe]]
  } else {
    # no guidance, try for the *32*
    foreach fil $possible {
      if {[string match {*n32*} $fil]} {
	lappend this $fil
      }
    }
    if {[llength $this] == 0} {
      set this [lindex $possible 0]
    } else {
      set this [lindex $this 0]
    }
  }
} else {
  # there is guidance...
  foreach wdsize $types {
    foreach fil $possible {
      if {[string match "*n$wdsize*" $fil]} {
	lappend found $wdsize
	lappend this $fil
	break
      }
    }
  }
  if {[llength $types] != [llength $this]} {
    puts "Requested word size version(s) $types, found only $found"
    puts "Fix this"
    exit 1
  }
}

# And one last thing.  We need to have a .tcl extension on the main 
# (i.e. fr) to appease freewrap.  To do this we create a link to "fr".
#
cd $FRDIR
if {! [file exists fr.tcl]} {
  puts "file \"fr.tcl\" does not exist"
  exit 1
}

# Lets use a compressed version string for the file name:

set version [string range [regsub -all {\.} $VERSION {}] 0 5]

# All is well, lets do the wrap...

foreach bitSize $this {
  if {[string match {*n32*} $bitSize]} {
    set bits "32"
  } elseif {[string match {*n64*} $bitSize]} {
    set bits "64"
  } else {
    set bits "00"
  }
  
  set cmd [list $freewrap [file native $FRDIR/fr.tcl] \
	       -f [file native $WRAPLIST] -i icons/filerunner.ico\
	       -o $RELDIR/fr-$version-$bits.exe -w [file native $bitSize]
	   ]
  puts "\ncmd $cmd\npwd is [pwd]"
  set r [catch {exec -ignorestderr -keepnewline {*}$cmd} out]
  puts $out
  if {$r != 0} {
    puts "Error trying to execute: $cmd "
    exit 1
  }
}
exit 0
