int g_components;

string testRequest(string request, string key, int value)
{
    int idx = strfind(request, key);    
    if (idx == -1)
        return request;

    int len = strlen(request);

    string ret;

    if (idx > 0)
        ret = substr(request, 0, idx);

    if (++idx < len)
        ret += substr(request, idx, len);
    g_components |= value;

    return ret;
}

void install(string request, string dest)
{
    string target;
    list pathsplit;
    string base;
    base = "tmp/install/";

    md(base);

    if (request == "x")
    {
        g_components = 63;
        request = "";
    }
    else 
    {
        request = testRequest(request, "a",  1);    // additional info
        request = testRequest(request, "b",  2);    // binary program
        request = testRequest(request, "d",  4);    // std documentation
        request = testRequest(request, "m",  8);    // man-pages
        request = testRequest(request, "s", 16);    // skeleton files
        request = testRequest(request, "u", 32);    // use guide
    }

    if (g_components == 0 || strlen(request) != 0)
    {
        printf("'build install selection ...': 'selection' must be x or "
                                                    "chars. of [abdsmu]\n");
        exit(1);
    }

    chdir(g_cwd);                       // determine the destination path
    if (dest == "")
        dest = "/";
    else
        md(dest);

    dest = cutEoln(backtick("readlink -f " + dest)[0]);

    if (g_logPath != "")
        exec(P_NOCHECK, "rm -f " + g_logPath);

    if (g_components & 1)
    {
        printf("\n  installing additional documentation\n");

        target = base + "a/" ADD + "/bison-docs/";
        printf("  installing original bison's docs below `", target + "'\n");
        logRecursive("documentation/html", target);

        target = base + "a/" ADD + "/demos/";
        printf("  installing examples below `", target + "'\n");
        logRecursive("documentation/demos", target);

        logRecursive("documentation/man/calculator", target + "/calculator/");

        printf("  installing regression tests below `", target + "'\n");
        logRecursive("documentation/regression", target + "/regression/");

        printf("  installing polymorphic semval demo at `", 
                                                        target + "poly/'\n");
        logRecursive("tmp/manual/poly", target + "poly/");

        printf("  installing polymorphic impl. demo at `", 
                                                    target + "/essence/'\n");
        logRecursive("tmp/manual/essence", target + "/essence/");

        destInstall(dest, base + "a");

        logInstalled(dest, "a");
    }

    if (g_components & 2)
    {
        target = base + "b/" BINARY;
        pathsplit = path_file(target);

            // copy srdir/src to destdir/dest, where destdir also contains 
            // the topic-icentifier (e.g., b in tmp/install/b)
        logFile("tmp/bin", "binary", pathsplit[0], pathsplit[1]);

        destInstall(dest, base + "b");

        logInstalled(dest, "b");
    }

    if (g_components & (4 | 8))
    {
        target = base + "d/" DOC "/";
        if (g_components & 4)
        {
            printf("  installing the changelog at `", target, "\n");
            logZip("", "changelog", target );

            destInstall(dest, base + "d");
        }

        if (g_components & 8)
        {
            printf("  installing the html-manual pages at `", target, "\n");
            logInstall("tmp/manhtml", "", target);

            destInstall(dest, base + "d");
        }

        logInstalled(dest, "d");
    }


    if (g_components & 8)
    {
        target = base + "m/" MAN;
        printf("  installing the manual pages below `", target, "'\n");
    
        target = base + "m/" MAN "/man1/";
        logZip("tmp/man", "bisonc++.1", target);
        destInstall(dest, base + "m");

        target = base + "m/" MAN "/man3/";
        logZip("tmp/man", "bisonc++api.3", target);
        destInstall(dest, base + "m");

        target = base + "m/" MAN "/man7/";
        logZip("tmp/man", "bisonc++input.7", target);
        destInstall(dest, base + "m");

        logInstalled(dest, "m");
    }

    if (g_components & 16)
    {
        target = base + "s/" SKEL "/";
        printf("  installing skeleton files at `" + target + "'\n");
        logInstall("skeletons", "", target);

        destInstall(dest, base + "s");

        logInstalled(dest, "s");
    }


    if (g_components & 32)
    {
        target = base + "u/" UGUIDE "/";
        printf("  installing the user-guide at `", target, "'\n");
        logInstall("tmp/manual", "", target);

        printf("  installing rpn sources at `", target + "/demos/'\n");
        logRecursive("tmp/manual/demos", target + "/demos/");

        destInstall(dest, base + "u");

        logInstalled(dest, "u");
    }

    printf("\n  Installation completed\n");

    exit(0);
}





