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

#include "INSTALL.im"

#include "icmake/run"
#include "icmake/md"
#include "icmake/getenv"
#include "icmake/special"
#include "icmake/clean"
#include "icmake/manpage"
#include "icmake/manual"
#include "icmake/library"
#include "icmake/program"
#include "icmake/rebuild"
#include "icmake/install"

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

    g_env = envp;

    setLocations();     // from INSTALL.im

    my_getenv("DRYRUN");
    g_dryrun = g_envvar;

    option = element(1, argv);
    if (element(2, argv) == "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")
        library(1);

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

    if (option == "parser")
    {
        chdir("parser");
        run("bisonc++ grammar");
        exit(0);
    }

    if (option == "rebuild")
        rebuild(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"
        "   parser             - use bisonc++ to rewrite parse.cc and\n"
        "                        parserbase.h\n"
        "   program [strip]    - build bisonc++ (optionally strip the\n"
        "                        executable)\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);
}
