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

#include "VERSION" 
#include "INSTALL.im"

// Use the next one to see what parser/p_parse() does
//#define COPT                "-O2 -Wall -DEBUG"
#define COPT                "-O2 -Wall -fdiagnostics-color=never "
                                // -Werror"

#define STD_CONVERSIONS     "html latex man txt xml"
#define ECHO_REQUEST        1

string  CLASSES;
string  g_lopt;
string  g_copt;
string  g_cwd;
string  g_wip =     "tmp/wip";          // no slash here
string  g_install = "tmp/install";      // no slash here

int     g_compiled;
int     g_nClasses;
list    g_classes;

#include "icmake/run"
#include "icmake/md"
#include "icmake/clean"
#include "icmake/stdcpp"
#include "icmake/stdcompile"
#include "icmake/compilerss"
#include "icmake/builtins"
#include "icmake/program"
#include "icmake/buildmacros"
#include "icmake/manualmacrolist"
#include "icmake/macros"
#include "icmake/man"
#include "icmake/manual"
#include "icmake/install"
#include "icmake/github"

string setOpt(string install_im, string envvar)
{
    list optvar;
    string ret;

    optvar = getenv(envvar);    

    if (optvar[0] == "1")
        ret = optvar[1];
    else 
        ret = install_im;

    return ret;
}

void main(int argc, list argv)
{
    string arg1;

    echo(ECHO_REQUEST);
    g_cwd = chdir(".");

    g_copt = setOpt(COPT + " -g", "CFLAGS");

#ifdef PROFILING
    g_copt = COPT + " -pg";
    g_lopt = "-pg";
#endif

    g_lopt = setOpt(g_lopt, "LDFLAGS");

#ifdef EXTENSIVE_OPTIONS    
    g_copt = "-O0 -g3 -ansi -pedantic -fno-common -pipe -W -Wall -Wcast-align"
        " -Wcast-qual -Wconversion -Wformat=2 -Winline -Wnested-externs"
        " -Wpointer-arith -Wshadow -Wstrict-prototypes -Wundef "
        "-Wno-unused-parameter -Waggregate-return -Wnested-externs";
#endif

    setLocations();

    if (
        STD_INCLUDE[0] != "/" || BIN[0] != "/"  || MAN[0] != "/"  || 
        DOC[0] != "/"  || DOCDOC[0] != "/" )
    {
        printf("Check INSTALL.im: BIN, DOC, DOCDOC, MAN, STD_INCLUDE, "
                                                "must be absolute paths\n");
        exit(1);
    }

    arg1 = argv[1];

    if (arg1 == "clean" || arg1 == "distclean")
        cleanupExit();

    md(g_install + " " + g_wip);

//    g_include = " -I.:" + g_install + STD_INCLUDE + ":" + g_wip;

    echo(OFF);
    run("rm -f " + g_wip + "/release.yo " + g_wip + "/config.h");
    echo(ON);

    fprintf(g_wip + "/release.yo", "SUBST(_CurVers_)(", VERSION, ")\n",
                              "SUBST(_CurYrs_)(", YEARS, ")\n",
                              "manttquoted(1)\n");

    fprintf(g_wip + "/config.h",
        "#define STD_INCLUDE    \"",    STD_INCLUDE,   "\"\n"
        "#define VERSION        \""     VERSION "\"\n"
        "#define YEARS          \""     YEARS   "\"\n"
        "#define YODL_BIN       \"",    BIN,    "\"\n"
    );

    if (arg1 == "programs")
        programsExit(argv[2]);

    if 
    (
        arg1 == "yodl" || arg1 == "yodlpost" || arg1 == "yodlverbinsert" ||
        arg1 == "yodlstriproff" || arg1 == "yodl2whatever" || arg1 == "programs"
    )
        programExit(arg1, argv[2]);

    if (arg1 == "man")
        manExit(argv[2]);

    if (arg1 == "macros")
        macrosExit();

    if (arg1 == "manual")
        manualExit("", argv[2]);

    if (arg1 == "html")
        manualExit("html", argv[2]);

    if (arg1 == "latex")
        manualExit("latex", argv[2]);

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

    if (arg1 == "github")
        github();

    printf("Usage: build action\n"
        "Where `action' is one of:\n"
        "   (dist)clean: clean up\n"
        "   programs [strip]:       build the programs\n"
        "   yodl     [strip]:       only build `yodl'\n"
        "   yodlpost [strip]:       only build `yodlpost'\n"
        "   yodlverbinsert [strip]: only build `yodlverbinsert'\n"
        "   yodlstriproff:          only build `yodlstriproff\n"
        "   yodl2whatever:          only build `yodl2whatever\n"
        "   macros:         (re)build the standard yodl macros\n"
        "                   run `build programs' first, run `build macros'\n"
        "                   before any of the following document-creating "
                                                                " commands\n"
        "   man    [path]:  (re)build the man-pages, optionally specify: "
                                                                    "path\n"
        "   manual [path]:  (re)build the manual, optionally specify: path\n"
        "   html [path]:    (re)build the html version of the manual\n"
        "                       (after 'manual' or 'macros')\n"
        "   latex [path]:   (re)build the latex version of the manual\n"
        "                       (after 'manual' or 'macros')\n"
        "   install programs WHERE:       install the programs under WHERE\n"
        "   install yodl WHERE:           install yodl under WHERE\n"
        "   install yodlpost WHERE:       install yodlpost under WHERE\n"
        "   install yodlverbinsert WHERE: install yodlverbinsert under WHERE\n"
        "   install yodlstriproff WHERE:  install yodlstriproff under WHERE\n"
        "   install yodl2whatever WHERE:  install yodl2whatever c.s. under "
                                                                    "WHERE\n" 
        "   install macros WHERE:         install the macros under WHERE\n"
        "   install man WHERE:            install the man-pages under WHERE\n"
        "   install manual WHERE:         install the manual under WHERE\n"
        "   install docs WHERE:           install additional docs under "
                                                                    "WHERE\n"
        "   github               - prepare github's gh-pages update\n"
        "                          (internal use only)\n"
        "`strip': the program(s) will be stripped,\n"
        "`path':  yodl is located in $PATH (otherwise just-built programs "
                                                                "are used).\n"
    );
    exit(0);
}
