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

#define VER             "1.00"

string 
    sizeflag;
    
void getsizeflag (string av0)
{
    string
        inf;
    list
        input;
	
    inf = change_base (av0, "size");
    inf = change_ext  (inf, "");
    
    if (input = fgets (inf, 0))
        sizeflag = "-s " + element (0, strtok (element (0, input), " \t\n"));
}

void main (int argc, list argv)
{
    string
        listfile,
	outputfile;

    echo (0);
    
    if (argc != 3)
    {
        printf ("\n"
	        "ICCE Backup Maker  V" VER "\n"
		"Copyright (c) ICCE 1994. All rights reserved.\n"
		"\n"
		"makebackup by Karel Kubat.\n"
		"\n"
		"Usage: makebackup listfile outputfile\n"
		"where:\n"
                "       listfile   : file containing the list of files "
		                                        "to backup\n"
                "                    (see the buildlist program)\n"
                "       outputfile : the file to receive the afio'd backup\n"
		"\n"
                "The files listed in the listfile are written to the "
		                                        "outputfile.\n"
		"\n");
        exit (1);
    }

    getsizeflag (element (0, argv));
	
    listfile   = element (1, argv);
    outputfile = element (2, argv);
    
    if (! exists (listfile))
    {
        printf ("makebackup: list file ", listfile, "not found\n");
	exit (1);
    }
    
    if ( (int) element (1, stat (listfile)) == 0 )
    {
        printf ("makebackup: empty list file ", listfile, "\n"
	        "If you are making an incremental backup, maybe there's "
		                                        "no need?\n");
        exec ("rm", "-f", listfile);
	exit (1);
    }
    
    if (exec (P_NOCHECK, "awk", "'{print $1}'", listfile, 
                "|", "afio", sizeflag, "-Z -v -o", outputfile)
       )
    {
        printf ("The backup shows some fault!\n"
	        "Correct the problem and retry. List file deleted.\n");
        exec ("rm", "-f", listfile);
	exit (1);
    }
    
    exit (0);
}
