<?

# This file has no license.  It is in the public domain.
# -- Roy Keene <tcl@rkeene.org> [20081025T1400Q]

if {[var exists source]} {
	headers type "text/plain; charset=us-ascii"
	rivet_flush

	set fd [open $::env(PATH_TRANSLATED) r]
	fcopy $fd stdout
	close $fd

	abort_page
	exit 0
}

source [file join [file dirname [info script]] common.tcl]

if {[var exists starpack_platform] && [var exists extensions]} {

	set ourkit [exec $::customkit::findkit]

	set starpack_platform [var get starpack_platform]
	set extensions [var get extensions]

	set platforminfo $::customkit::Mapping($starpack_platform)
	set platforminfo_kit [lindex $platforminfo 0]
	set platforminfo_tclver [lindex $platforminfo 1]
	set platforminfo_os [lindex $platforminfo 2]
	set platforminfo_cpu [lindex $platforminfo 3]

	set workdir [file join "/tmp" DELETEME_[::md5::md5 -hex [clock seconds][clock clicks][pid][array get ::env]]]
	set extpath [file join $workdir exts]
	set kitpath [file join $workdir kit]
	file mkdir $workdir

	array set incextensions [::customkit::download_extensions_teapot $extpath [list teapot.rkeene.org] $extensions $platforminfo_os $platforminfo_cpu]

	set webkit [file join kits customkit-v$platforminfo_tclver-$platforminfo_os-$platforminfo_cpu-[string tolower [::md5::md5 -hex [list $platforminfo [array get incextensions]]]].bin]

	if {![file exists $webkit]} {
		file copy -force -- $platforminfo_kit $kitpath

		catch {
			exec $ourkit $::customkit::sdx sync -auto 1 -forcedest 1 $extpath $kitpath
		}

		file mkdir [file dirname $webkit]
		file copy -force -- $kitpath $webkit
	}

	file delete -force -- $workdir
}

# Set content-type
headers type "text/html; charset=us-ascii"

rivet_flush

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>CustomKit Builder</title>
  </head>
  <body>
    <h1>CustomKit</h1>
<?
if {[info exists webkit]} {
?>    <h2>Complete</h2>
    <div>
      The following CustomKit has been created: <a href="<? puts -nonewline $webkit ?>"><? puts -nonewline [file tail $webkit] ?></a>.  It was created with the following parameters:
      <table border="1">
        <tr>
          <td>Tcl Version:</td>
          <td><? puts -nonewline $platforminfo_tclver ?></td>
        </tr>
        <tr>
          <td>OS:</td>
          <td><? puts -nonewline [::customkit::descOSLocalToEnglish $platforminfo_os] ?></td>
        </tr>
        <tr>
          <td>CPU:</td>
          <td><? puts -nonewline [::customkit::descCPULocalToEnglish $platforminfo_cpu] ?></td>
        </tr>
        <tr>
          <td>Included Extensions:</td>
          <td>
<?
	foreach {ext extinfo} [array get incextensions] {
		set extinfo_ver [lindex $extinfo 0]
		if {[lsearch $extensions $ext] == -1} {
			puts "            <i>$ext v$extinfo_ver</i><br>"
		} else {
			puts "            $ext v$extinfo_ver<br>"
		}
	}
?>          </td>
        </tr>
        <tr>
          <td>Excluded Extensions:</td>
          <td>
<?
	foreach ext $extensions {
		if {![info exists incextensions($ext)]} {
			puts "            <b>$ext</b><br>"
		}
	}
?>          </td>
        </tr>
      </table>
    </div>
    <div>
      <a href="?start_over=1">Start Over</a>
    </div>
<?
} else {
?>    <h2>Select Parameters</h2>
    <form method="post" action="<? puts -nonewline $::env(REQUEST_URI) ?>">
      <table>
        <tr>
          <td>Target Platform:</td>
          <td>
            <select name="starpack_platform">
<?
	set descs [::customkit::get_descs_from_mapping]

	foreach desckey [lsort -dictionary $descs] {
		set desc [lindex $desckey 0]
		set key [lindex $desckey 1]
		puts "              <option value=\"$key\">$desc</option>"
	}
?>            </select>
          </td>
        </tr>
        <tr>
          <td>Extensions:</td>
          <td>
            <select name="extensions" size="20" multiple>
<?
	set extensionsinfo [::customkit::get_extensions_teapot teapot.rkeene.org]

	set extensions [list]
	foreach {extension extdata} $extensionsinfo {
		if {[lsearch -exact $extensions $extension] != -1} {
			continue
		}

		lappend extensions $extension
	}

	foreach extension [lsort -dictionary $extensions] {
		puts "              <option value=\"$extension\">$extension</option>"
	}
?>            </select>
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <input type="submit" name="submit_upload" value="Create">
          </td>
        </tr>
      </table>
    </form>
<?
}
?>    <div>
      <a href="?source=1">Source</a>
    </div>
  </body>
</html>
