#!/bin/sh

# \
  exec /usr/local/bin/wish8.4 "$0" "$@"

source progressbar.tcl

proc tract_html {fitxer} {
global table1 table2 estils marge

set file [open $fitxer r ] 
set file_out [open ./out/$fitxer { RDWR CREAT } ] 

     while { ![eof $file] } {
     set linea [gets $file] 
     if {$linea=="<HEAD>"} {puts $file_out $estils}
     if {$linea=="<BODY>"} {puts $file_out $marge}


if {[regexp {<H1>.*</H1>} $linea] || [regexp {<H2>.*</H2>} $linea] || [regexp {<H3>.*</H3>} $linea] || [regexp {<H4>.*</H4>} $linea]} {

        puts $file_out $table1
        puts $file_out $linea
        puts $file_out $table2
      } else {if {$linea=="<center>"} {
              puts $file_out "<BR><BR>"
              puts $file_out $linea
              } else {
	           if {[regexp {<LI>.*Index} $linea]} {
		   } else {
	           if {$linea!="<HEAD>" || $linea!="<BODY>"} {puts $file_out $linea} } } }
     }

close $file_out
close $file


}


proc wait_win {} {
    global progreso
    
    #toplevel .wait 
    frame .wait

    text .wait.t -height 8 -width 35     
    .wait.t tag configure estil -font {Arial 16 bold} -foreground mediumblue
    .wait.t insert end "\n \n \n"
    .wait.t insert end "Please wait ... " estil
    pack .wait.t -side top -padx 2m -pady 2m 
    
    ::progressbar::progressbar .wait.pb -variable progreso -width 200
    pack .wait.pb -side top -padx 2m -pady 2m 
    
    set progreso 0
    
    pack .wait
    #wm overrideredirect . 1
    
    wm geometry .  +300+200
}

proc actualiza_progreso { parcial total} {
    global progreso

    set progreso [ expr int( 0.5 + 100.0 * $parcial / $total)]
}

global table1 table2 estils marge

set estils {<HEAD>
<STYLE type="text/css">
     <!--
     A { text-decoration:none }
     -->
     </STYLE>

     <STYLE type="text/css">
     <!--
     PRE { font-family:Courier New, Courier, Mono; }
     -->
     </STYLE>

<STYLE type="text/css">
     <!--
     H1 { font-family:Arial; font-size: 28}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     H2 { font-family:Arial; font-size: 24}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     H3 { font-family:Arial; font-size: 22}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     H4 { font-family:Arial; font-size: 18}
     -->
     </STYLE>


<STYLE type="text/css">
     <!--
     P { font-family:Arial, Helvetica, sans-serif; font-size: 17}
	 
     -->
     </STYLE>

<STYLE type="text/css">
     <!--
     CODE { font-family:Courier New, Courier, Mono; font-size: 16}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     LI { font-family:Arial, Helvetica, sans-serif; font-size: 17}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     OL { font-family:Arial, Helvetica, sans-serif; font-size: 17}
     -->
     </STYLE>
<STYLE type="text/css">
     <!--
     UL { font-family:Arial, Helvetica, sans-serif; font-size: 17}
     -->
     </STYLE>}



set marge {<BODY>
           <DIV style="margin-left:20px">}




set table1 {<table border="0">
  <tr>
    <td></td>
  </tr>
</table>
<table width="100%" border="0" bgcolor="#F2F2F2">
  <tr>
    <td>}

set table2 {    </td>
  </tr>
</table>}

wait_win 
update
set lst [glob gid*html]
set n [ llength $lst]
for { set i 0} { $i < $n} { incr i} {
    set archivo [ lindex $lst $i]
    if {$archivo != "tract-css.tcl" && $archivo != "out"}   {
        set var(nom_fitxer) $archivo
        tract_html $archivo
    } 
    actualiza_progreso [ expr $i + 1] $n
    update idletasks
}
tk_messageBox -message " ---> Done!"
#destroy .
exit
