#!/usr/local/bin/icmake -qt /tmp/build

/***************************************************************************
**			State University of Groningen
**				    ICCE
**
**			       Frank B. Brokken
**
**	Copyright (c) State University of Groningen, ICCE, the Netherlands.
** 
**			This file is part of callback.
**
**
****************************************************************************


Callback was originally developed by Karel Kubat (karel@icce.rug.nl)

Callback version 2.00 was developed by Frank B. Brokken (frank@icce.rug.nl)
who is, as of February 1995, the maintainer of the callback software.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
ICCE OR THE STATE UNIVERSITY OF GRONINGEN BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.

Except as contained in this notice, the name of the ICCE shall not be used in
advertising or otherwise to promote the sale, use or other dealings in this
Software without prior written authorization from the ICCE.

*/

#include "configure.h"
#include "versions.h"

#define DEBUG		"-O2"

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

    curdir = chdir (".");
    
    libfile = "lib" + dir + ".a";   
    hfile = dir + ".h";
    
    printf("Entering src/", dir, "\n");
    chdir (dir);

    if (hfile younger libfile)
    	cfiles = makelist ("*.c");
    else
    	cfiles = makelist ("*.c", younger, libfile);
    
    for (i = 0; i < sizeof (cfiles); i++)
    {
    	cfile = element (i, cfiles);
    	ofile = change_ext (cfile, ".o");
    	
    	if (! exists (ofile) || ofile older cfile)
    	    exec (CC, DEBUG, CFLAGS, cfile);
    }
    
    if (ofiles = makelist ("*.o"))
    {
    	exec (AR, ARREPLACE, libfile, "*.o");
    	exec ("rm", "*.o");
    	ret = 1;
    }
    
    printf("Returning to ", curdir, "\n");
    chdir (curdir);
    
    return (ret);
}

void linkprog (string dir)
{
    chdir (dir);
    exec (CC, DEBUG, "-o", dir, "-l" + dir, "-lrss", "-L. -L../rss");
    chdir ("..");
}

void checkversion(string target)
{
    if ("versions.h" younger target)
	system("touch " + target);	// touch in case of version change
}

void buildprogs ()
{
    int
    	cblogin,
    	cbstat,
	cbgetty,
    	rss;

    if ("configure.h" younger"src/rss/rss.h")
	system("touch src/rss/rss.h");

    checkversion("src/cb/usage.c");
    checkversion("src/cblogin/banner.c");
    checkversion("src/cbgetty/usage.c");
    checkversion("src/cb/cb.c");
    checkversion("src/cblogin/cblogin.c");
    checkversion("src/cbgetty/cbgetty.c");
    checkversion("src/rss/progversion.c");

    chdir ("src");
    
    cblogin = compdir ("cblogin");
    cbstat  = compdir ("cb");
    cbgetty = compdir ("cbgetty");
    rss     = compdir ("rss");


    if (cblogin || rss)
    	linkprog ("cblogin");


    if (cbstat || rss)
    	linkprog ("cb");


    if (cbgetty || rss)
	linkprog ("cbgetty");
    	
    chdir ("..");
}

void instprog (string prog, string destdir)
{
    chdir ("src/" + prog);
    exec (STRIP, prog);
    exec ("chmod", "700", prog);
    exec ("cp", prog, destdir);
    chdir ("../..");
}

void install (string mode)
{
    if (mode != "dial-in" && mode != "callback")
    {
	printf("Installation mode required with 'install':\n"
		"Use either 'build install callback' or 'build install "
							"dial-in'.\n"
		"\n"
	);
	exit (1);
    }

    buildprogs ();


    instprog ("cb",  CALLBACKDIR);

    if (mode == "callback")
    {
	instprog ("cblogin", CALLBACKDIR);
	instprog ("cbgetty", CALLBACKDIR);
    }
    printf("\n"
	"\n"
	"********************************************************\n"
	"* Make sure to have/create a link to " CALLBACKDIR "/cb\n"
	"********************************************************\n"
	"\a\n"
	);
}

void cleandir (string dir)
{
    chdir ("src/" + dir);
    exec ("rm", "-f", "*.o lib*.a", dir);
    chdir ("../..");
}

void clean ()
{
    exec ("rm", "-f", "build.bim");
    cleandir ("cblogin");
    cleandir ("cb");
    cleandir ("cbgetty");
    cleandir ("rss");
}

void makedist ()
{
    list
    	examples;
    int
    	i;

    buildprogs ();
    
    exec ("cp",
	  "src/cblogin/cblogin", "src/cb/cb", "src/cbgetty/cbgetty",
	  "bin");
    exec ("cp", "/usr/local/bin/icm*", "icmake-bin");

    clean ();
    
    chdir ("..");
    exec ("tar", "cvzf", "callback-" + VERSION + ".tar.gz", "callback");
    chdir ("callback");
}

void main (int argc, list argv)
{
    if (element (1, argv) == "progs")
    	buildprogs ();
    else if (element (1, argv) == "install")
    	install (element(2, argv));
    else if (element (1, argv) == "clean")
    	clean ();
    else if (element (1, argv) == "dist")
    	makedist ();
    else
    {
        printf ("\n"
                "Usage: build progs            - builds programs\n"
        	"       build install callback - installs programs for mode "
						    "callback\n"
        	"       build install dial-in  - installs programs for mode "
						    "dial-in\n"
        	"       build clean            - cleanup .o files etc.\n"
        	"\n"
        	"       build dist             - makes .tar.gz distribution "
								    "file.\n"
        	"\n");
        exit (1);
    }
    
    exit (0);
}
