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 $symlink_rule = File::Find::Rule->symlink();
73 my @files = File::Find::Rule->or( $git, $file_rule, $symlink_rule )
77 my $tempdir = tempdir( CLEANUP => 1 );
78 make_path("$tempdir/out") or die "Could not create $tempdir/out: $!";
81 #initialize template toolkit
83 my $template = Template->new;
85 foreach my $file (@files) {
86 next if $file =~ /^$out_dir/;
87 next if $file =~ /$0$/;
88 if ($file =~ /\.tt2$/) {
90 $template->process($file, { mirrors => $mirrors }, \$output)
91 || die "Could not process file \"$file\": $!";
93 my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
94 make_path("$tempdir/out/$path") unless -d "$tempdir/out/$path";
95 open (my $fh, '>', "$tempdir/out/$path/$name")
96 or die "Could not write to $file: $!";
100 fcopy ($file, "$tempdir/out/$file") or die "Could not copy $file to $tempdir/out/$file: $!";
106 dirmove ($out_dir, $out_dir . ".bak")
107 or die "Could not move $out_dir to $out_dir.bak: $!";
109 if (! dirmove ("$tempdir/out", "$out_dir")) {
110 warn "Could not move $tempdir/out to $out_dir: $!";
112 remove_tree($out_dir);
113 dirmove ($out_dir . ".bak", $out_dir);
115 remove_tree($out_dir . ".bak");