#!/usr/bin/icmake -t.

#define LOGENV "SIMRISC"

#include "icmconf"

string
    g_logPath = getenv(LOGENV)[1],
    g_cwd = chdir("");  // initial working directory, ends in /

int g_echo = ON;

#include "icmake/cuteoln"
#include "icmake/backtick"

#include "icmake/setopt"
#include "icmake/beep"      // beep if /usr/local/bin/beep exists and exit
#include "icmake/run"
#include "icmake/md"

#include "icmake/pathfile"
#include "icmake/findall"
#include "icmake/loginstall"
#include "icmake/logzip"
#include "icmake/logfile"
#include "icmake/uninstall"

#include "icmake/special"

#include "icmake/clean"
#include "icmake/manpage"

#include "icmake/latexdoc"
#include "icmake/pdf"
#include "icmake/manual"

#include "icmake/install"
#include "icmake/gitlab"

void main(int argc, list argv)
{
    string option;
    int idx;

    for (idx = listlen(argv); idx--; )
    {
        if (argv[idx] == "-q")
        {
            g_echo = OFF;
            argv -= (list)"-q";
        }
    }

    echo(g_echo);

    option = argv[1];

    if (option == "clean")
        clean(0);

    if (option == "distclean")
        clean(1);

    if (option != "")
        special();

    if (option == "install")
        install(argv[2], argv[3]);

    if (option == "uninstall")
        uninstall(argv[2]);

    if (option == "gitlab")
        gitlab();

    if (option == "man")
        manpage();

    if (option == "manual")
        manual();

    if (option == "pdf")            // creates the pdf version of the
        pdf();                      // tech manual

    if (option == "library")
    {
        system("icmbuild library");
        beep();
    }

    if (option == "program")
    {
        system("icmbuild program");

        if (argv[2] == "strip")
            system("strip " TMP_DIR "/bin/binary");
        beep();
    }

    if (option == "xref")
    {
        system("icmbuild program");
        run("oxref -t main -r replacements -fxs tmp/main.o tmp/lib"
                                            LIBRARY ".a > " PROGRAM ".xref");
        if (argv[2] == "strip")
            system("strip " TMP_DIR "/bin/binary");
        beep();
    }

    printf("Usage: build [-q] what\n"
        "Where\n"
        "   [-q]:   run quietly, do not show executed commands\n"
        "`what' is one of:\n"
        "   clean                - clean up remnants of previous "
                                                            "compilations\n"
        "   distclean            - clean + fully remove tmp/\n"
        "   library              - build " PROGRAM "'s library\n"
        "   man                  - build the man-page (requires Yodl)\n"
        "   manual               - build the html manual (requires Yodl)\n"
        "   pdf                  - build the pdf manual (requires Yodl)\n"
        "   program [strip]      - build " PROGRAM " (optionally strip the\n"
        "                          executable)\n"
        "   xref [strip]         - same a `program', also builds xref file\n"
        "                          using oxref\n"
        "   install selection [base] - to install the software in the \n"
        "                         locations defined in the INSTALL.im file,\n"
        "                         optionally below base\n"
        "                         selection can be\n"
        "                               x, to install all components,\n"
        "                         or a combination of:\n"
        "                               b (binary program),\n"
        "                               d (documentation),\n"
        "                               m (man-page)\n"
        "   uninstall logfile   - remove files and empty directories listed\n"
        "                         in the file 'logfile'\n"
        "   gitlab               - prepare gitlab's web-pages update\n"
        "                          (internal use only)\n"
        "\n"
    );
}
