#!/usr/bin/tcl


proc head {fo title} {
  puts $fo "<html>"
  puts $fo "<head><title>$title</title></head>"
  puts $fo "<body>"
  puts $fo "<h1>$title</h1>"
}

proc foot {fo} {
  puts $fo "<p>--&gt;<a href=\"index.html\">LaTeX index</a>"
  puts $fo "</body>"
  puts $fo "</html>"
}

set fi [open latex2e.html r]
set fo [open help/english/latex/index.html w]
set fidx [open help/english/latex/latex.idx w]
set lidx {}
while {![eof $fi]} {
  gets $fi s
  if {[regexp {NAME="(SEC[^"]*)} $s dummy sec]} {
    foot $fo
    close $fo
    set sec [string tolower $sec]
    set fo [open help/english/latex/${sec}.html w]
    regexp {A NAME[^>]*>([^<]*)} $s dummy title
    regsub -all {\\} $title {} key
    puts $fidx [list $key $sec.html]
    regsub -all {\\} $title {\&#92;} title
    head $fo $title
  } else {
    regsub -all {\\} $s {\&#92;} s
    if {[regexp {NAME="IDX([^"]*)"} $s dummy idx]} {
      lappend lidx [list $idx $sec]
      puts "$idx $sec"
    } else {
      regsub -all {HREF="latex2e.html#SEC([^"]*)"} $s {href="sec\1.html"} s
      if [regexp {HREF="latex2e.html#IDX([^"]*)"} $s dummy idx] {
        puts $s
        set i [lsearch $lidx $idx]
        set ref [lindex [lindex $lidx $i] 1]
        regsub {HREF="latex2e.html#IDX([^"]*)"} $s \
          "href=\"$ref.html\"" s
      }
      puts $fo $s
    }
  }
}
foot $fo
close $fo
close $fidx
close $fi

#set fi [open dummy.html r]
#set fo [open help/eng/latex/index.html w]
#puts $fo "<html>"
#puts $fo "<head><title>LaTeX index</title>"
#puts $fo "</head>"
#puts $fo "<body>"
#puts $fo "<h1>LaTeX Index</h1>"
#puts $fo "<ul>"
#while {![eof $fi]} {
#  gets $fi s
#  if {[regexp {<LI>} $s]} {
#    puts $fo $s
#  }
#}
#puts $fo "</ul>"
#puts $fo "<p>"
#puts $fo "--&gt; <a href=\"../main.html#main\">Start menu</a>"
#puts $fo "</body>"
#puts $fo "</html>"
#close $fo
#close $fi

