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

void buildIn(string where, string what, string arg)
{
    string cwd;

    cwd = chdir(".");
    chdir(where);
    exec("./build", what, arg);
    chdir(cwd);
}

void cleanup()
{
    system("rm -rf *stamp debian/yodl debian/*substvars*");

    buildIn("src",      "clean", "");
    buildIn("macros",   "clean", "");
    buildIn("manual",   "clean", "");
    buildIn("man",      "clean", "");
}

void package()
{
    buildIn("src",      "progs", "");   // first, create yodl and yodl-post

    buildIn("macros",   "version", ""); // then create the version file
    buildIn("macros",   "std", "");     // and the std conversions
    
    buildIn("manual",   "docmacros", "");// macrolist for the manual
    buildIn("manual",   "manual", "");  // create the manual

    buildIn("man",      "man", "");     // create the manpages
}

void install(string where)
{
    system("mkdir -p " + where);

    buildIn("src",      "install",  where + "/bin");
    buildIn("macros",   "install",  where + "/share/yodl");
    buildIn("man",      "install",  where + "/share/man");
    buildIn("manual",   "install",  where + "/share/doc");

    exec("cp", "-r", 
            "AUTHORS.txt changelog contrib CHANGES PATCHES.txt README.txt", 
            where + "/share/doc/yodl");
}

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

    if (argc == 1)
    {
        printf
        (
            "\n"
            "Build what? Options are:\n"
            "   clean: clean up\n"
            "   install WHERE: install the products under WHERE "
                                                "(following `build package')\n"
            "                  The Debian installation process will install "
                                                        "under debian/tmp\n"
            "   package: build all parts of the package (after `clean')\n"
            "\n"
        );
        exit(1);
    }

    arg1 = element(1, argv);

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