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

/*
    Sample Icmake script. This program deletes all files except for the
    ones mentioned on the command line. Uses "rm" and "rm -r" for the
    actual deletion of files.
    
    For the installation: see the sample file 'tolower'.
*/

#define VER "1.00"

int
    recursive;					// remove subdirs too?
    
void checkfiles (list files)			// check that the files
{						// are present
    int
    	i;					// loop counter
    string
    	file;					// one file from list
    	
    for (i = 0; i < sizeof (files); i++)
    {
    	file = element (i, files);		// get 1 filename
    	stat (file);				// stat file.. if this fails,
    }						// Icmake will exit
}

void keep (list files)				// remove all but the 
{						// files
    int
    	i;					// loop counter
    list
    	dir;					// dir listing
    string
    	recursive_flag;				// recursive flag for 
    						// "rm"
    						
    if (recursive)				// set recursive flag
    	recursive_flag = "-r";			// if needed
    	
    checkfiles (files);				// make sure files are
    						// there
    if (recursive)
    	dir = makelist (O_ALL, "*");		// make dir listing
    else					//  with subdirs if 
    	dir = makelist ("*");			//  recursive processing
    	
    dir -= files;				// scratch files to keep
    
    for (i = 0; i < sizeof (dir); i++)		// for each file in dir:
    	exec ("rm", recursive_flag, 		// remove it
    	      element (i, dir));
}

void main (int argc, list argv)
{
    if (element (1, argv) == "-r")		// check first flag
    {
    	recursive++;
    	argv -= (list) element (1, argv);
    }
    
    if (! element (1, argv))			// print usage if no args
    {
    	printf ("\n"
    		"ICCE Directory Cleaner  V", VER, "\n"
    		"Copyright (c) ICCE, 1993. All rights reserved.\n"
    		"\n"
    		"Usage: keep [-r] file(s)\n"
    		"where:\n"
    		"      -r      - optional flag; specifies recursive"
    							" processing\n"
    		"      file(s) - files or subdirectories to keep, others"
					    		" are deleted\n"
		"\n");
	exit (1);
    }
    
    argv -= (list) element (0, argv);		// remove makefile from args
    keep (argv);				// and.. do it to it
    
    exit (0);
}
