#!/usr/bin/icmake -qt/tmp/stealth

#define LOGENV "STEALTH"

#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/run"
#include "icmake/md"
#include "icmake/findall"
#include "icmake/loginstall"
#include "icmake/logzip"
#include "icmake/logfile"
#include "icmake/uninstall"
#include "icmake/pathfile"
#include "icmake/precompileheaders"     // check the name of main.ih

#include "icmake/special"

#include "icmake/clean"
#include "icmake/manpage"
#include "icmake/manual"
#include "icmake/destinstall"
#include "icmake/install"
#include "icmake/gitlab"

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

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

    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 == "uguide")
        manual();

    if (option == "library")
    {
        precompileHeaders();
        system("icmbuild library");
        exit(0);
    }

    if (argv[2] == "strip")
        strip = "strip";
    
    if (option == "p")
    {
        system("tput clear");
        precompileHeaders();
        system("icmbuild program strip");
        exit(0);
    }

    if (option == "program")
    {
        precompileHeaders();
        system("icmbuild program " + strip);
        exit(0);
    }

    if (option == "xref")
    {
        precompileHeaders();
        system("icmbuild program " + strip);
        run("oxref -fxs tmp/lib" LIBRARY ".a > " PROJECT ".xref");
        exit(0);
    }

    printf("Usage: build [-q -P] what\n"
        "Where\n"
        "   [-q]:   run quietly, do not show executed commands\n"
        "   [-P]:   do not use precompiled headers\n"
        "`what' is one of:\n"
        "   clean                - clean up remnants of previous "
                                                            "compilations\n"
        "   distclean            - clean + fully remove tmp/\n"
        "   library              - build " PROJECT "'s library\n"
        "   man                  - build the man-page (requires Yodl)\n"
        "   uguide               - build the user guide (requires Yodl)\n"
        "   p                    - build " PROJECT " after `tput clear' and\n"
        "                          strip the executable)\n"
        "   program [strip]      - build " PROJECT " (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"
        "                               a (additional documentation),\n"
        "                               b (binary program),\n"
        "                               d (documentation),\n"
        "                               m (man-page)\n"
        "                               u (user-guide)\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"
        "If the environment variable DRYRUN is defined, no commands are\n"
        "actually executed\n"
        "\n"
    );
    exit(0);
}
