#!/usr/bin/env raku

use File::Temp;
use Shell::Command;
use paths;

unit sub MAIN(Bool :$upload);

my $tempdir = tempdir.IO;
my $ttc-number = dir('build/ttc').first.basename;
my $ttc = "build/ttc/$ttc-number".IO;

# Filenames to ignore while processing source files
my Str:D @ignored = ["README.md", "SUMMARY.md"];
# Check to see if a filename is ignored
sub not-ignored($path) {
    for @ignored -> $ignored {
        return False if $path.ends-with: $ignored;
    }
    return True;
}

# Copy a file from the current directory to the temporary directory, preserving
# realtive path. Resolves symlinks in source, but does not reflect symlink
# resoultion in the output path
sub cp-temp($src) {
   my $src-path = do given $src {
       when Str {
           $src.IO
       }
       when IO::Path {
           $src
       }
       default {
           die "Invalid source $src, {$src.WHAT}"
       }
   }
   my $output-path = $tempdir.add($src-path.relative).IO;
   # Create the parent directory if needed
   if !$output-path.parent.d {
       $output-path.parent.mkdir;

   }
   # Copy the file
   $src-path.resolve.copy: $output-path;
}

# Invoke katla on a source file, streaming its output to the temporary directory
sub katla($src, $ttc-src) {
    # Run katla and collect the output
    my $katla = run 'katla', 'markdown', $src, $ttc-src, :out;
    my $output = $katla.out.slurp(:close);
    # TODO: Post process them to set themeing correctly
    $output ~~ s:g/'<style>' .* '</style>'//;
    $output ~~ s:g/'<br />'//;
    $output ~~ s:g/'\\*'/*/;
    $output ~~ s:g/'\\_'/_/;
    $output ~~ s:g/'\\\\'/\\/;
    $output ~~ s:g/'<code'/<pre><code/;
    $output ~~ s:g/'</code>'/<\/code><\/pre>/;
    $output ~~ s:g/'class="IdrisKeyword"'/class="hljs-keyword"/;
    $output ~~ s:g/'class="IdrisModule"'/class="hljs-symbol hljs-emphasis"/;
    $output ~~ s:g/'class="IdrisComment"'/class="hljs-comment"/;
    $output ~~ s:g/'class="IdrisFunction"'/class="hljs-symbol"/;
    $output ~~ s:g/'class="IdrisBound"'/class="hljs-name"/;
    $output ~~ s:g/'class="IdrisData"'/class="hljs-title"/;
    $output ~~ s:g/'class="IdrisType"'/class="hljs-type"/;
    $output ~~ s:g/'class="IdrisNamespace"'/class="hljs-symbol hljs-emphasis"/;

    # Spurt the output to the temporary directory
    my $output-path = $tempdir.add: $src;
    if !$output-path.parent.d {
        $output-path.parent.mkdir;
    }
    $output-path.spurt($output);
}

# Copy our metadata files
cp-temp "book.toml";
# TODO: Special Handling for the README so we don't need the symlink and the
# links work
cp-temp "src/README.md";
cp-temp "src/SUMMARY.md";

# Katla over the source files
for paths("src", :file(*.&not-ignored)) -> $path {
    my $ttc-path = $ttc.add($path.IO.relative: "src").extension: "ttm";
    katla $path.IO.relative, $ttc-path.relative;
}

# Build the book

indir $tempdir, {
    my $mdbook = run <mdbook build>;
    die "Ooops" unless $mdbook;
}

# Copy it over
rm_rf "book";
cp $tempdir.add("book"), "book", :r;

if $upload {
   my $rsync = run 'rsync', '-avzh', $tempdir.add("book").Str,
       'ubuntu@static.stranger.systems:/var/www/static.stranger.systems/idris-by-contrived-example';
   die "rsync went bad" unless $rsync;
}