#!/usr/local/bin/perl
#
# Perform file transfers with a 3Com switch
#
# Jim Trocki
#
# $Id: 3cft,v 1.12 1999/04/06 15:27:00 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 Getopt::Std;
use A3Com::Sys;

sub help;

getopts ("t:f:o:hrsF", \%opt);

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

$HOST		= shift || die "no device specified\n";
$TFTPHOST	= $opt{"t"} || die "no TFTP host specified\n";
$FILE		= $opt{"f"} || die "no file specified\n";
$OWNER		= getpwuid($<) || "xyzzy";
$OP		= $opt{"o"};
$COMM		= "";
$FORCE		= $opt{"F"} ? "true" : "false";
$STAT		= $opt{"s"} ? \&status : undef;

die "operation must be one of save_nvdata|restore_nvdata|softwareupdate\n"
    if ($OP !~ /^save_nvdata|restore_nvdata|softwareupdate$/);

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

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

($r, $v) = A3Com::Sys::filetransfer (
	operation => $OP,
	host => $HOST,
	community => $COMM,
	owner => $OWNER,
	tftphost => $TFTPHOST,
	file => $FILE,
	status => $STAT,
	force => $FORCE,
);

if (!defined $r) {
    die "$HOST failed $v\n";
}

print "$HOST complete\n";

A3COM::Sys::reboot (host => $HOST, community => $COMM)
    if ($opt{"r"});

exit;


sub help {
    print <<EOF;
usage: 3cft [-s] -t tftphostname -f filename -o opname host
    -t tftphostname   hostname or IP address of TFTP server
    -f filename       filename of source or destination
    -o opname         one of {save_nvdata|restore_nvdata|softwareupdate}
    -s                display file transfer status
    -r                reboot after successful transfer
    -F                force

    SNMP write community is accepted from an interactive prompt if stdin
    is a tty, otherwise from stdin.

EOF
    exit 0;
}


sub status {
    my ($st, $bytes) = @_;

    print "  bytes transferred: $bytes\n";
}
