#!/usr/local/bin/wish

proc LesHTMLSous Rep {
    set LesH [lsort [glob -nocomplain -directory $Rep "*.htm" "*.html" "*.avi" "*.mov" "*.mp3"]]
    foreach D [lsort [glob -nocomplain -directory $Rep -type d *]] {
	set SesH [LesHTMLSous $D]
	set LesH [concat $LesH $SesH]
    }
    return $LesH
}

proc CreePlanDuSite Racine {
    set MemoPwd [pwd]
    cd $Racine
    set Ici [file tail $Racine]

    set LesBGI [glob -nocomplain "background*.jpg" "background*.gif" "background*.png"]
    set FichierBackground [lindex $LesBGI 0]
    set Sortie [open "PlanDuSite.html" "w"] 
    puts $Sortie "<html>"
    puts $Sortie "<head>"
    puts $Sortie "<title>$Ici</title>"
    puts $Sortie "<style type='text/css'>"
    puts $Sortie " :link    {color: rgb(255, 255, 255)}"
    puts $Sortie " :visited {color: rgb(255, 255, 255)}"
    puts $Sortie " :active  {color: rgb(255, 255, 255)}"
    puts $Sortie " h1, h2, h3, h4, h5 {color: rgb(255, 255, 255)}"
    puts $Sortie " body {"
    puts $Sortie "  background-color:lightGrey;"
    if {$FichierBackground!=""} {
	puts $Sortie "  background-image:url($FichierBackground);"
    }
    puts $Sortie "  background-repeat:no-repeat;"
    puts $Sortie "  background-position:top center;" 
    puts $Sortie "}"
    puts $Sortie "</style>"
    puts $Sortie "<center>"
    puts $Sortie "</head>"
    puts $Sortie "<body>"
    puts $Sortie "<h1>$Ici</h1>"
    puts $Sortie "<h2>&copy; <i><a href='mailto:ripp@noos.fr'>Raymond Ripp</a></i></h2>"
    puts $Sortie "<div  style='position:absolute; width :auto; height :auto; \
	    left :45%; top :15%' align='left'>"
    puts $Sortie "<pre>"
    set LesO {}
    foreach H [LesHTMLSous ""] {
	set Queue [file tail $H]
	if {[regexp {^(node|sld|tsld)[0-9]+.htm} $Queue]} { continue }
	set LesR [split $H "/"]
	set Meme ""
	foreach R $LesR {
	    set ProchainMeme "$Meme$R/"
	    if { ! [info exists DejaVu($ProchainMeme)]} { set DejaVu($ProchainMeme) 1 ; break }
	    set Meme $ProchainMeme
	}
	set Decal [string repeat " " [string length $Meme]]
#	regsub $Meme $H $Decal BeauH
	regsub $Meme $H "" BeauH
	puts $Sortie "$Decal<A HREF=\"$H\">$BeauH </A>"
    }
    puts $Sortie "</pre>"
    puts $Sortie "</div>"
    puts $Sortie "</body>"
    puts $Sortie "</html>"
    close $Sortie
    cd $MemoPwd
    return "$Racine/PlanDuSite.html"
}

#rR ############################################################# ca commence ici

set Script [info script]
set PlanDuSiteDir [file dirname $Script]

set InitDir ""
set  PlanDuSiteLog "$PlanDuSiteDir/PlanDuSite.log"
if {[file exists $PlanDuSiteLog]} {
    set f [open $PlanDuSiteLog "r"]
    set LesLignes [split [read -nonewline $f] "\n"]
    set InitDir [lindex $LesLignes end]
    close $f    
}

set Rep [tk_chooseDirectory -initialdir $InitDir]

set FichierCree [CreePlanDuSite $Rep]
if {$FichierCree!=""} {
    catch {
	set f [open $PlanDuSiteLog "a"]
	puts $f "$Rep"
	close $f
    }
}
exit





