# generateHTML.pm
#
# A skinnable module for generating HTML pages.
#
# author:  Max-Gerd Retzlaff <m.retzlaff@gmx.net>
# date:    $Date: 2002/05/06 00:16:03 $
# version: $Revision: 1.4 $
#
# This application is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version. 
#
# This application is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111 USA.  

package generateHTML;

use HelpfulFunctions;

$skin_version = 'generateHTML skin version 0.2';
$suitable_skin_loaded = 0;

#use Exporter;
#@ISA = ('Exporter');
#@EXPORT = ();

$www_dir = '';
$www_skin_dir = '';

sub set_www_dir {
    my ($pkg, $dir) = @_;
    $www_dir = $dir;
    # print "set_www_dir: $dir\n";
}

sub set_www_skin_dir {
    my ($pkg, $dir) = @_;
    $www_skin_dir = $dir;
    # print "set_www_skin_dir: $dir\n";
}

sub set_mwiki_addr {
    my ($pkg, $dir) = @_;
    $mwiki_addr = $dir;
    # print "set_wmiki_addr: $dir\n";
}

sub read_skin {
    my ($pkg, $filename) = @_;

    if ($filename ne '' and -f $filename) {
	local $/ = "[MWIKI-SEPERATOR]\n";
	open(SOURCE, "< $filename") or 
	    mdie(1, "ERROR: Can't open file \"$filename\" for reading.\n");

	my $header_part = <SOURCE>; # strip of header (incl. comments)
	chomp($header_part);
	(my $file_version = $header_part ) =~ s/^(.*?)\n.*$/$1/s;
	
	if ( $skin_version ne $file_version and $suitable_skin_loaded == 0 ) {
	    # mdie(1, "ERROR: \"$filename\" is not a \"$skin_version\"-file.\n");
	    mdie(1, "ERROR: \"$filename\" is not a \"$skin_version\"-file. Please load a suitable skin first.\n");
	} else {
	    $suitable_skin_loaded = 1;
	    foreach my $record (<SOURCE>) {
		chomp($record);
		my @skin = split("\n", $record );
		my $method = shift(@skin);
		$skin{$method} = join( "\n", @skin );
	        # print STDERR "+".$method."+++".join("\n",@skin);
            }
        }
	close(SOURCE);
    } else {
	return 1;
    }
    return 0;
}

sub AUTOLOAD {
    my ($pkg, @substitution) = @_;
    #$text = '' unless ( defined $text );
    
    return if ( $AUTOLOAD =~ /::DESTROY$/ );

    (my $subroutine = $AUTOLOAD ) =~ s/^.*:://;
    if ( defined $skin{$subroutine} ) {
	#(my $output = $skin{$subroutine} ) =~ s/\[MWIKI\-WILDCARD\]/$text/g;
	my $output = $skin{$subroutine};
	$output =~ s/\[MWIKI\-WWW\-DIR\]/$www_dir/g;
	$output =~ s/\[MWIKI\-SKIN\-DIR\]/$www_skin_dir/g;
	$output =~ s/\[MWIKI\-MAIL\-ADDRESS\]/$mwiki_addr/g;
	
	if ( $subroutine =~ /_mwiki_sep$/ ) {
	    join( $output, @substitution );
	} else {
	    # foreach $text (@substitution) {
	    for(my $i = 0;  $i <= $#substitution; $i++) {
		if ($i == 0) {
		    $pos =  '';
		} else {
		    $pos = "\-$i";
		}
		$output =~ s/\[MWIKI\-WILDCARD$pos\]/$substitution[$i]/g;
	    }
	}

	return $output."\n";
    }

    mdie(1, "ERROR: Don't support method \"$subroutine\".\n");
    return 1;
}

sub whole_page {
    my ($pkg, $page_title, $main_title, $index_title, $index,
	$content_title, $content, $footer) = @_;

    return generateHTML->page_begin($page_title).
	generateHTML->maintitle($main_title).
	generateHTML->index(generateHTML->title($index_title).$index).
	generateHTML->content( generateHTML->title($content_title).$content).
	generateHTML->page_end( generateHTML->footer($footer) );
}

return 1; # Last executing statement in file must be
          # non-zero, to indicate succesful loading
