#! /usr/bin/env tclkit

file copy -force ../../kit-tars/runtime85.kit runtime-tk.kit
set db [vfs::mk4::Mount runtime-tk.kit runtime -readonly]
catch { vfs::attributes runtime -state translucent }

file delete runtime/tclkit.ico
file delete -force runtime/lib/tk8.4

set fd [open runtime-sh.kit w]
mk::file save $db $fd
close $fd

vfs::unmount runtime

puts "constructing tclkit-win32.exe and tclkitsh-win32.exe ..."
exec cat kit.exe runtime-tk.kit > tclkit.exe
exec cat kitsh.exe runtime-sh.kit > tclkitsh.exe
puts ""

puts "constructing tclkitlite-win32.exe and tclkitshlite-win32.exe ..."
exec cat kitlite.exe ../src/thrive/kitlite.rom runtime-tk.kit \
	  > tclkitlite.exe
exec cat kitshlite.exe ../src/thrive/kitlite.rom runtime-sh.kit \
	  > tclkitshlite.exe
puts ""

proc gzip {orig dest} {
  puts "gzip-compressing: $orig -> $dest ..."
  exec gzip < $orig > $dest
}

gzip tclkit.exe win32-85.exe.gz
gzip tclkitsh.exe win32-85-sh.exe.gz
gzip tclkitlite.exe win32-85-lite.exe.gz
gzip tclkitshlite.exe win32-85-shlite.exe.gz
puts ""

proc upx {orig dest rt lite} {
  puts "upx-compressing: $orig -> $dest ..."
  file delete $dest
  catch { exec upx -qq -9 -o $dest $orig } err
  if {$lite} { exec cat ../src/thrive/kitlite.rom >>$dest }
  exec cat runtime-$rt.kit >> $dest
}

upx kit.exe win32-85.upx.exe tk 0
upx kitsh.exe win32-85-sh.upx.exe sh 0
upx kitlite.exe win32-85-lite.upx.exe tk 1
upx kitshlite.exe win32-85-shlite.upx.exe sh 1
puts ""
