#!/usr/bin/env perl
-use strict;
-use warnings;
-use CGI;
-use DBI;
-use Template;
 
-require "secrets"; # server data - shit that doesn't need to uploaded.
+BEGIN {push @INC, './perl'}
+use Triple;
 
-my $cgi = CGI->new;
-
-my $tt = Template->new({
-    INCLUDE_PATH => '.',
-    INTERPOLATE  => 1,
-                      }) or die Template->error();
-
-# $dbfile from secrets
-my $dbh = DBI->connect("dbi:SQLite:uri=file:$dbfile?mode=rwc");
-
-my $path = $cgi->param('path') // '';
-
-my %site = (
-    ixtab => 'index',
-    img   => 'img',
-    );
-
-my $vars = (
-    sneed => 'feed',
-    chuck => 'fuck',
-    );
-
-my $page = $site{"$path"} // '404';
-
-print $cgi->header(
-    -Content_Type => 'text/html',
-    -charset => 'UTF-8',
-    );
-
-# I should probably put all of below into a buffer and run a pruning regexp on it
-$tt->process('tt/' . $page, $vars) or die $tt->error();
+Triple::main();
 
--- /dev/null
+package Triple;
+
+use strict;
+use warnings;
+
+use Sys::Syslog;
+use Data::Dumper;
+
+use CGI;
+use FCGI;
+use Template;
+
+use feature 'state';
+
+# Internal
+use Secrets;
+
+our $debug = 0;
+
+our @boards = (
+    '/img/',
+    '/doc/',
+    );
+
+our %titles = (
+    '/' => 'Root',
+    '/img/' => 'Imgb.',
+    '/doc/' => '2nd.',
+    );
+
+our %descriptions = (
+    '/' => 'Forum Index',
+    '/img/' => 'Test Board',
+    '/doc/' => 'Secondary Board',
+    );
+
+our %sitemap_static = (
+        '/'       => 'index',
+        '/404/'   => '404',
+        );
+our %sitemap;
+
+sub sitemapgen {
+    %sitemap = %sitemap_static;
+    foreach (@boards) {
+        syslog("info", "+ $_");
+        $sitemap{"$_"} = 'img';
+    }
+}
+
+sub sitename {
+    use Cwd;
+    my $sitename = getcwd(); # pwd
+    $sitename =~ s/.*\///;   # /a/b/c/sitename/ => sitename
+    return $sitename;
+}
+
+sub debug {
+    my $sitename = $_[0];
+    $debug = 1 if $sitename =~ /^test\./;
+    openlog($sitename, "ndelay,pid", Sys::Syslog::LOG_DAEMON);
+}
+
+sub handlers {
+    my $include = $_[0];
+    my $request = FCGI::Request();
+    my $template;
+    $template = Template->new({INCLUDE_PATH => $include}) or syslog("info", $template->error());
+    return ($request, $template);
+}
+
+sub version {
+    open my $fh, '<', $_[0] or die "Can't open file: $!";
+    my $line = uc(substr(<$fh>, 0, 12));
+    close $fh;
+    return $line;
+}
+
+sub init {
+    my $sitename = sitename();
+    debug($sitename);
+    my ($request, $template) = handlers('tt');
+    sitemapgen();
+    return $sitename, $request, $template;
+}
+
+sub main() {
+    my ($sitename, $request, $template) = Triple::init();
+
+    my $version = Triple::version("version");
+    syslog("info", "loading $version");
+
+    my %static;
+    {
+        my $vars = {
+            boards       => \@Triple::boards,
+            titles       => \%Triple::titles,
+            descriptions => \%Triple::descriptions,
+            sitename     => $sitename,
+            version      => $version,
+        };
+        foreach (my ($path, $page) = each %sitemap_static) {
+            syslog("info", "staticify $path");
+            $vars->{uri} = $path;
+            my $description = $Triple::descriptions{$path} // 'No Description Retard';
+            $vars->{description} = $description;
+            my $pregen;
+            $template->process($page, $vars, \$pregen) or syslog("info", $template->error());
+            $static{$path} = $pregen;
+        }
+    }
+
+
+    while($request->Accept() >= 0) {
+        my $cgi = CGI->new;
+
+        # my $method = $ENV{'REQUEST_METHOD'} || '?';
+        my $ip = $ENV{'REMOTE_ADDR'} || '?';
+        $ip = 'hidden' if not $Triple::debug;
+
+        my $path = $ENV{'REQUEST_URI'} // '/';
+        my $page = $Triple::sitemap{$path} // '404';
+        my $stale = $static{$path} || '';
+
+        print $cgi->header(
+            -Content_Type => 'text/html',
+            -charset      => 'UTF-8',
+            );
+        if (not $stale) {
+            syslog("info", "serving live");
+            my $description = $Triple::descriptions{$path} // 'No Description Retard';
+            my $vars = {
+                uri          => $path,
+                description  => $description,
+                sitename     => $sitename,
+                version      => $version,
+                ip           => $ip,
+            };
+            $template->process($page, $vars) or syslog("info", $template->error());
+        } else {
+            syslog("info", "serving stale");
+            print $stale;
+        }
+        # my $description = $Triple::descriptions{$path} // 'No Description Retard';
+        # syslog("info", "$ip $method $path template $page described as '$description'") if $Triple::debug;
+    }
+}
+
+1;
 
--- /dev/null
+<head>
+<link rel="icon" type="image/png" sizes="16x16" href="/static/favicon.png">
+<link rel="stylesheet" type="text/css" href="/static/style.css">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+<meta name="viewport" content="width=device-width, initial-scale=0.5">
+<meta property="og:title" content="[% sitename %]">
+<meta property="og:type" content="website">
+<meta property="og:url" content="https://[% sitename %][% uri %]">
+<meta property="og:image" content="/logo.png">
+<meta property="og:site_name" content="[% sitename %]">
+<meta name="description" content="[% description %]">
+<title>[% uri %] @ [% sitename %]</title>
+</head>