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

#define VER             "1.03"
#define DEFAULT_HOME    "/usr/local/etc/backup"

void usage ()
{
    printf (
"\n"    
"ICCE Backup Utility  V" VER "\n"
"Copyright (c) ICCE 1994. All rights reserved.\n"
"\n"
"backup by Karel Kubat.\n"
"\n"
"Usage: backup action [arguments]\n"
"where action and arguments are:\n"
"     -full volume outputfile        : makes a full backup of a volume to\n"
"                                      the given output file\n"
"     -incremental volume outputfile : makes an incremental backup of all\n"
"                                      changed files since the last full\n"
"                                      backup of the same volume\n"
"     -backups                       : lists performed backups\n"
"     -which [pattern]               : searches for a pattern or all files\n"
"                                      in the backup list files\n"
"     -verbose inputfile [pattern]   : searches for a pattern or all files\n"
"                                      in the backup on the output file\n"
"     -restore inputfile pattern     : restores files matching the pattern\n"
"                                      relative to the current directory\n"
"\n"
    );
    exit (1);
}

string
    home;
    
void homeenv (list envp)
{
    int
        i;
	
    for (i = 0; i < sizeof (envp); i += 2)
        if (element (i, envp) == "BACKUP_HOME")
	{
	    home = element (i + 1, envp);
	    return;
	}
	
    home = DEFAULT_HOME;
    putenv ("BACKUP_HOME=" DEFAULT_HOME);
}

void needargs (int argc, int min, int max)
{
    if (argc < min || argc > max)
        usage ();
}

void full (int argc, list argv)
{
    needargs (argc, 4, 4);
    exec (home + "/backupscripts/runbackup", 
        element (2, argv), element (3, argv));
}    

void incremental (int argc, list argv)
{
    needargs (argc, 4, 4);	
    exec (home + "/backupscripts/runbackup", 
        "-incremental", element (2, argv), element (3, argv));
}

void which (int argc, list argv)
{
    string
        pattern;
	
    needargs (argc, 2, 3);
    if (argc == 3)
        pattern = "'" + element (2, argv) + "'";
    exec (home + "/backupscripts/list", pattern);
}	

void backups (int argc, list argv)
{
    needargs (argc, 2, 2);
    exec (home + "/backupscripts/listbackups");
}

void verbose (int argc, list argv)
{
    string
        pattern;
	
    needargs (argc, 3, 4);
    if (argc == 4)
        pattern = "'" + element (3, argv) + "'";
    exec (home + "/afioscripts/listbackup", element (2, argv), pattern);
}

void restore (int argc, list argv)
{
    string
        pattern;
	
    needargs (argc, 4, 4);
    pattern = "'" + element (3, argv) + "'";
    exec (home + "/afioscripts/restorebackup", element (2, argv), pattern);
}

void makedist ()
{
    list
        files;
    int
        i;
	
    files = (list) "/usr/local/bin/icmake" +
            (list) "/usr/local/bin/icm-pp" +
            (list) "/usr/local/bin/icm-comp" +
            (list) "/usr/local/bin/icm-exec" +
            (list) "/usr/local/bin/icmum" +
            (list) "/usr/local/bin/afio";

    for (i = 0; i < sizeof (files); i++)
        if (change_path (element (i, files), "auxbin") older 
	    element (i, files)
	   )
	    exec ("cp", element (i, files), "auxbin");
	    
    if ("auxscripts/netbackup" older "/usr/local/bin/netbackup")
        exec ("cp", "/usr/local/bin/netbackup", "auxscripts");
	
    exec ("find", ".", "-name \\\*.bim", "-exec rm {} \\;");
    
    exec ("mkdir", "-p", "/tmp/backup/lists", "/tmp/backup/volumes",
           "/tmp/backup/stamps");
    exec ("mv", "volumes/*", "/tmp/backup/volumes");
    exec ("mv", "lists/*",   "/tmp/backup/lists");
    exec ("mv", "stamps/*",  "/tmp/backup/stamps");
    
    chdir ("..");
    exec ("tar", "cvzf", "backup-" VER ".tar.gz", "backup");
    
    chdir ("backup");
    exec ("mv", "/tmp/backup/stamps/*",  "stamps");
    exec ("mv", "/tmp/backup/lists/*",   "lists");
    exec ("mv", "/tmp/backup/volumes/*", "volumes");
    exec ("rm", "-r", "/tmp/backup auxbin/* auxscripts/*");
}

void main (int argc, list argv, list envp)
{
    string
        selector;

    echo (0);

    homeenv (envp);
	
    selector = element (1, argv);
    
    if (selector == "-full")
        full (argc, argv);
    else if (selector == "-incremental")
        incremental (argc, argv);
    else if (selector == "-which")
        which (argc, argv);
    else if (selector == "-backups")
        backups (argc, argv);
    else if (selector == "-verbose")
        verbose (argc, argv);
    else if (selector == "-restore")
        restore (argc, argv);
    else if (selector == "-makedist")
        makedist ();
    else
        usage ();
	
    exit (0);
}
