#!/usr/bin/icmake -qt/tmp/flexc++

#include "INSTALL.im"

#define BINARY              "flexc++" 
#define COMPILER            "g++ -Wall --std=c++0x "
#define LIBRARY             "flexc++"       // NO lib, NO .a
#define LIBS                "-lbobcat"      // -l required
#define MAIN                "flexc++.cc"
#define SOURCES             "*.cc"
#define TMP_DIR             "tmp"

list    g_classes;          // all class-directories
int     g_nClasses;         // number of class-directories
int     g_compiled;         // any source compiled (but main)?
list    g_classLines;       // list of all lines in CLASSES
list    g_inspected;        // list of classes whose dependencies have been 
                            // inspected (used with USE_ALL)
int     g_base;             // compile the sources in the base directory
int     g_chdir;            // display chdirs to directories without sources
                            // to compile
string  g_compiler;
string  g_cwd = chdir("");  // initial working directory

#include "icmake/run"
#include "icmake/md"
#include "icmake/getenv"
#include "icmake/special"
#include "icmake/clean"
#include "icmake/manpage"
#include "icmake/manual"
#include "icmake/library2"
#include "icmake/program"

// #include "icmake/rebuild"

#include "icmake/install"

// #include "icmake/test"

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

    g_env = envp;

    setLocations();     // from INSTALL.im

    my_getenv("DRYRUN");
    g_dryrun = g_envvar;

    option = element(1, argv);
    option2 = element(2, argv);
    
    if (option2 == "strip")
        strip = "-s";

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

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

    if (option == "install")
        install(option2, 1);

    if (option == "installprog")
        install(option2, 0);

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

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

    if (option == "library")
    {
        build_library();
        exit(0);
    }

    if (option == "program")
        program(strip);

//    if (option == "rebuild")
//        rebuild(strip);

    if (option == "scanner")
    {
        chdir("scanner");
        system("./newscanner");
        chdir("..");
        program(strip);
    }

//    if (option == "test")
//        test(option2);

    printf("Usage: build [-p] what\n"
        "Where `what' is one of:\n"
        "   clean              - clean up remnants of previous compilations\n"
        "   distclean          - clean + fully remove tmp/\n"
        "   library            - build flexc++'s library\n"
        "   man                - build the man-page (requires Yodl)\n"
        "   manual             - build the manual (requires Yodl)\n"
        "   program [strip]    - build flexc++ (optionally strip the\n"
        "                        executable)\n"
        "   scanner [strip]    - build new scanner, then 'build program'\n"
        "   test [what]        - run the test-suite\n"
        "                        `what' can be the name of a specific test\n"
        "                        to run. If omitted, all tests are run.\n"
        "   rebuild [strip]    - recreate the parser using flexc++ created\n"
        "                        by `build program' (optionally strip the\n"
        "                        executable)\n"
        "   install <base>     - install the software in the locations "
                                                            "defined\n"
                                                               
        "                        in the INSTALL.cf file, optionally "
                                                            "below <base>\n"
        "   installprog <base> - only install the program and skeleton files\n"
        "\n"
        "If the environment variable DRYRUN is defined, no commands are\n"
        "actually executed\n"
        "\n"
    );
    exit(1);
}
