#!/usr/bin/env rdmd
/+
  sdp
+/
/+ sdp: sisu document parser, see http://sisudoc.org +/
import
  compile_time_info,          // sdp/compile_time_info.d
  ao_abstract_doc_source,     // sdp/ao_abstract_doc_source.d
  ao_conf_make_meta,          // sdp/ao_conf_make_meta.d
  ao_conf_make_meta_native,   // sdp/ao_conf_make_meta_native.d
  ao_conf_make_meta_sdlang,   // sdp/ao_conf_make_meta_sdlang.d
  ao_defaults,                // sdp/ao_defaults.d
  ao_output_debugs,           // sdp/ao_output_debugs.d
  ao_read_config_files,       // sdp/ao_read_config_files.d
  ao_read_source_files,       // sdp/ao_read_source_files.d
  ao_rgx,                     // sdp/ao_rgx.d
  output_hub,                 // output_hub.d
  output_html;                // output_html.d
/+ sdlang http://sdlang.org +/
import sdlang;                            // sdlang.d
/+ std +/
private import
  std.algorithm,
  std.array,
  std.container,
  std.exception,
  std.getopt,
  std.json,
  std.process,
  std.stdio,
  std.file,
  std.range,
  std.regex,
  std.string,
  std.traits,
  std.typecons,
  std.utf,
  std.conv : to;

