#
# Copyright (c) 1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

#
# backup-null - backup script that doesn't do a backup.  For testing.
#

$chain =	0;
$level =	0;
$directory =	".";

require 'global.defs';
require 'local.defs';
require $yagrip_perl;
require $backuplib_perl;
require $unctime_perl;
require $ctime_perl;

$options = "c:l:f:d:F:Db:m:";
$usage = 
"usage: backup-null [-c chain] [-l level] [-f host:tape] [-d debuglevel]
    [-F files] [-D] [-b buid#] directory
    -c chain		chain number
    -l level		level number (or name)
    -f host:tape	host and tape to use
    -d debuglevel	print debugging messages
    -F files		where to log dump directory (NYI)
    -D			we're archiving to disk, not tape
    -b buid#		backup id number for this backup
    directory		which directory to back up\n";

&init();

$now_date = &getdate();
$now_timestamp = time;

#
# Update from_date, to_date in this hosts's BU DB
#
print "backup-null: update dates in per host backup DB\n" if $opt_d & $DEBUG_INFO;

%replace = ();
%replace = ("fromdate",	100,
	    "todate",	$now_timestamp);
if (&update_db($backup_host_file . $hostname, $backup_host_file . $hostname,
	       $buid,"backup_host", $DB_UPDATE, *replace, *add, *result)) {
    &sigh("$0: cannot update DB entry for $buid in backup host file for $hostname\n");
}

    #
    # How to deal with the "file" list with dump...
    #
undef $opt_F;			# ignore -F for now.
if (defined($opt_F)) {
    $toc_flag = "a";
    $toc_file = $opt_F;
} else {
    $toc_flag = "";
    $toc_file = "";
}

    #
    # Fork and execute the command string, and wait for the results.  If 
    # the command failed, die now (and don't update the tardates file).
    #

    #
    # If we are in parallel mode, save STDOUT, close it and reopen it to 
    # shove the archive through compress and into the archive file.
    #
if (defined($opt_D)) {
    open(SAVEOUT, ">&STDOUT");	# save STDOUT, 
    close(STDOUT);

    $cmd = "| $compress_prog > $opt_f.Z";
    print stderr "backup-dump: open '$cmd'\n" if $opt_d & $DEBUG_SYSTEM;

    if (! open(STDOUT, $cmd)) {
	&clean_up_dd();
	die "error (backup-null): can't pipe archive to compress";
    }
}

    #
    # Prepare the command line for running dump.
    #
if (defined($opt_D)) {
    $archive_file = "-";
} else {
    ($tape_user, $tape_host, $tape_dev) = &splittapedev($opt_f);
    if ($tape_host eq $hostname) {
	$archive_file = $tape_dev;
    } else {
	if ($tape_user ne "" && $dump_does_remote_users) {
	    $archive_file = "$tape_user@$tape_host:$tape_dev";
	} else {
	    $archive_file = "$tape_host:$tape_dev";
	}
    }
}

$command = sprintf("%s %ddsuf%s%s%s $dump_density $dump_length $archive_file $toc_file $dump_extra_args $directory",
		   (defined($opt_D)) ? $dump_prog : $rdump_prog,
		   $level,
		   $toc_flag,
		   $dump_extra_keys);

#print stderr "backup-dump: system $command\n";

    #
    # If we are in parallel mode, redirect STDOUT back to what it 
    # was before.
    #
if (defined($opt_D)) {
    close(STDOUT);
    open(STDOUT, ">&SAVEOUT");
}

exit(0);


#
# Deal with command line args and do init stuff
#
sub init {

    #
    # Make sure that we clean up after ourselves if someone wants to kill us.
    #
    $SIG{'INT'} = 'handle_interrupts';
    $SIG{'HUP'} = 'handle_interrupts';
    $SIG{'TERM'} = 'handle_interrupts';
    $SIG{'QUIT'} = 'handle_interrupts';

    #
    # deal with args
    #	default for tapehost and tapedev is scarecrow and /dev/rst1 for now
    #
    &read_config();		# Read backup.config file

    &getopt($options) || die $usage;

    die $usage if $#ARGV != 0;

    if (! defined($opt_c) || ! defined($opt_l) || ! defined($opt_f) 
	|| ! defined($opt_b)) {
	die "error (backup-null): -c, -l, -f and -b are required options (oxymoron?)\n";
    }

    $directory = $ARGV[0];
    $chain = $opt_c;
    $level = $opt_l;
    $buid = $opt_b;
    $ENV{"BUID"} = $buid;
}

