#!/usr/bin/perl -w

# Make a list of URLs in the cache and their corresponding
# cache filenames

use File::Find ();
use File::Basename;

BEGIN {
    unshift @INC, dirname($0).'/../../lib/arc';
}

use ConfigCentral;


# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;

my $conffile;

if (@ARGV == 2 && $ARGV[0] eq '-c') {
    $conffile = $ARGV[1];
}

sub usage {
    print <<EOH;
    usage: $0 -c <arex_config_file>
EOH
    exit 1;
}

if (!$conffile && -e $ENV{"ARC_CONFIG"}) {
    $conffile = $ENV{"ARC_CONFIG"};
}

usage() unless $conffile;

# parse to find cache dirs
my @caches;

my $config = ConfigCentral::parseConfig($conffile);
die "Failed parsing A-REX config file '$conffile'" unless $config;
die "No users set up in config file '$conffile'"
    unless $config->{control} and ref $config->{control} eq 'HASH';
for my $control (values %{$config->{control}}) {
    next unless ref $control eq 'HASH';
    next unless $control->{cachedir} and ref $control->{cachedir} eq 'ARRAY';
    for (@{$control->{cachedir}}) {
        print "\n Warning: cache-clean cannot deal with substitutions - $_\n" and next if /%/;
        print "\n Warning: ignoring malformed cahe location - $_\n" and next unless m{^(/\S+)};
        push @caches, $1;
    }
}
die "No caches found in config file '$conffile'" unless @caches;

foreach $cache (@caches) {
    print "Cache: $cache\n";
    if (! -d $cache || ! -d $cache."/data") { print " Cache is empty\n"; }
        else { File::Find::find({wanted => \&wanted}, $cache."/data"); }
}

sub wanted {
    return if $name !~ m|\.meta$|;
    return if ! -e substr($name, 0, -5);
    open FILE, $name or die "$name $!";
    my $line = <FILE>;
    my @data = split(/\s+/, $line);
    my $fname = substr($name, 0, rindex($name, ".meta"));
    print " $data[0] $fname\n";
}
