#!/usr/bin/perl 
# this is not programmed by me.. :}

# ugly hack..
BEGIN {
    (my $dir = $0) =~ s/^(.*)\/[^\/]*?$/$1/;
    chdir( $dir );
}

use HelpfulFunctions;
use generateHTML;
use structuredText;

########################################################################
### variable declarations

my $source_dir = "source";
my $target_dir = "target";
my $skin_dir = "skins";
my $cp = "/bin/cp";

my $www_dir = "file:///home/mgr/daten/coding/perl/makeWeb/target";
my $www_skin_dir = "$www_dir/skins";

my $itemlist_file = "itemlist";
my $index_filename = "index.html";
my $index_include_file = "index.include";
my $latest_news_file =  "$source_dir/latestnews";
my $latest_news_include_file = "$target_dir/latestnews.include";

my $main_title = "Home";
my $index_title = "Index";

my $dir_perm_public   = 0755;
my $file_perm_public  = 0644;
my $file_perm_private = 0600;

########################################################################
### main program

# You have always to load the 'cleantable'-skin
# as it sets defaults for all other skins that exist until now..
generateHTML->read_skin("$skin_dir/cleantable.skin");
# generateHTML->read_skin("$skin_dir/C3H.skin");
# generateHTML->read_skin("$skin_dir/clean.skin");
 generateHTML->read_skin("$skin_dir/WEBpage.skin");

generateHTML->set_www_dir($www_dir);
if(defined $www_skin_dir) {
    generateHTML->set_www_skin_dir($www_skin_dir);
}
#generateHTML->set_mwiki_addr($mwiki_addr);


my @sections = get_itemlist( $source_dir );
generate_latest_news_include();
generate_index_includes( @sections );
generate_pages( @sections );
copy_skin_resources();

########################################################################
### sub functions


sub get_itemlist{
    my $dir = $_[0];

    my ($sections, @sections);
    file_as_string( \$sections, "$dir/$itemlist_file" );
    foreach my $line ( split( "\n", $sections ) ) {
	push( @sections, [ split( " ", $line, 2 ) ] );
    }
    return @sections;
}

## the next function is not used anymore..
sub generate_latest_news_include {
    my $latest_news_raw;
    my $latest_news = '';
    file_as_string( \$latest_news_raw, $latest_news_file );
    foreach my $line ( split( "\n", $latest_news_raw) ) {
#	$latest_news .= generateHTML->latest_news( split( '\[MWIKI\-SEP\]', $line ) );
	my ( $file, $text ) = split( " ", $line, 2);
	$latest_news .= "<a href=\"$www_dir/$file\">$text</a><br>\n";
    }
    string_to_file( $latest_news_include_file, $latest_news, $file_perm_private );
}


# generates directory indexes
sub generate_index_includes {
    my @sections = @_;

    my @subdirs = map { $_->[0] } @sections;
    push( @subdirs, '.' ); # ihhk

    foreach my $dir (@subdirs) {
#    foreach my $outer_section (@sections) {
#	my $dir = $outer_section->[0];

	my $main = generateHTML->index_item( $index_filename, $main_title);
	$main .= generateHTML->index_separator;
	foreach my $section (@sections) {
	    $main .= generateHTML->index_item( $section->[0] . "/". $index_filename,
					       $section->[1] );

            # create submenu for this section
	    if ( $section->[0] eq $dir ) { 
		my $submenu = '';

		my @pages = get_itemlist( "$source_dir/$dir" );
		foreach my $page (@pages) {
		    $submenu .= generateHTML->subindex_item( $section->[0] ."/". remove_extension( $page->[0] ) . ".html",
							     $page->[1] );
		}
		$main .= generateHTML->subindex( $submenu );

	    }
	}

        # create directories and write index includes
	mkdir( "$target_dir/$dir" );
	chmod( $dir_perm_public, "$target_dir/$dir" );
	string_to_file( "$target_dir/$dir/$index_include_file", $main, $file_perm_private );
    }

}

sub generate_pages {
    my @sections = @_;

    my $latest_news = '';
    file_as_string( \$latest_news, $latest_news_include_file );
    unlink( $latest_news_include_file );
#    file_as_string( \$latest_news, $latest_news_file );

#    my @subdirs = map { $_->[0] } @sections;
#    push( @subdirs, '.' );
#    foreach my $dir (@subdirs) {
    push( @sections, [ ".", $main_title ] ); # ihhhk
    foreach my $section (@sections) {
	my $dir = $section->[0];

	my $index = '';
	my $this_index_include = "$target_dir/$dir/$index_include_file";
	file_as_string( \$index, $this_index_include );
	# not the best position for this..
	unlink( $this_index_include );

	my @pages = ($dir ne ".")?get_itemlist( "$source_dir/$dir" ):(); # ugly, ugly, ugly!
	push( @pages, [ ( -f "$source_dir/$dir/index.txt" )?"index.txt":"index.html", $section->[1] ] );
	foreach my $page (@pages) {

	    my $source_text = '';
	    file_as_string( \$source_text, "$source_dir/$dir/" . $page->[0] );
	    
	    if ( get_extension( $page->[0] ) eq "txt" ) {
		$source_text = structuredText2HTML( $source_text );
	    }
	    
	    string_to_file( "$target_dir/$dir/" . remove_extension( $page->[0] ) . ".html",
			    generateHTML->whole_page($page->[1], '', $index_title, $index,
#						     (remove_extension( $page->[0] ) eq "index" ) ?
							 $page->[1],
#							 generateHTML->content_title( $section->[1], "John Doe", $page->[1] ),
						     $source_text,
						     $latest_news ), # scalar localtime($incoming_date) ),
			    $file_perm_public );
	}
    }
}

sub remove_extension {
    $filename = $_[0];
    $filename =~ s/\..*?$//;
    return $filename;
}

sub get_extension {
    $filename = $_[0];
    $filename =~ s/^.*\.//;
    return $filename;
}

sub copy_skin_resources {
    mkdir "$target_dir/$skin_dir";
    opendir(SKINDIR, $skin_dir );
    while($dirname = readdir(SKINDIR)) {
	if ($dirname =~ /_resources$/ && -d "$skin_dir/$dirname" ) {
	    system( $cp, "-a", "$skin_dir/$dirname", "$target_dir/$skin_dir");
	    
	}
    }
    closedir(SKINDIR);
}
