#!/usr/bin/perl

# This script makes an FTP connection to the remote host and recursively
# transfers the specified files and directories to the remote host.
# The directory structure created underneath the specified root directory
# on the remote host mirrors the directory structure underneath the
# specified root directory on the local host.  The script takes two
# arguments.
#
# The script performs either a "full" or an "incremental" transfer.
# If the transfer is full, then all of the specified files and directories
# are transferred to the remote host with the exception of the files
# and directories on the exclusion list.  If the transfer is incremental,
# then only files which are newer than the log file are transferred.
#
# All information describing the transfer that is to be performed can be
# specified using command-line arguments.  However, there are no required
# command-line arguments.  See the documentation for details.

use Transfer;

# Collect information provided on the command line.

$info = read_command_line(@ARGV);

# Gather information from the info file, if one is provided.

if ($info->{info_file}) {

    # If an information file name was provided, collect the information
    # contained in the file.

    $flags = parse_info_file($info, $info->{info_file});

} else {

    # If there was no information file name provided and if the HOME
    # environment variable is set, collect information from the file
    # .transferrc in the HOME directory if it exists.

    if ($home = $ENV{HOME}) {

        $home =~ s/\/?$//;
        $rcfile = "$home/.transferrc";

        if (-s $rcfile and -f $rcfile) {
            $flags = parse_info_file($info, $rcfile);
        }

    }

}

$debug = 0;
if ($debug) { print_info($info, $list); exit; }

# Prompt the user for any missing information.

$info = prompt_for_missing_info($info, $flags);
chomp %$info;

# Remove any trailing white space and a trailing slash, if there is one,
# from the local and remote root directories.

$info->{local_root}  =~ s/\/?\s*$//;
$info->{remote_root} =~ s/\/?\s*$//;

# Extract the list of files for transfer.
# If performing an incremental transfer, then look through the directory tree
# for files modified more recently than the log file.  If performing a full
# transfer, construct the list of all files.  This step also adds the files
# in any directories on the list to the list of files and removes the
# directories from the list.

$list = retrieve_list($info);

# For debugging purposes.

$debug = 0;
if ($debug) { print_info($info, $list); }

# Throw out the files and directories on the exclusion list.
# There are no directory names in @files.

$debug = 0;
if ($debug) {
    print "Files before exclusion:\n";
    for (@$list) { print "$_\n" }
}

if ($info->{exclusions}) { exclude($list, $info) }

if ($debug) {
    print "\nFiles after exclusion:\n";
    for (@$list) { print "$_\n" }
    exit;
}

# If there are files to backup, make a ppp connection and ftp the files
# to the corresponding directory under the remote root.

if (@$list) { transfer_list($list, $info) }
