#!/usr/bin/perl -T

$request = 'main';

# breaks up a string according to a separator
if(defined($ENV{REQUEST_URI}))
{
  @uri = split('/', $ENV{REQUEST_URI});

  if( $#uri > -1 && -e "/openwcms/data/$uri[1].html" )
  {
    $request = "$uri[1]";
  }
}

require '/openwcms/add_items.pl';
# domain is always the first thing in the string, pick the first element
$domain = (split /\./, $head[0]{href})[0];

print "Content-type: text/html\n\n";
print <<eof;
<!DOCTYPE html>
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta name="viewport" content="width=device-width" />
	<link href="favicon.ico" type="image/x-icon" rel="icon" />
	<title>$head[0]{title} - $domain</title>
	<meta name="description" content="$head[0]{anchor}" />
	<link rel="stylesheet" href="/style/openwcms.css" type="text/css" />
</head>
<body>
<div id="body-wrapper">

<div id="header">
  <div class="branding"></div>
  <ul>
eof
foreach my $i (0 .. $#header)
{
  print qq(\t<li><a href="$header[$i]{href}" title="$header[$i]{title}">$header[$i]{anchor}</a></li>\n)
}
print <<eof;
  </ul>
</div>

<div id="main">
eof
open (FH, "/openwcms/data/$request.html");
while ($line = <FH>)
{
  chomp $line; # to remove the last trailing newline
  print $line;
}
close (FH);

print <<eof;
</div>

<div id="sidebar">
  <h3><a href='/'>News</a></h3>
  <ul>
eof
foreach my $i (0 .. $#sidebar)
{
  print qq(\t<li><a href='$sidebar[$i]{href}' title='$sidebar[$i]{title}'>[ $sidebar[$i]{anchor} ]</a></li>\n)
}
print <<eof;
  </ul>
</div>

</div>

<div id="footer">
  <a href="$footer[0]{href}" title="$footer[0]{title}" target="_blank">$footer[0]{anchor}</a>
</div>

</body>
</html>
eof