<?php

require __DIR__ . "/../vendor/autoload.php";

require "lib/settings.php";
require "lib/mustache.php";
require "lib/markdown.php";

function toHtml($file, $stache = null): string {
    global $_SETTINGS, $STACHECONTEXT;

    if ($file->getExtension() != "php") {
        if ($stache == null) {
            $stache = $STACHECONTEXT;
        }
        $contents = file_get_contents($file->getPathname());
        $cleancontents = "";
        foreach (preg_split("/((\r?\n)|(\r\n?))/", $contents) as $line) {
            if (strpos($line, "// ") === 0) {
                $keyval = explode(" ", str_replace("// ", "", $line), 2);
                $stache[$keyval[0]] = $keyval[1];
            } else {
                $cleancontents .= $line . "\n";
            }
        }
        $contents = stacheify($cleancontents, "", $stache);
    }
    switch ($file->getExtension()) {
        case "md":
            $html = md2html($contents);
            return stacheify($html, $_SETTINGS["md_template"], $stache);
        case "htm": // .htm for HTML files that should be templated
            return stacheify($contents, $_SETTINGS["md_template"], $stache);
        case "html":
            return $contents;
        case "php":
            ob_start();
            include $file->getPathname();
            $contents = ob_get_clean();
            return stacheify($contents, "", $stache);
        default:
            return "";
    }
}

echo "Compiling templates and included snippets...\n";
$STACHECONTEXT = getstachecontext();

echo "Cleaning old files in $_SETTINGS[output_folder]...\n";
$outputdir = __DIR__ . '/../' . $_SETTINGS["output_folder"];
$di = new RecursiveDirectoryIterator($outputdir, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($ri as $file) {
    $file->isDir() ? rmdir($file) : unlink($file);
}

$it = new RecursiveDirectoryIterator(__DIR__ . "/../" . $_SETTINGS["source_folder"] . "/pages", \RecursiveDirectoryIterator::SKIP_DOTS);

echo "Compiling pages...\n";
foreach (new RecursiveIteratorIterator($it) as $file) {
    if ($file->isDir()) {
        continue;
    }
    $html = toHtml($file);
    if ($html == "") {
        continue;
    }
    $indenter = new \Gajus\Dindent\Indenter();
    $html = $indenter->indent($html);
    $outpath = str_replace($_SETTINGS["source_folder"] . "/pages", $_SETTINGS["output_folder"], $file->getPath());
    if (!file_exists($outpath)) {
        mkdir($outpath, 0777, true);
    }
    $outfile = $outpath . "/" . $file->getBasename("." . $file->getExtension()) . ".html";
    echo "  " . explode("../$_SETTINGS[output_folder]", $outfile, 2)[1] . "\n";
    file_put_contents($outfile, $html);
}


echo "Copying static content...\n";
$source = __DIR__ . "/../" . $_SETTINGS["source_folder"] . "/static";
$dest = __DIR__ . "/../" . $_SETTINGS["output_folder"];

foreach ($iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST) as $item) {
    echo "  " . explode("../$_SETTINGS[source_folder]/static", $item, 2)[1] . "\n";
    if ($item->isDir()) {
        mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
    } else {
        copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
    }
}