set config_filename "JadeRC.TCL"
set jade_name "jade"
set catalog_name "C:/catalog"
set debug 0
set scripts(htm) "not set"
set type(htm) "sgml"
set scripts(rtf) "c:/usr/dsssl/docbook/print/docbook.dsl"
set type(rtf) "rtf"
set scripts(tangle) "c:/usr/dtd/dblp/SGMLTangle.dsl"
set type(tangle) "sgml"

if [file exists $config_filename] {
 source $config_filename
 }
  
set target [tk_getOpenFile \
    -title "Set Target File Name" \
    -filetypes {
     {{SGML files} {.sgm}}
     {{XML files} {.xml}}
     {{All files} {".*"}}
     }]
set target_path [file dirname $target]
set target_name [file tail $target]
eval cd \"$target_path\"
button .browse -textvariable target_name -command { 
set target [tk_getOpenFile \
     -title "Select New Target File Name" \
     -filetypes {
      {{SGML files} {.sgm}}
      {{XML files} {.xml}}
      {{All files} {".*"}}
      }]
  set target_path [file dirname $target]
  set target_name [file tail $target]
  eval cd \"$target_path\"
  
} 
label .pathname -textvariable target_path
frame .menubar -relief groove -borderwidth 4 

menubutton .menubar.file   -text "File" \
  -direction below -menu .menubar.file.menu 
menu .menubar.file.menu\
  -tearoff 0
.menubar.file.menu add command -label "Set target" -command {
  set target [tk_getOpenFile -title "File" -filetypes {
      {{SGML files} {.sgm}}
      {{XML files} {.xml}}
      {{All files} {".*"}}
      }]
  set target_path [file dirname $target]
  set target_name [file tail $target]
  eval cd \"$target_path\"
  }
.menubar.file.menu add command -label "Read config" \
  -command {
   source  [tk_getOpenFile -filetypes {
      {{TCL Files} {.tcl}}
      {{All files} {".*"}}
  }]
  foreach script [array names scripts] {
    set outfile($script) ""
  append outfile($script) [file rootname $target_name] "." $script
  set cmd($script) ""
  append cmd($script) "\"$jade_name\" " 
  append cmd($script) " " -c " " \"$catalog_name\" " "
  append cmd($script) " " -d " \"" $scripts($script) "\"" 
  append cmd($script) " " -t " " $type($script)
  append cmd($script) " " \"$target_name\" 
  append cmd($script) " " 2>> jadetcl.err 
  append cmd($script) " " > $outfile($script) 

  }}
.menubar.file.menu add command -label "Exit" -command {
  exit}

menubutton .menubar.config -text "Config" \
  -direction below -menu .menubar.config.menu
menu .menubar.config.menu \
  -tearoff 0 
.menubar.config.menu add command -label "Jade" -command {
  set jade [tk_getOpenFile -filetypes {
      {{Executables} {.exe}}
      {{All files} {".*"}}
  }]
  } 
.menubar.config.menu add command -label "Catalog File" -command {
  set catalog_name [tk_getOpenFile -title "catalog" -filetypes {
      {{All files} {".*"}}
  }]
  } 

menubutton .menubar.help   -text "Help" \
  -direction below -menu .menubar.help.menu
menu .menubar.help.menu \
  -tearoff 0 
.menubar.help.menu add command -label "Help" \
        -command {showHelp}
.menubar.help.menu add command -label "About" -command {
  tk_messageBox -type ok -message \
  "This is Jade.tcl, a TCL/Tk application providing a 
   graphical front end for Jade.\n\n
   Copyright (c) Mark Wroth &#60;mark@astrid.upland.ca.us&#62; 2001\n\n
   Free distribution under the terms of the Gnu Public License
   is authorized."
  }

