#!/usr/local/bin/perl
#
# Backup 3com switch configs in batch mode
#
# Jim Trocki
#
# $Id: 3cbatch-backup,v 1.5 1999/05/14 03:01:36 trockij Exp $
#
# Copyright (C) 1998 Jim Trocki
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
use A3Com::Sys;
use Getopt::Std;

sub touch;
sub help;
sub status;

getopts ("hdsp:t:", \%opt);

help() if ($opt{"h"});

die "$opt{p} does not exist\n"
    if ($opt{"p"} && ! -d $opt{"p"});

$TFTPHOST = $opt{"t"} || die "tftp host not specified\n";
$PATH = $opt{"p"} || "/tftpboot/3cbackup";
$STATUS = $opt{"s"} ? \&status : undef;

die "$PATH does not exist\n"
    if (! -d $PATH);

if ($opt{"d"}) {
    my @t = localtime;
    my $DATE = sprintf ("%02d%02d%04d-%02d%02d",
    	$t[4]+1, $t[3], 1900 + $t[5], $t[2], $t[1]);
    $PATH = "$PATH/$DATE";
    if (! -d $PATH) {
	if (!mkdir ($PATH, 0755)) {
	    die "could not mkdir $PATH: $!\n";
	}
    }
}

die "tftp host should be an IP address\n"
    if ($TFTPHOST !~ /^\d+\. \d+\. \d+\. \d+$/x);

die "no switch list supplied\n"
    if (@ARGV == 0);

$COMM = A3Com::Sys::get_password ("Write community: ");

die "invalid community string\n" if ($COMM =~ /^\s*$/);

my $failures;

open (SWLIST, $ARGV[0]) ||
    die "could not open $ARGV[0]: $!\n";

MAINLOOP: while (<SWLIST>) {
    next if (/^\s*#/ || /^\s*$/);
    chomp;
    my $host = $_;
    $host =~ s/^ \s* (\S+) .* $/$1/x;
    my $P = "$PATH/$host";

    if (!defined (touch $P)) {
    	print "$host failed could not create $P: $!\n";
	$failures++;
	next MAINLOOP;
    }

    if (!defined (touch "$P.nvd")) {
    	print "$host failed could not create $P.nvd: $!\n";
	$failures++;
	next MAINLOOP;
    }

    if (defined $STATUS) {
    	print STDERR "$host\n";
    }

    my $owner = getpwuid ($<) or "xyzzy";
    my ($r, $v) = A3Com::Sys::filetransfer (
    	host => $host,
	community => $COMM,
	owner => $owner,
	operation => "save_nvdata",
	tftphost => $TFTPHOST,
	file => "$P.nvd",
	force => "true",
	status => $STATUS,
    );

    if (!defined $r) {
    	print "$host failed $v\n";
	$failures++;
    }
}

close (SWLIST);

exit $failures if ($failures);

exit 0;


sub touch {
    my $f = shift;

    return undef if (!open (O, ">$f"));
    close (O);
    return undef if (!chmod (0666, $f));
    return 1;
}


sub help {
    print <<EOF;
usage: 3cbatch-backup [-h] [-d] [-p path] -t tftphost

  -h            help
  -d            date
  -p path       destination path
  -t tftphost   IP address of TFTP host
  -s            display status during transfer

EOF
    exit;
}

sub status {
    my ($s, $b) = @_;

    print STDERR "  $b bytes transferred\n";
}
