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

#define STD_CONVERSIONS     "html latex man txt xml"

string bin;
string inc;
string scripts;
string root;
string cwd;
string yodl;
string yodlpost;
string config;

void preset(string toYodlRoot)
{
    cwd     = chdir(".");
    root    =  chdir(toYodlRoot);

    bin =       root + "src/bin/";
    scripts =   root + "scripts/";

    config      = root  + "src/config.h";
    yodl        = bin   + "yodl";
    yodlpost    = bin   + "yodlpost";
    inc         = ".:" + root + "macros/yodl";

    chdir(cwd);
}

format(string fmt)
{
    exec(scripts + "configreplacements", config,
                                    "in/"   + fmt + ".in",
                                    "yodl/" + fmt + ".yo");
    
    chdir("rawmacros");
    system("./create " + fmt);
}

stdmacros(string series)
{
    list std;
    int idx;
    string conversion;

    std = strtok(series, " ");

    for (idx = 0; idx < sizeof(std); idx++)
    {
        conversion = element(idx, std);
        format(conversion);
        chdir(cwd);
    }
}
    
void cleanup()
{
    system("rm -f yodl/*.yo");
}

void version()
{
    system("echo \"DEFINESYMBOL\(XXyodlVersion\)\(@VERSION@\)\""
                                                        " > yodlversion.in");
    system(scripts + "configreplacements " + config + 
                                    " yodlversion.in yodl/yodlversion.yo");
    system("rm -f yodlversion.in");
}        

void install(string where)
{
    exec("mkdir", "-p", where);
    exec("cp", "-r", "yodl/*", where);
}

void main(int argc, list argv)
{
    string arg1;
    
    if (argc == 1)
    {
        printf("Build what ? Options are:\n"
                "   clean: cleanup\n"
                "   macros name: build the std.name.yo filec\n"
                "     std: build macros for all standard conversions:\n"
                "           html, latex, man, txt (and (experimental) xml)\n"
                "     version: update the  yodlversion.yo file to the\n"
                "                current Yodl version\n"
        );
        exit(1);
    }

    preset("..");

    arg1 = element(1, argv);

    if (arg1 == "macros")
        format(element(2, argv));
    else if (arg1 == "clean")
        cleanup();
    else if (arg1 == "install")
        install(element(2, argv));
    else if (arg1 == "std")
        stdmacros(STD_CONVERSIONS);
    else if (arg1 == "version")
        version();
    else
    {
        printf("request `", arg1, "' not yet available\n");
        exit(1);
    }
    exit(0);
}






