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

// script generated by the C++ icmake script version 2.33

/*
Configurable defines:

    CLASSES:        string of directory-names under which sources of classes
                    are found. E.g., CLASSES  = "class1 class2"

    All class-names must be stored in one string.
    If classes are removed from the CLASSES definition or if the names in the
    CLASSES definition are reordered, the compilation should start again from
    scratch.  
*/

// #include "../def/destinations"

string
    CLASSES;

void setClasses()
{
//  ADD ADDITIONAL DIRECTORIES CONTAINING SOURCES OF CLASSES HERE
//  Use the construction `CLASSES += "classname1 classname2";' etc.

    CLASSES += "";
}

#define COMPILER           "gcc"
#define COPT               "-Wall -O2 -DHAVE_GLOB -g"

#define ECHO_REQUEST       1

//                      NO CONFIGURABLE PARTS BELOW THIS LINE

/*
                            V A R S . I M
*/

string                  // contain options for
    cwd,                // current WD
    libs,               // extra libs, e.g., "-lrss -licce"
    libpath,            // extra lib-paths, eg, "-L../rss"
    copt,               // Compiler options
    lopt,               // Linker options
    libxxx,             // full library-path
    ofiles,             // wildcards for o-files
    sources,            // sources to be used
    current,            // contains name of current dir.
    programname;        // the name of the program to create
int
    nClasses,           // number of classes/subdirectories
    program;            // 1: program is built
list
    classes;            // list of classes/directories
/*
                                 parser.im
*/


/*
                                I N I T I A L . I M
*/
void initialize()
{
    echo(ECHO_REQUEST);
    sources = "*.c";
    ofiles = "o/*.o";                       // std set of o-files
    copt = COPT;

    cwd = chdir(".");
}


/*
                                O B J F I L E S . I M
*/

list objfiles(list files)
{
    string
        file,
        objfile;
    int
        i;

    for (i = 0; i < sizeof(files); i++)
    {
        file = element(i, files);           // determine element of the list
        objfile = "./o/" + change_ext(file, "o");    // make obj-filename
        if (objfile younger file)           // objfile is younger
        {
            files -= (list)file;            // remove the file from the list
            i--;                            // reduce i to test the next
        }
    }
    return (files);
}
/*
                                A L T E R E D . I M
*/

list altered(list files, string target)
{
    int
        i;
    string
        file;

    for (i = 0; i < sizeof(files); i++)     // try all elements of the list
    {
        file = element(i, files);           // use element i of the list
            
        if (file older target)              // a file is older than the target
        {
            files -= (list)file;            // remove the file from the list
            i--;                            // reduce i to inspect the next
        }                                   // file of the list
    }
    return (files);                         // return the new list
}
/*
                            F I L E L I S T . I M
*/

list file_list(string type, string library)
{
    list
        files;

    files = makelist(type);                 // make all files of certain type
    files = altered(files, library);    // keep all files newer than lib.
    files = objfiles(files);                // remove if younger .obj exist

    return (files);
}
/*
                        L I N K . I M
*/

void link(string library)
{
    printf("\n");
    exec(COMPILER, "-o", programname,
            "-l" + library, libs, "-L.", libpath, lopt, "-s");
    printf("ok: ", programname, "\n");
}
/*
                          P R E F I X C L . I M
*/
void prefix_class(string class_id)
{
    list
        o_files;
    string
        o_file;
    int
        i;

    chdir("o");
    o_files = makelist("*.o");
    for (i = 0; o_file = element(i, o_files); i++)
        exec("mv", o_file, class_id + o_file);
    chdir("..");
}
/*
                          R M C L A S S P . I M
*/

string rm_class_id(string class_id, string ofile)
{
    string
        ret;
    int
        index,
        n;

    n = strlen(ofile);
    for (index = strlen(class_id); index < n; index++)
        ret += element(index, ofile);

    return ret;
}


void rm_class_prefix(string class_id)
{
    list
        o_files;
    string
        o_file;
    int
        i;

    chdir("o");
    o_files = makelist("*.o");
    for (i = 0; o_file = element(i, o_files); i++)
        exec("mv", o_file, rm_class_id(class_id, o_file));
    chdir("..");
}
/*
                            C C O M P I L E . I M
*/

