#!/usr/local/bin/icmake -qi

/* 
    This simple icmake script starts a given command in the current
    directory, and then recursively in all subdirectories. For the
    installation: see the sample script "tolower" (or, "tolower.im").
*/

#define VER "1.02"

string makecmd (list cmd)			// make one long cmd by
{						// expanding list elements
    string
    	ret;					// returned cmd
    int
    	i,					// outer/inner loop
    	j;					// counters
    list
    	aux;					// expanded inner list
    	
    ret = element (0, cmd);			// add program name itself
    
    for (i = 1; i < sizeof (cmd); i++)		// for all other elements:
    {
    	if (aux = makelist (element (i, cmd)))	// expand element, and
    	    for (j = 0; j < sizeof (aux); j++)	// add expansion
    	    	ret += " " + element (j, aux);
        else
            ret += " " + element (i, cmd);	// or add unexpanded
    }
    
    return (ret);				// return the string
}

void process (list cmd)
{
    list
    	dirs;					// list of subdirs
    int
    	i;					// counter for subdirs or
    string					// command name list
    	cwd,					// stored current working dir
    	sys;					// expanded command to run
    						
    cwd = chdir (".");				// get cwd
    printf ("==== r: directory ", cwd, 		// print this dir
        " ====\n");
    
    sys = makecmd (cmd);			// make and the command
    system (P_NOCHECK, sys);			// run the command
    
    if (dirs = makelist (O_SUBDIR, "*"))	// get list of subdirs
    {
	for (i = 0; i < sizeof (dirs); i++)	// for each one:
	{
	    chdir (element (i, dirs));		// go there
	    process (cmd);			// recursively run cmd
	    chdir (cwd);			// and.. back again
	}    
    }    
}

void main (int argc, list argv)
{
    echo (0);					// suppress re-echoing

    if (argc == 1)				// usage info if no
    {						// cmdline arguments
    	printf ("ICCE Recursive Command-expander  Version ", VER, "\n"
    		"Copyright (c) ICCE 1993. All rights reserved.\n"
    		"\n"
    		"Usage: r program arguments\n"
    		"Will run \"program arguments\" in this directory and"
    					" recursively in the\n"
    		"subdirectories.\n"
    		"\n");
    	exit (1);
    }
    
    argv -= (list) element (0, argv);		// remove makefile name
    
    process (argv);				// and.. start at current
    						// dir
    						
    exit (0);					// done.
}
