string setWhere(string where, string defaultWhere)
{
    if (where == "")
        where = defaultWhere;

    md(where);
    return where;
}

void install(string what, string where)
{
    list files;                             
    int idx;
    string base;
    string dest;

    if (what == "program")
    {
        if (where == "")
            where = BINARY;

                            // 'where' exists, but is no regular file
        if (exists(where) && ((int)stat(where)[0] & S_IFREG) == 0)
        {
                printf("'build install program path': path must be a "
                        "regular file\n");
                exit(0);
        }

        md(get_path(where));

        printf("  INSTALLING the executable `", where, "'\n");
        run("icmbuild install program " + where);

        log(where);

        writeLog();     // exits
    }

    if (what == "skel")
    {
        where = setWhere(where, SKEL);

        printf("  INSTALLING skeleton files at `" + where + "'\n");

        run("cp skeletons/* " + where);
        logFiles("skeletons", where);

        writeLog();
    }

    if (what == "man")
    {
        where = setWhere(where, MAN);

        printf("  INSTALLING the manual page in `", where, "'\n");
        dest = where + "/" PROGRAM ".1.gz";

        run("gzip -9 -n < tmp/man/" PROGRAM ".1 > " + dest);
        log(dest);

        writeLog();
    }

    if (what == "manual")
    {
        base = setWhere(where, MANUAL);

        logDirs("tmp/manual", base);
        printf("  INSTALLING the manual below `", base, "'\n");
        run("cp -r tmp/manual/* " + base);
        logFiles("tmp/manual", base);

        writeLog();
    }

    if (what == "std")
    {
        base = setWhere(where, STD);

        printf("  INSTALLING the changelog at `", base, "\n");
        dest = base + "/changelog.gz";
        run("gzip -9 -n < changelog > " + dest);
        log(dest);
    
        printf("  INSTALLING the html-manual page at `", base, "\n");
        run("cp tmp/manhtml/" PROGRAM "man.html " + base);
        log(base + "/" PROGRAM "man.html");

        writeLog();
    }

    if (what == "extra")
    {
        where = setWhere(where, EXTRA);

        base = where + "/bison-docs";

        printf("  INSTALLING original bison's docs below `", base + "'\n");

        logDirs("documentation/html", base + "/html");
        run("cp -r documentation/html " + base);
        logFiles("documentation/html", base + "/html");

        where += "/examples";

        printf("  INSTALLING examples below `", where + "'\n");
        logDirs("documentation/examples", where);
        run("cp -r documentation/examples/* " + where);
        logFiles("documentation/examples", where);

        base = where + "/calculator";
        md(base);
        logDirs("documentation/man/calculator", base);
        run("cp -r documentation/man/calculator/* " + base);
        logFiles("documentation/man/calculator", base);

      base = where + "/regression";
        printf("  INSTALLING regression tests below `", base + "'\n");
        md(base);
        logDirs("documentation/regression", base);
        run("cp -r documentation/regression/* " + base);
        logFiles("documentation/regression", base);

        writeLog();
    }

    exit(0);
}




