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

#define LOGENV "BOBCAT"

#include "INSTALL.im"

#define LIBRARY             "bobcat"

list g_classes;         // list of classes/directories, listed in CLASSES

int g_echo = ON;
int g_nClasses;         // number of classes/subdirectories
int g_all = 1;          // if 0: only compile the classes not requiring
                        // additional libaries. see icmake/special

string  
    g_logPath = getenv(LOGENV)[1],
    g_cwd,              // current WD, ends in /.
    g_file,             // used to store the name of a file from a list
    g_copt,             // compiler options
    g_cxx,              // compiler used
    g_sources,          // sources to be used
    g_version,          // The library's version from the file VERSION
    g_tmpliba,          // Static Library compilation target
    g_tmplibso,         // Shared Library compilation target
    g_tmphdr,           // Class header target
    g_tmpbin,           // Binary target
    g_sharedLibReq;     // libraries required by the shared lib


#include "icmake/cuteoln"
#include "icmake/backtick"
#include "icmake/run"
#include "icmake/md"
#include "icmake/getenv"
#include "icmake/clean"
#include "icmake/addclasses""
#include "icmake/special"

#include "icmake/findall"
#include "icmake/loginstall"
#include "icmake/logrecursive"
#include "icmake/logzip"

#include "icmake/uninstall"

#include "icmake/man"
#include "icmake/github"
#include "icmake/library"
#include "icmake/libraries"
#include "icmake/install"


void main(int argc, list argv)
{
    string option;
    string all;
    string strip;
    int keepPch = 0;
    int idx;

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

    echo(g_echo);

    g_cwd = chdir(".");

    g_tmpliba   = "tmp/liba";
    g_tmplibso  = "tmp/libso";
    g_tmphdr    = "tmp/" LIBRARY "/";
    g_tmpbin    = "tmp/bin";


    g_cxx =  setOpt(CXX, "CXX");
    g_copt = setOpt(CXXFLAGS, "CXXFLAGS");

    option = argv[1];

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

    if (option == "light")
    {
        option = "libraries";
        g_all   = 0;
        libraries(keepPch, "bobcat", 1, argv[2] == "strip");
    }

        // 'all': all all libraries unconditionally
        // not 'all': ask for the inclusion of additional libraries
        // g_light: do not consider the additional libraries
    if (option == "libraries")
    {
        all = argv[2];          // maybe 'all' or 'strip' or empty

        if (all == "all")       // all -> specify strip from [3]
            strip = argv[3];
        else                    // not all: defines 'strip'
            strip = all;

        libraries(keepPch, "bobcat", all == "all", strip == "strip");
    }

    if (option == "l")
    {
        system(P_NOCHECK, "tput clear");
        libraries(keepPch, "bobcat", 1, 0);
    }

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

    if (option == "github")
        github();

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

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

    printf("Usage: build [-q -p -P] what\n"
        "Where\n"
        "   [-q]:   run quietly, do not show executed commands\n"
        "   [-p]:   used with build light, build libraries and build l\n"
        "           when specified, the precompiled headers are kept after\n"
        "           constructing the libraries\n"   
        "   [-P]:   do not use precompiled headers\n"
        "`what' is one of:\n"
        "   clean               - clean up remnants of previous compilations\n"
        "   distclean           - same as clean\n"
        "   man                 - build the manual page (requires Yodl)\n"
        "   light [strip]       - build the bobcat(-dev) libraries without\n"
        "                         the classes requiring the Milter, X11, SSL "
                                                                    "and\n"
        "                         Readline libraries\n"
        "   libraries [all] [strip] - build the bobcat(-dev) libraries\n"
        "                         (optionally add optional classes,\n"
        "                         optionally strip the libraries)\n"
        "   l                   - same as 'libraries all', but clearing the\n"
        "                         screen before starting the compilation\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"
        "                               d (documentation),\n"
        "                               h (header files),\n"
        "                               l (libraries),\n"
        "                               m (man-pages)\n"
        "   uninstall logfile   - remove files and empty directories listed\n"
        "                         in the file 'logfile'\n"
        "   github              - prepare github's gh-pages update\n"
        "                         (internal use only)\n"
    );
    exit(0);
}
