#!./bin/icmake -qi

#define VER		"1.03"

/*
        NOTE: This is an icmake-script.
	
        For your convenience, I have included de icmake binaries
	in the dim-subdirectory, hence the line

                        #!./bin/icmake -qi

        icmake is not a part of dim, but it allows you to create dim from 
	the sources. If you already have icmake, or if you want to have
	icmake in another directory, the icm* files in the bin subdirectory
	can be deleted or moved. If so, change the './bin/' path specification
	in the first line of this file to the path where you keep icmake. 
	E.g.,           #!/usr/local/bin/icmake -qi
	
	If you're interested in obtaining icmake: it's at
            - tsx-11.mit.edu in the linux-subdirectory sources/usr.bin,
	    - sunsite.unc.edu in the linux-subdirectory devel,
            - beatrix.icce.rug.nl in the directory /pub/unix
	    
        NOTE that the icmake binaries in the bin subdirectory were
	last compiled with the 4.5.8 shared libraries. Icmake will
	complain about the version if you use an older one, but will
	do its job normally.
*/	



/*      ----------------------------------------------------------
        Programs used. If you don't have them in your path,
	alter the program names such that they can be found
*/	
#define AR		"ar"
#define    ARREPLACE	"rvs"
#define CC		"gcc"
#define    CFLAGS	"-c -Wall"
#define CHMOD           "chmod"
#define CP              "cp"
#define RM              "rm"
#define STRIP		"strip"

/*      ---------------------------------------------------------
        Paths used. If you don't want these, alter them
*/

#define CONFDIM     	"/conf/dim"
#define DIMDEST		"/usr/local/bin"


//================================================================
//      The remainder is the icmake-script, doing its thing.
//      Don't alter what follows, unless you know what you're doing

int compdir ()
{
    int
        ret,
    	i;
    list
    	ofiles,
    	cfiles;
    string
    	hfile,
    	cfile,
    	ofile,
    	libfile;

    libfile = "libdim.a";                       // set some filenames
    hfile = "dim.h";
    
    chdir("src");                              // go to source-dir

    if (hfile younger libfile)                  // hfile changed: make all
    	cfiles = makelist ("*.c");
    else                                        // recompile changed c-files.
    	cfiles = makelist ("*.c", younger, libfile);
    
    for (i = 0; i < sizeof (cfiles); i++)       // do the compilation
    {
    	cfile = element (i, cfiles);
    	ofile = change_ext (cfile, ".o");
    	
    	if (cfile younger ofile)
    	    exec (CC, CFLAGS, cfile);
    }
    
    if (ofiles = makelist("*.o"))               // put .o files in the lib.
    {
    	exec(AR, ARREPLACE, libfile, "*.o");
    	exec("rm", "*.o");
    	ret = 1;                                // relink required
    }
    
    chdir("..");                                // back to org dir.
    
    ret |= "bin/dim" older "src/" + libfile;   // library ok, no prog yet

    return (ret);                               // if ret == 0, no relink
}

void linkprog ()
{
    chdir ("src");
    exec (CC, "-o", "../bin/dim", "-ldim", "-L.");
    chdir ("..");
}

void buildprog ()
{
    int
    	dim;
    	
    if (compdir())
    	linkprog ();
    printf("bin/dim is up-to-date\n");
}

void instprog (string destdir)
{
    exec (STRIP, "bin/dim");
    exec (CHMOD, "700", "bin/dim", CONFDIM);    // rwx for owner of dim
                                                // including getty-files dir.
    exec (CP, "bin/dim", destdir);
}

void install ()
{
    buildprog ();
    instprog (DIMDEST);
    printf
    (
        "Make sure getty.ttyS#.[org dis] are copied to ", CONFDIM, "\n"
	"YOU HAVE TO DO THAT YOURSELF !\n"
    );
}

void clean ()
{
    chdir ("src");
    exec ("rm", "-f", "*.o lib*.a");
    chdir ("..");
    exec ("rm", "-f", "build.bim");
}

int main (int argc, list argv)
{
    if (element (1, argv) == "dim")             // build the programs
    	buildprog ();
    else if (element (1, argv) == "install")
    	install ();                             // install the programs
    else if (element (1, argv) == "clean")
    	clean ();                               // cleanup garbage
    else
    {
        printf ("\n"
            "Usage: build dim       - builds dim\n"
      	    "       build install   - installs dim (if you're allowed to)\n"
            "       build clean     - cleanup .o files etc.\n"
            "\n");
        return (1);
    }
    return (0);
}