mixin(import("version.txt"));
mixin CompileTimeInfo;
mixin RgxInit;
/++ A SiSU document parser writen in D. +/
void main(string[] args) {
  mixin SiSUregisters;
  mixin SiSUheaderExtractHub;
  mixin SiSUheaderExtractSDLang;
  mixin SiSUbiblio;
  mixin SiSUrgxInitFlags;
  mixin SiSUconfigSDLangHub;
  mixin SiSUmarkupRaw;
  mixin SiSUdocAbstraction;
  mixin SiSUoutputDebugs;
  mixin SiSUoutputHub;
  auto raw = MarkupRaw();
  auto head = HeaderDocMetadataAndMake();
  auto abs = Abstraction();
  auto dbg = SDPoutputDebugs();
  auto output = SDPoutput();
  /+
  struct DocumentParts {
    string[string][] contents;
    string[string][string] meta_aa;
    string[string][string] make_aa;
    string[][string][string] bookindex_unordered_hashes;
    JSONValue[] biblio;
  }
  +/
  string[] fns_src;
  string flag_action;
  string arg_unrecognized;
  auto rgx = Rgx();
  scope(success) {
    debug(checkdoc) {
      writefln(
        "~ run complete, ok ~ (sdp-%s.%s.%s, %s v%s, %s %s)",
        ver.major, ver.minor, ver.patch,
        __VENDOR__, __VERSION__,
        bits, os,
      );
    }
  }
  scope(failure) {
    debug(checkdoc) {
      stderr.writefln(
        "run failure",
      );
    }
  }
  bool[string] opt_action_bool = [
    "assertions"      : false,
    "concordance"     : false,
    "digest"          : false,
    "docbook"         : false,
    "epub"            : false,
    "html"            : false,
    "manifest"        : false,
    "no_ocn"          : false,
    "odt"             : false,
    "pdf"             : false,
    "postgresql"      : false,
    "qrcode"          : false,
    "sisupod"         : false,
    "source"          : false,
    "sqlite"          : false,
    "text"            : false,
    "verbose"         : false,
    "xhtml"           : false,
    "xml_dom"         : false,
    "xml_sax"         : false,
  ];
  auto helpInfo = getopt(args,
    std.getopt.config.passThrough,
    "assert",         "--assert set optional assertions on",       &opt_action_bool["assertions"],
    "concordance",    "--concordance file for document",           &opt_action_bool["concordance"],
    "digest",         "--digest hash digest for each object",      &opt_action_bool["digest"],
    "docbook",        "--docbook process docbook output",          &opt_action_bool["docbook"],
    "epub",           "--epub process epub output",                &opt_action_bool["epub"],
    "html",           "--html process html output",                &opt_action_bool["html"],
    "manifest",       "--manifest process manifest output",        &opt_action_bool["manifest"],
    "no-ocn",         "--no-ocn suppress object cite numbers",     &opt_action_bool["no_ocn"],
    "odf",            "--odf process odf:odt output",              &opt_action_bool["odt"],
    "odt",            "--odt process odf:odt output",              &opt_action_bool["odt"],
    "pdf",            "--pdf process pdf output",                  &opt_action_bool["pdf"],
    "pg",             "--pg process postgresql output",            &opt_action_bool["postgresql"],
    "postgresql",     "--postgresql process postgresql output",    &opt_action_bool["postgresql"],
    "qrcode",         "--qrcode with document metadata",           &opt_action_bool["qrcode"],
    "sisupod",        "--sisupod sisupod source content bundled",  &opt_action_bool["sisupod"],
    "source",         "--source markup source text content",       &opt_action_bool["source"],
    "sqlite",         "--sqlite process sqlite output",            &opt_action_bool["sqlite"],
    "text",           "--text process text output",                &opt_action_bool["text"],
    "txt",            "--txt process text output",                 &opt_action_bool["text"],
    "verbose|v",      "--verbose output to terminal",              &opt_action_bool["verbose"],
    "xhtml",          "--xhtml process xhtml output",              &opt_action_bool["xhtml"],
    "xml-dom",        "--xml-dom process xml dom output",          &opt_action_bool["xml_dom"],
    "xml-sax",        "--xml-sax process xml sax output",          &opt_action_bool["xml_sax"],
  );
  if (helpInfo.helpWanted) {
    defaultGetoptPrinter("Some information about the program.", helpInfo.options);
  }
  foreach(arg; args) {
    if (match(arg, rgx.flag_action)) {
      flag_action ~= " " ~ arg;   // flags not taken by getopt
    } else if (match(arg, rgx.src_pth)) {
      fns_src ~= arg;             // gather input markup source file names for processing
    } else {                      // anything remaining, unused
      arg_unrecognized ~= " " ~ arg;
    }
  }
  auto conf = ConfigHub();
  auto sdl_root_configuration = conf.configSDLang("conf.sdl");
  auto sdl_root_doc_make = conf.configSDLang("sisu_document_make");
  auto confsdl = HeaderExtractSDL();
  auto conf_settings_aa = confsdl.configSettingsSDLangToAAmake(sdl_root_configuration);
  auto conf_doc_make_aa = confsdl.documentMakeSDLangToAAmake(sdl_root_doc_make);
  foreach(fn_src; fns_src) {
    if (!empty(fn_src)) {
      scope(success) {
        debug(checkdoc) {
          writefln(
            "%s\n%s",
            "~ document complete, ok ~",
            "-------------------------------",
          );
        }
      }
      scope(failure) {
        debug(checkdoc) {
          stderr.writefln(
            "~ document run failure ~ (%s  v%s)\n\t%s",
            __VENDOR__, __VERSION__,
            fn_src
          );
        }
      }
      enforce(
        match(fn_src, rgx.src_pth),
        "not a sisu markup filename"
      );
      /+ ↓ read file +/
      auto read_in_file_string = raw.sourceContent(fn_src);
      /+ ↓ file tuple of header and content +/
      auto header_and_body_tuple = raw.sourceContentSplitIntoHeaderAndBody(read_in_file_string, fn_src);
      auto header = header_and_body_tuple[0];
      auto content_body = header_and_body_tuple[1];
      debug(header_and_body) {
        writeln(header);
        writeln(header_and_body_tuple.length);
        writeln(content_body[0]);
      }
      /+ ↓ split header into make and meta +/
      auto header_make_and_meta_tuple = head.headerContentAA(header, conf_doc_make_aa);
      static assert(!isTypeTuple!(header_make_and_meta_tuple));
      string[string][string] dochead_make = header_make_and_meta_tuple[0];
      string[string][string] dochead_meta = header_make_and_meta_tuple[1];
      /+ ↓ document abstraction: process document, return abstraction as tuple +/
      auto t = abs.abstract_doc_source(content_body, dochead_make, dochead_meta);
      static assert(!isTypeTuple!(t));
      auto doc_ao_contents = t[0]; // head ~ toc ~ contents ~ endnotes_seg ~ glossary ~ bibliography ~ bookindex ~blurb;
      auto doc_ao_bookindex_unordered_hashes = t[1]; // redundant?
      auto doc_ao_biblio = t[2];                     // redundant?
      string[][string] document_section_keys_sequenced = [
        "seg": [
           "head",
           "toc_seg",
           "body",
         ],
         "scroll": [
           "head",
           "toc_scroll",
           "body",
         ]
      ];
      if (doc_ao_contents["endnotes_seg"].length > 1) {
        document_section_keys_sequenced["seg"] =
          document_section_keys_sequenced["seg"] ~= "endnotes_seg";
      }
      if (doc_ao_contents["endnotes_scroll"].length > 1) {
        document_section_keys_sequenced["scroll"] =
          document_section_keys_sequenced["scroll"] ~= "endnotes_scroll";
      }
      if (doc_ao_contents["glossary"].length > 1) {
        document_section_keys_sequenced["seg"] =
          document_section_keys_sequenced["seg"] ~= "glossary";
        document_section_keys_sequenced["scroll"] =
          document_section_keys_sequenced["scroll"] ~= "glossary";
      }
      if (doc_ao_contents["bibliography"].length > 1) {
        document_section_keys_sequenced["seg"] =
          document_section_keys_sequenced["seg"] ~= "bibliography";
        document_section_keys_sequenced["scroll"] =
          document_section_keys_sequenced["scroll"] ~= "bibliography";
      }
      if (doc_ao_contents["bookindex_seg"].length > 1) {
        document_section_keys_sequenced["seg"] =
          document_section_keys_sequenced["seg"] ~= "bookindex_seg";
      }
      if (doc_ao_contents["bookindex_scroll"].length > 1) {
        document_section_keys_sequenced["scroll"] =
          document_section_keys_sequenced["scroll"] ~= "bookindex_scroll";
      }
      if (doc_ao_contents["blurb"].length > 1) {
        document_section_keys_sequenced["seg"] =
          document_section_keys_sequenced["seg"] ~= "blurb";
        document_section_keys_sequenced["scroll"] =
          document_section_keys_sequenced["scroll"] ~= "blurb";
      }
      /+ ↓ debugs +/
      debug(checkdoc) {
        dbg.abstract_doc_source_debugs(
          doc_ao_contents,
          document_section_keys_sequenced,
          doc_ao_bookindex_unordered_hashes, // redundant?
          doc_ao_biblio,                     // redundant?
          dochead_make,
          dochead_meta,
          fn_src,
          opt_action_bool
        );
      }
      /+ ↓ output hub +/
      output.hub(
        doc_ao_contents,
        document_section_keys_sequenced,
        dochead_make,
        dochead_meta,
        fn_src,
        opt_action_bool
      );
      scope(exit) {
        debug(checkdoc) {
          writefln(
            "processed file: %s",
            fn_src
          );
        }
        destroy(content_body);
        destroy(t);
        destroy(doc_ao_contents);
        destroy(doc_ao_biblio);
        destroy(fn_src);
      }
    } else {
      /+ no recognized filename provided +/
      writeln("no recognized filename");
      break;
    }
  }
}
unittest {
  /++
	name        "sdp"
	description "A SiSU document parser writen in D."
	homepage    "http://sisudoc.org"
  +/
}