#!/usr/bin/perl -w
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  savehost - create backup copies of a list of files for this host
#
#SYNOPSIS
#  savehost [file]..
#
#REQUIRES
	push @INC, ".", "sh";
	require "Backup.pm";
#
#DESCRIPTION
#
#OPTIONS
#
#EXAMPLES
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$forreal = 1;		# If true, do the copying
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 1;	# Verbose level.

($host = `hostname`) =~ s/[\r\n]+//;
$host =~ s/\..*$//;		# Strip off domain part
if (-d $host) {
	print "Directory $host exists.\n" if $V>1;
} elsif (-e $host) {
	print STDERR "$P: ### host name $host is a file ###\n" if $V>0;
	exit 1;
} else {
	print "Creating $host directory.\n" if $V>1;
	if (mkdir $host, 0755) {
		print "Directory $host created.\n" if $V>1;
	} else {
		print STDERR "$P: ### can't create $host directory ###\n" if $V>0;
		exit $?;
	}
}

for $arg (@ARGV) {
	print STDERR "$P: Arg \"$arg\"\n" if $V>2;
	if (($oflg,$opts) = ($arg =~ /^([-+])(.*)$/)) {
		print STDERR "$P: ARG '$oflg' \"$opts\"\n" if $V>1;
	} else {
		push @files, $arg;
	}
}

@files = qw(DT.pm Vopt.pm cgilocal.pm clientinfo.pm dt.pm sendsubs.pm taintsubs.pm tuneadd tunefind form.cgi tuneform tuneget tunelist webpage.pm)
	unless @files;

file:
for $file (@files) {
	print "File \"$file\"\n" if $V>1;
	if (-f $file) {
		print "Save $file ...\n" if $V>1;
		&save($host,$file);
	} else  {
		print STDERR "$P: File \"$file\" doesn't exist.\n" if $V>0;
	}
	if (-f "$host-$file") {
		print "Save $host-$file ...\n" if $V>1;
		&save($host,"$host-$file");
	}
}

print STDERR "$P: Exit with status $exitstat.\n" if $V>1;
exit $exitstat;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub save {my $F='save'; local($h,$f) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	local($cmd);
	$cmd = "/bin/cp -p $f $h/$f";
	print "$F: cmd=\"$cmd\"\n" if $V>2;
	if ($forreal) {
		system $cmd;
		print "Copied \"$f\" to \"$h/$f\".\n" if $V>1;
	} else {
		print "Copy \"$f\" to \"$h/$f\".\n" if $V>1;
	}
}