pack .menubar.file .menubar.config  -side left -fill x
pack .menubar.help -side right -fill x
foreach script [array names scripts] {
    set outfile($script) ""
  append outfile($script) [file rootname $target_name] "." $script
  set cmd($script) ""
  append cmd($script) "\"$jade_name\" " 
  append cmd($script) " " -c " " \"$catalog_name\" " "
  append cmd($script) " " -d " \"" $scripts($script) "\"" 
  append cmd($script) " " -t " " $type($script)
  append cmd($script) " " \"$target_name\" 
  append cmd($script) " " 2>> jadetcl.err 
  append cmd($script) " " > $outfile($script) 

  checkbutton .$script \
    -textvariable scripts($script) \
    -variable do($script) \
    -onvalue  1 \
    -offvalue 0 \
    -anchor w 
}
button .go -text "Go" -command {
  if [file exists jadetcl.err] {file delete jadetcl.err}
  foreach script [array names scripts] {
    if $do($script) {
      if $debug {tk_messageBox -message $cmd($script)}
      catch {eval exec $cmd($script)}
      catch {
        if [expr [file size jadetcl.err] > 0] {
          showFile jadetcl.err
          } else {
          file delete jadetcl.err
          }
        }
      } else {}
  }
}

button .showerr -text "Show Err" -command {
  showFile jadetcl.err
}

button .exit -text "EXIT" -command {exit}
pack .menubar -side top -fill x
pack .pathname .browse  -side top -fill x
foreach script [array names scripts] {
  pack .$script -side top -fill x
}
pack .go .showerr .exit -side left -expand 1 -fill x
proc showHelp {} {
     set helptext "\
                   OVERVIEW
                   This is a Tcl/Tk front end for the Jade \
                   processor. It takes a single target file and (as \
                   part of its initialization) an array of DSSSL \
                   scripts and output types.  When commanded, it \
                   runs Jade on each script in turn.
                   
                   TARGET FILE
                   The name of the target file is displayed in a text button near \
                   the top of the application window, with the path to that file \
                   displayed above it.  The jade process will execute in this \
                   directory, which means that all relative path names defined \
                   in the target file will be interpreted relative to this directory.
                   \tTo change the target file, click on the text button containing \
                   the target file name.  A file requester will appear; navigate to and \
                   select the new target file using the requester.  By default, the \
                   requester will show only files ending in \".sgm\", since this is the \
                   usual extension given to SGML target files.
                   \tThe target file requester can also be summoned via the \"File\" \
                   menu. \n

                   GO\n
                   To execute Jade with the currently selected target file and options, click \
                   on the button labeled \"Go\".
                   CONFIGURATION\n
                   The application assumes that the Jade executable is found \
                   somewhere in the operating system path.  If this is not the case, the path \
                   to the nuweb executable can be set using the \"Config\" menu \"Jade\" \
                   option.
                   EXITING FROM THE APPLICATION\n
                   There are three different ways to exit from the Nuweb Tcl application:
                   \t - Click on the button marked \"Exit\"
                   \t - Select the \"Exit\" item on the \"File\" menu
                   \t - Click on the \"Close Window\" button provided by the operating \
                   system's window manager.
                   "
     set w .text
     catch {destroy $w}
     toplevel $w
     wm title $w "Jade TCL/Tk Help"
     wm iconname $w "text"

     frame $w.buttons 
     pack $w.buttons -side bottom -fill x -pady 2m
     button $w.buttons.dismiss -text Dismiss -command "destroy $w"
     pack $w.buttons.dismiss  -side bottom -expand 1

     text $w.text -relief sunken -bd 2 -yscrollcommand "$w.scroll set" \
        -setgrid 1 -height 30 \
        -tabs {1c 2c 3c} \
        -wrap word  
     scrollbar $w.scroll -command "$w.text yview"
     pack $w.scroll -side right -fill y
     pack $w.text -expand yes -fill both
     $w.text insert end $helptext
     $w.text mark set insert 0.0 } 

proc showFile {thefile} {
     set w .text
     catch {destroy $w}
     toplevel $w
     wm title $w "$thefile"
     wm iconname $w "text"

     frame $w.buttons
     pack $w.buttons -side bottom -fill x -pady 2m
     button $w.buttons.dismiss -text Dismiss -command "destroy $w"
     pack $w.buttons.dismiss  -side bottom -expand 1

     text $w.text -relief sunken -bd 2 -yscrollcommand "$w.scroll set" \
        -setgrid 1 -height 30
     scrollbar $w.scroll -command "$w.text yview"
     pack $w.scroll -side right -fill y
     pack $w.text -expand yes -fill both
     textLoadFile $w.text $thefile
     $w.text mark set insert 0.0 } 

proc textLoadFile {w file} {
    if [file exists $file] {
      set f [open $file]
      $w delete 1.0 end
      while {![eof $f]} {
         $w insert end [read $f 10000]
      }
      close $f
  }
}