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

#define VER             "1.00"

string 
    home;

void gethome (list envp)
{
    int
        i;
    for (i = 0; i < sizeof (envp); i += 2)
        if (element (i, envp) == "BACKUP_HOME")
	{
	    home = element (i + 1, envp);
	    return;
	}
	
    printf ("runbackup: Environment variable BACKUP_HOME undefined!\n"
            "Either define that variable or use the backup script.\n");
    exit (1);	    
}

void main (int argc, list argv, list envp)
{
    string
        fullstampfile,
	incrementalstampfile,
	volume,
	volume_startdir,
	volume_exclude,
	output,
	fulllistfile,
	inclistfile;
    int
        incremental;
	
    echo (0);
	
    if (argc < 3 || argc > 4)
    {
        printf ("\n"
	        "ICCE Backup Maker  V" VER "\n"
	        "Copyright (c) ICCE 1994. All rights reserved.\n"
		"\n"
		"runbackup by Karel Kubat.\n"
		"\n"
		"Usage: runbackup [-incremental] volume outputfile\n"
		"where:\n"
                "       -incremental : optional flag, an incremental "
		                                        "backup is made\n"
                "                      (default: full backup)\n"
                "       volume       : volume to backup, must match one of "
		                                        "the definitions\n"
                "                      in volumes/\n"
		"       outputfile   : archive to receive backup\n"
                "\n");
        exit (1);
    }
    
    gethome (envp);
    
    if (element (1, argv) == "-incremental")
    {
        incremental++;
	argv -= (list) element (1, argv);
    }
    volume = element (1, argv);
    output = element (2, argv);
    
    volume_startdir = home + "/volumes/" + volume + ".startdir";
    volume_exclude  = home + "/volumes/" + volume + ".exclude";
    
    if (! exists (volume_startdir) || ! exists (volume_exclude))
    {
        printf ("runbackup: can't find volume definition file(s)\n"
	        "(", volume_startdir, " or ", volume_exclude, ")\n");
	exit (1);
    }
    
    fullstampfile        = home + "/stamps/" + volume + ".full";
    incrementalstampfile = change_ext (fullstampfile, ".incremental");
    
    if (incremental)
    {
	if (! exists (fullstampfile))
	{
	    printf ("runbackup: can't find stampfile for incremental backup\n"
	            "(", fullstampfile, ")\n"
		    "You can't make an incremental backup when no full "
                		    "backup exists!\n");
            exit (1);
	}
    }
    
    inclistfile  = home + "/lists/" + volume + ".incremental";
    fulllistfile = home + "/lists/" + volume + ".full";
	
    if (incremental)
    {
        exec (home + "/misc/buildlist", volume_startdir, volume_exclude, 
	        inclistfile, fullstampfile, fulllistfile);
        exec (home + "/afioscripts/makebackup", inclistfile, output);
        if (exists (incrementalstampfile))
	    exec ("rm", "-f", incrementalstampfile);
        exec ("date", ">>", incrementalstampfile);
    }
    else
    {
        exec (home + "/misc/buildlist", volume_startdir, volume_exclude, 
	        fulllistfile);
        exec (home + "/afioscripts/makebackup", fulllistfile, output);
        if (exists (fullstampfile))
	    exec ("rm", "-f", fullstampfile);
        exec ("date", ">>", fullstampfile);
    }
    
    exit (0);	
}
