#!/usr/bin/perl -w
#
# generate (c) Con4U 2001
# generovanie timoveho webu
# 22. okt 2001 Robo
#
# globalne pouzivane premenne:
# @files - zoznam suborov .template na najvyssej urovni hierarchie
# %names - tabulka: meno suboru => meno s diakritikou a medzerami a tak
#

# horizontalna ciara... taka pekna
sub ciara {
	print <<EOF;
<!-- ciara -->
  <tr bgcolor="#555555">
    <td><img src="pixel.gif" width="1" height="1" alt=""></td>
  </tr>

EOF
}

# cela hlavicka stranky aj s fakultou, bannerom a menu
sub html_header {
	print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Con4U - tímový projekt</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<style type="text/css">
  <!--
  BODY, P, DIV, TD, TH, TR, FORM, OL, UL, LI, INPUT, TEXTAREA, SELECT, A
  {
  	font-family: Verdana, Tahoma, Arial, Helvetica;
      }

  A  {
	  text-decoration: none;
  }
  -->
</style>

</head>
<body bgcolor="#ffffff" link="#3366CC" vlink="#336666" alink="#3366CC" text="#000000" marginwidth="0" marginheight="0" topmargin="0" leftmargin="0"> 

<table cellspacing="0" cellpadding="0" width="100%" border="0">

<!-- hlavicka -->
  <tr bgcolor="#e7e7e7" align="center">
    <td>
      <a target="_blank" href="http://www.stuba.sk"><font color="#555555" size="-2">
        Slovenská technická univerzita v Bratislave
      </font></a><br>
      <a target="_blank" href="http://www.elf.stuba.sk"><font color="#555555" size="-2"><b>
        Fakulta elektrotechniky a informatiky</b></font></a><br>
      <a target="_blank" href="http://www.dcs.elf.stuba.sk"><font color="#555555" size="-2">
        Katedra informatiky a výpočtovej techniky</font></a>
    </td>
  </tr>

EOF

	&ciara;

	print <<EOF;
<!-- logo -->
  <tr bgcolor="#f0f0f0" valign="middle" align="center">  
  <td>
    <font size="15">&nbsp;</font>
    <a href="index.html"><font size="15" color="1010c0"><b>
    Con4U</b></font></a>
    <font color="#101080">&nbsp;&nbsp;
    tímový projekt študentov inžinierskeho štúdia odboru informatika</font>
  </td>
  </tr>
EOF

	&ciara;

	print <<EOF;
<!-- menu -->
  <tr bgcolor="#e7e7e7" align="center">
  <td><font color="#555555">&middot;
EOF

	# tuto sa generuje menu
	foreach $f (@files) {
		print <<EOF;
<a href="$f.html"><font color="#555555"><b>
  $name{$f}</b></font></a> &middot;
EOF
	}

	print <<EOF;
  </font>
  </td>
  </tr>
EOF

	&ciara;
}

# toto nie je nic podstatne...
sub pole_header {
	print <<EOF;
<tr><td> <!-- toto musi ist prec, ked tu bude prave menu -->
<table cellspacing="0" cellpadding="0" width="100%" border="0">
<tr>
<td width="5" bgcolor="#f0f0f0"><img src="pixel.gif" width="1" height="1" alt=""></td>
<td align="left" valign="top" bgcolor="#f0f0f0">

EOF
}

# toto je tiez len html formalna hlavicka
sub prave_menu_header {
	print <<EOF;
</td>
<td width="5" bgcolor="#f0f0f0"><img src="pixel.gif" width="1" height="1" alt=""></td>
<td width="1" bgcolor="#555555"><img src="pixel.gif" width="1" height="1" alt=""></td>
<td width="5" bgcolor="#e7e7e7"><img src="pixel.gif" width="1" height="1" alt=""></td>

<!-- prave menu -->

<td align="left" valign="top" bgcolor="#e7e7e7" width="200">
EOF
}

# toto hadam netreba komentovat
sub html_close {
	print <<EOF;
</td></tr></table>

</td>
</tr>
</table>

</body>
</html>
EOF
}

# dumpnutie celej jednej stranky (na najvyssej urovni)
sub page {
	$name=$_[0];

	close STDOUT;
	open STDOUT, ">$name.html";
	
	open TEXTFILE, "$name.template" or die "Cannot open $name.template";
	

	&html_header;
	&pole_header;
	
	print <<EOF;
<font size="+2" color="#101080"><b>&nbsp;&nbsp;[$name{$name}]</b></font><br>
EOF

	while(<TEXTFILE>) {
		next if (/\<name/);	
		print;
	}
	close TEXTFILE;

#	&prave_menu_header;
#
#	open MENUFILE, "menu.template" or die "Cannot open menu.template";
#	while(<MENUFILE>) {print;}
#	close MENUFILE;
	
	&html_close;
}

# skenovanie hlavicky suboru .template
sub scan {
	$name=$_[0];
	
	$name{$name}=$name; # :)
	
	open TEXTFILE, "$name.template" or die "Cannot open $name.template";
	while(<TEXTFILE>) {
		if(/^\<name/) {
			/^\<name="(.*)"/;
			$name{$f}=$1;
			print STDERR "Name=$name{$f}\n";
			next;
		}
		last;
	}
	close TEXTFILE;
}

sub dir_file {
	$dirname=$_[0];
	shift @_;
	@files=@_;
	
	foreach $a (@files) {
		next unless $a =~ /.*\.template/;
		print "File $a in dir $dirname\n";
	}
}

# prebehnutie vsetkych .template suborov v adresari a vyvolanie prislusnych akcii nad nimi
sub files {
	local @ls=split(/\n/,`ls`);
	foreach $f (@ls) {
		next unless $f =~ /.*\.template/;
		if (-d $f) { # adresar
			#print STDERR "Directories not supported yet.\n";
			#next;
			@dir=split(/\n/,`ls $f`);
			if(! -e "$f/menu.template" ) {
				print STDERR "No menu.template in $f\n";
				next;
			}
			&dir_file($f,@dir);
			foreach $second (@dir) {
				$second =~ s/(.*).template/$1/;
				next if ($second =~ /menu/);
				# tu treba vygenerovat $f/$second.html
			}
			next;
		}
		$f =~ s/(.*)\.template/$1/;
		next if ($f =~ /menu/);
		push @files,$f;
	}
	foreach $f (@files) {
		print STDERR "Scanning header of $f.template\n";
		&scan($f);
	}
	foreach $f (@files) {
		print STDERR "Generating $f.html\n";
		&page($f);
	}
}

# void main() {
&files;
