
# This code is shared by all the ICEM CFD GUI's.
#
# Copyright (c) 1996 Pacific Marketing and Consulting, Inc,
#       and ICEM Systems, GmbH.  All rights reserved.
# Author:  Wayne A. Christopher, wayne@icemcfd.com
#

proc makedoc {out files {title "Programmer's guide"}} {
    set fout [open $out w]
    puts $fout "<title>$title</title>"
    puts $fout "<h1>$title</h1>"
    puts $fout "In the following documentation, default arguments for each"
    puts $fout "proc argument, if any, are given in \[\] brackets.<p><hr>"

    set basicTcl [file join $::env(ICEM_ACN) lib/guibase/BasicTcl.html]
    if [file exists $basicTcl] {
	set fin [open $basicTcl r]
	fcopy $fin $fout
	close $fin
    }

    foreach f $files {
	set fin [open $f r]
	set curblock ""
	while {![eof $fin]} {
	    set line [string trim [gets $fin]]
	    if [string match ##* $line] {
		append curblock "[string range $line 2 end]\n"
		if [string match "\#\# <h*" $line] {
		    puts $fout "$curblock<p>"
		    set curblock ""
		}
	    } elseif {[string match "proc *" $line] && ($curblock != "")} {
		# This is a pain
		
		while {[string match *\\\\ $line]} {
		    set line "[string trimright $line \\] [string trim [gets $fin]]"
		}
		regsub \{\$ $line "" line
		set name [lindex $line 1]
		set args [lindex $line 2]
		puts $fout "<br><b><a name=$name>$name</a></b>"
		foreach a $args {
		    puts $fout " <em>[lindex $a 0]</em>"
		    if {[llength $a] == 2} {
			set def [lindex $a 1]
			if {$def == ""} { set def \"\" }
			puts $fout " \[$def\]"
		    }
		}
		puts $fout "<p>$curblock<p>"
		set curblock ""
	    } elseif {$line != "" && ![string match #* $line]} {
		if {$curblock != ""} {
		    puts $fout "$curblock<p>"
		    set curblock ""
		}
	    }
	}
	close $fin
	puts $fout <hr>
    }
    close $fout
}

# ------------------------------

proc file_pguide {{file ""}} {
    global prog_guide_files source_dirs env

    if {[info exists env(ICEM_AI_ENVIRONMENT)] && $env(ICEM_AI_ENVIRONMENT) && ($file == "")} {
		set file [tk_getSaveFile -title "Output HTML File" -initialfile [pwd]/prog_guide.html \
		-defaultextension .html -filetypes {{{HTML Files} {.html .htm}} {{All Files} *}}]	
    }  
	if {![info exists env(ICEM_AI_ENVIRONMENT)] && {$file == ""}} {
		set file [file_select "Select the output HTML file." \
		  [pwd]/prog_guide.html \
		  "Output file" 0 1 "" "" "" {has_suffix "HTML File" .html .htm}]
    } 

    set files ""
    foreach f $prog_guide_files {
	if {[file pathtype $f] == "absolute"} {
	    lappend files $f
	} else {
	    foreach dir $source_dirs {
		if [file exists $dir/$f.tcl] {
		    lappend files $dir/$f.tcl
		    break
		}
	    }
	}
    }
    makedoc $file $files
    mess "Wrote programmer's guide ([llength $files] files) to $file\n"
}