void c_compile(list cfiles)
{
        string
                nextfile;
        int
                i;
                
    if (!exists("o"))
        system("mkdir o");
                                                      
    if (sizeof(cfiles))                 // files to compile ?
    {
        printf("\ncompiling: ", current, "\n\n");
        
                                        // compile all files separately
        for (i = 0; nextfile = element(i, cfiles); i++)
            exec(COMPILER,
                "-c -o o/" + change_ext(nextfile, "o"),
                copt, nextfile);

        printf("\n");
    }
    printf("ok: ", current, "\n");
}
/*
                            U P D A T E L I . I M
*/

void updatelib(string library)
{
    list
        arlist,
        objlist;
    string
        to,
        from;

    objlist = makelist("o/*.o");

    if (!sizeof(objlist))
        return;

    printf("\n");

    exec("ar", "rvs", library, "o/*.o");
    exec("rm", "o/*.o");

    printf("\n");
}

/*
                                S T D C P P . I M
*/

void std_cpp(string library)
{
    list
        cfiles;

    cfiles = file_list(sources, library);     // make list of all cpp-files

    c_compile(cfiles);                      // compile cpp-files
}
/*
                                C P P M A K E . C

    CPP files are processed by stdmake.

    Arguments of CPPMAKE:

    cpp_make(
        string mainfile,    : name of the main .cpp file, or "" for library
                              maintenance
        string library,     : name of the local library to use/create
                                (without lib prefix or .a/.so suffix
                                (E.g., use `main' for `libmain.a')
        )

    Both mainfile and library MUST be in the current directory
*/

void cpp_make(string mainfile, string library)
{
    int
        index;
    string class;
        
    if (nClasses)
        ofiles += " */o/*.o";               // set ofiles for no LIBRARY use

                                            // make library name
    libxxx = "../tmp/lib" + library + ".a";
                                            // first process all classes
    for (index = 0; index < nClasses; index++)
    {
        class = element(index, classes);  // next class to process
        chdir(class);                     // change to directory

        current = "subdir " + class;
        std_cpp(libxxx);                    // compile all files
        chdir(cwd);                         // go back to parent dir
    }

    current = "auxiliary " + sources + " files";
    std_cpp(libxxx);                        // compile all files in current dir

    
                                        // prefix class-number for .o files
        for (index = 0; index < nClasses; index++)
        {
            current = element(index, classes);  // determine class name
            chdir( current);              // chdir to a class directory.
            prefix_class((string)index);  
            updatelib(libxxx);
            chdir(cwd);                // go back to parent dir
        }
        current = "";                  // no class anymore
        updatelib(libxxx);             // update lib in current dir
}

void main(int argc, list argv)
{
    int extensive;
    string version;
    string versionFile;
    list line;
    list cut;
    string key;
    string value;
    string cwd;
    string versionh = "../tmp/version.h";
    versionFile = "../VERSION";


    if (argc == 1)
    {
        printf("Usage:\n"
               "   build lib [-e]\n");
        exit (1);
    }

    cwd = chdir("");
    
    chdir("..");
    system("./icm_prepare");
    chdir(cwd);

//    system("mkdir -p ../tmp/");

    echo(OFF);
    system("rm -f " + versionh);
    echo(ON);

    while (sizeof(line = fgets(versionFile, (int)line[1])))
    {
        cut = strtok(line[0], "= \t\n");
        key = cut[0];
        value = cut[1];

        if (key == "VERSION")
            fprintf(versionh, "#define VERSION \"", value, "\"\n");
        else if (key == "YEARS")
            fprintf(versionh, "#define YEARS \"", value, "\"\n");
    }

    if (extensive = (element(argc - 1, argv) == "-e"))
        --argc;

    initialize();

    if (extensive)
        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"
        " -DHAVE_GLOB";

    if (element(1, argv) == "lib")
    {
        program = 0;
        cpp_make
        (
            "",
            "icrss"  // static- or so-library
        );
    }

    system("rm -rf o");
    exit (0);
}
