8 use File::Path qw(make_path remove_tree);
9 use File::Basename qw (fileparse dirname);
10 use File::Copy::Recursive qw(fcopy dirmove);
11 use File::Temp qw (tempdir);
13 use lib '/usr/share/perl5/';
16 my $m = Mirmon ->new('/etc/mirmon.conf');
17 my $conf = $m->conf ; # a Mirmon::Conf object
18 my $state = $m->state ; # the mirmon state
21 my $out_dir = shift || "out/";
22 my $masterlist = '/usr/local/src/grml-mirrors/Mirrors.masterlist';
24 open (my $fh, '<', $masterlist) or die "Could not open $masterlist: $!";
27 sub get_last_state ($) {
29 my $mirror_state = $state->{ $url } ; # a Mirmon::Mirror object
30 my ($time, $history) = split('-', $mirror_state->{state_history});
31 my $last_state = substr($history,-1,1);
37 while (my $line = <$fh>) {
39 if ($line =~ /([^:]+): (.*)/) {
42 $data->{$key} = $value;
43 } elsif ($line eq '') {
44 my $url = sprintf ("http://%s%s", $data->{'site'}, $data->{'grml-http'});
45 next if get_last_state($url) eq 'f';
46 $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data;
49 print "Malformed line: $line\n";
54 my $url = sprintf ("http://%s%s", $data->{'site'}, $data->{'grml-http'});
55 $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data if get_last_state($url) eq 'f';
60 #rule to match git directorys
61 my $git = File::Find::Rule->directory
67 my $file_rule = File::Find::Rule->file();
70 my @files = File::Find::Rule->or( $git, $file_rule )
74 my $tempdir = tempdir( CLEANUP => 1 );
75 make_path("$tempdir/out") or die "Could not create $tempdir/out: $!";
78 #initialize template toolkit
80 my $template = Template->new;
82 foreach my $file (@files) {
83 next if $file =~ /^$out_dir/;
84 next if $file =~ /$0$/;
85 if ($file =~ /\.tt2$/) {
87 $template->process($file, undef, \$output)
88 || die "Could not process file \"$file\": $!";
90 my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
91 make_path("$tempdir/out/$path") unless -d "$tempdir/out/$path";
92 open (my $fh, '>', "$tempdir/out/$path/$name")
93 or die "Could not write to $file: $!";
97 fcopy ($file, "$tempdir/out/$file") or die "Could not copy $file to $tempdir/out/$file: $!";
103 dirmove ($out_dir, $out_dir . ".bak")
104 or die "Could not move $out_dir to $out_dir.bak: $!";
106 if (! dirmove ("$tempdir/out", "$out_dir")) {
107 warn "Could not move $tempdir/out to $out_dir: $!";
109 remove_tree($out_dir);
110 dirmove ($out_dir . ".bak", $out_dir);
112 remove_tree($out_dir . ".bak");