#!/usr/local/bin/perl

use strict;
use vars qw($opt_D $opt_c $opt_l $opt_u $opt_d $opt_o $opt_v);
use Getopt::Std;
use Fan::Farm;
use Fan::Scan;

BEGIN {
	$| = 1;
}

getopts("Dludo:v")
	or die("Usage: $0 <-c|-u|-d|-l> [-v] [-o file]\n");

($opt_c + $opt_l + $opt_u + $opt_d == 1)
	or die("$0: just one of -c/-l/-u/-d must be given.\n");

$Fan::Farm::LOG = 6 if $opt_v;
$Fan::Farm::LOG = 7 if $opt_D;

$opt_o = \*STDOUT if $opt_o eq '';

if ($opt_c) {
	my $dir = shift;
	-d $dir or die("database directory must be given.\n");
	Fan::Farm::scan_listup($opt_o, $dir);
} elsif ($opt_u) {
	my $base = shift;
	-f $base or die("base index must be given.\n");
	@ARGV > 0 or die("no patch file specified.\n");
	Fan::Farm::scan_update($opt_o, $base, @ARGV);
} elsif ($opt_d) {
	my $old = shift;
	my $new = shift;
	-f $old or die("old index must exist.\n");
	-f $new or die("new index must exist.\n");
	Fan::Farm::scan_differ($opt_o, $old, $new);
} elsif ($opt_l) {
	my $index = shift;
	-f $index or die("index file must be given.\n");
	my $scan = Fan::Scan->new(
		scan_type => 'INDEX',
		scan_index => $index,
	);
	ref($scan) or die("can't create scan object.\n");
	my $x;
	while (defined($x = $scan->get)) {
		next if $x->type eq '.' || $x->type eq 'U';
		print $x->path."\n";
	}
} else {
	die("I don't know what to do\n");
}
