#!/usr/pkg/bin/perl
# socgen - Generate Steam of Consciousness HTML page
# 2015 David Meyer <papa@sdf.org> +JMJ

use strict;
use warnings;
use Getopt::Std;
use Fortune;
use Text::Template;;

my $TRUE = 1;
my $FALSE = 0;

my $shdmnuf = ".sochdmnu";
my $sftmnuf = ".socftmnu";
my $ssidebrf = ".socsidbr";
my $sfortunf = "../bonmots/bonmots";

sub alify
{
    my ($bolist, $ssechd, @surilab) = @_;
    my ($salist, $slopen, $slclose);
    my $bopenlst = $FALSE;
    if ($bolist) {$slopen = '<ol>'; $slclose = '</ol>';}
    else         {$slopen = '<ul>'; $slclose = '</ul>';}
    foreach my $surilab (@surilab)
    {
	if ($surilab =~ /^\[(.*)\]$/)
	{
	    my $ssechtxt = $1;
	    if ($bopenlst) 
	    {
		$salist .= $slclose;
		$bopenlst = $FALSE;
	    }
	    if ($ssechd) 
	    {
		$salist .= "<$ssechd>$ssechtxt</$ssechd>";
	    }
	    else         { $salist .= "<p>$ssechtxt</p>"; }
	}
	elsif ($surilab =~ /(\S*)?\s+(.*)?/)
	{
	    my $suri = $1; my $slabel = $2;
	    if (!$bopenlst)
	    {
		$salist .= $slopen;
		$bopenlst = $TRUE;
	    }
	    if ($suri && $slabel) 
	    {
		$salist .= "<li><a href=\"$suri\">$slabel</a></li>";
	    }
	    elsif ($suri) 
	    {
		$salist .= "<li><a href=\"$suri\">$suri</a></li>";
	    }
	    elsif ($slabel) 
	    {
		$salist .= "<li>$slabel</li>";
	    }
	}
    }
    if ($bopenlst) { $salist .= $slclose; }
    return $salist;
}

open (my $FHM, $shdmnuf) || die "Couldn't open file $shdmnuf";
my @shdmenu = <$FHM>;

open (my $FSB, $ssidebrf) || die "Couldn't open file $shdmnuf";
my @ssidebar = <$FSB>;

open (my $FFM, $sftmnuf) || die "Couldn't open file $sftmnuf";
my @sftmenu = <$FFM>;

my $ofortune = new Fortune ($sfortunf);
$ofortune->read_header ();

my $template = Text::Template->new(SOURCE => 'soc-lsb.tmpl')
    or die "Couldn't construct template: $Text::Template::ERROR";

my %tmplvars = (TITLE => 'Test Title',
		CSS => 'soctest.css',
		HEADMENU => alify ($FALSE, "", @shdmenu),
		LSIDEBAR => alify ($FALSE, 'h5', @ssidebar),
		MAINCONT => '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.  Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.  Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum doloe eu fugiat nulla
pariatur.  Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</p>
      <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua.  Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat.  Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum doloe eu fugiat nulla
pariatur.  Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.</p>',
		FORTUNE => $ofortune->get_random_fortune (),
		FOOTMENU => alify ($FALSE, "", @sftmenu),
		HOSTLINK => '<p><a href="/">Powered by localhost</a></p>'
    );

my $html = $template->fill_in(HASH => \%tmplvars);

if (defined $html) { print $html }
else { die "Couldn't fill in template: $Text::Template::ERROR" }