#!/usr/local/bin/perl
;#
;# Copyright (c) 1995-1999
;#	Ikuo Nakagawa. All rights reserved.
;#
;# Redistribution and use in source and binary forms, with or without
;# modification, are permitted provided that the following conditions
;# are met:
;#
;# 1. Redistributions of source code must retain the above copyright
;#    notice unmodified, this list of conditions, and the following
;#    disclaimer.
;# 2. Redistributions in binary form must reproduce the above copyright
;#    notice, this list of conditions and the following disclaimer in the
;#    documentation and/or other materials provided with the distribution.
;#
;# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
;# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
;# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
;# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
;# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
;# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;#
;# $Id: ftpmirror,v 1.15 1999/11/10 23:12:28 ikuo Exp $
;#
use strict;
use vars qw($VERSION $LOG $program);

;# modules
use Fan;

;# this is an alpha version.
$VERSION = '0.15';
$LOG = 5;

;# this program name
$program = $0 =~ m|([^/]+)$|;

;# full setup of Fan module.
Fan->full_setup(\@ARGV)
	or die("can't initialize Fan library.\n");

;# parsing arguments.
while (@ARGV) {
	my $name = shift;
	my $p = Fan->find_archive($name);

	unless (ref($p) && $p->isa('Fan')) {
		warn("$name: can't find archive, skipped.\n"), next;
	}

	# check first
	unless ($p->check) {
		warn("$name: check failure.\n"), next;
	}

	#
	my $todo;
	if (!defined($todo = $p->todo)) {
		$todo = $program;
	}

	# we use ``eval'' to do real work.
	# so, we can abort anywhere in subroutine.
	if ($todo eq 'update-master') {
		eval { $p->update_master };
	} elsif ($todo eq 'scan-local') {
		eval { $p->scan_local };
	} elsif ($todo eq 'scan-remote') {
		eval { $p->scan_remote };
	} elsif ($todo eq 'mkdirinfo') {
		eval { $p->mkdirinfo };
	} elsif ($todo eq 'synch-remote') {
		eval { $p->step_synch };
	} elsif ($todo eq 'step-mirror') {
		eval { $p->run_step_mirror };
	} else { # remaining cases including "ftpmirror"
		eval { $p->run_full_mirror };
	}
	if ($@) {
		warn("eval($todo):\n$@");
	}
}

;#
exit;

;#
sub show_usage {
	use Fan::Usage;

	if (@_) {
		my $sig = shift;
		warn("* signal $sig detected.\n");
	}
	my $u = getrusage;
	$u->dump;
	undef $u;
	1;
}

=head1 NAME

ftpmirror - Mirror directory hiearachy via FTP (or HTTP).

=head1 SYNOPSIS

C<ftpmirror archive>

=head1 DESCRIPTION

Ftpmirror mirrors directory hiearachy via FTP (or HTTP).

=head1 AUTHER

Ikuo Nakagawa, Oct, 1999

=item

=cut
