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

#include "INSTALL.im"

#define BINARY              "bisonc++" 
#define COMPILER            "g++ -Wall --std=c++0x "
#define LIBRARY             "bisonc++"       // NO lib, NO .a
#define LIBS                "-lbobcat"      // -l required
#define MAIN                "bisonc++.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"

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 = argv[1];

    if (argv[2] == "strip")
        strip = "-s";
    
    if (option == "clean")
        clean(0);

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

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

    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("flexc++ lexer");
        chdir("..");
        program(strip);
    }

    if (option == "sourceforge")
    {
        special();
        exit(0);
    }


    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 bisonc++'s library\n"
        "   man                - build the man-page (requires Yodl)\n"
        "   manual             - build the manual (requires Yodl)\n"
        "   program [strip]    - build bisonc++ (optionally strip the\n"
        "                        executable)\n"
        "   scanner [strip]    - build new scanner, then 'build program'\n"
        "   rebuild [strip]    - recreate the parser using bisonc++ created\n"
        "                        by `build program' (optionally strip the\n"
        "                        executable)\n"
        "   install program <base> - install the program in the locations "
                                                        "defined\n"
        "                        in the INSTALL.cf file, optionally "
                                                            "below <base>\n"
        "   install man <base> - install the man pages in the location "
                                                        "defined\n"
        "                        in the INSTALL.cf file, optionally "
                                                            "below <base>\n"
        "   install manual <base> - install the manual in the location "
                                                        "defined\n"
        "                        in the INSTALL.cf file, optionally "
                                                            "below <base>\n"
        "   sourceforge        - create files for sourceforge index update\n"
        "\n"
        "If the environment variable DRYRUN is defined, no commands are\n"
        "actually executed\n"
        "\n"
    );
    exit(1);
}
