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 $masterlist = '/usr/local/src/grml-mirrors/Mirrors.masterlist';
17 my $mirmonconf = '/etc/mirmon.conf';
23 if (eval {require Mirmon; 1 } && -f $masterlist && -f $mirmonconf) {
25 open ($fh, '<', $masterlist) or die "Could not open $masterlist: $!";
26 $m = Mirmon ->new($mirmonconf);
27 $conf = $m->conf ; # a Mirmon::Conf object
28 $state = $m->state ; # the mirmon state
30 print "Skipping mirmon, it's either not installed or not configured.\n";
34 my $out_dir = shift || "out/";
38 sub get_last_state ($) {
40 my $mirror_state = $state->{ $url } ; # a Mirmon::Mirror object
41 my ($time, $history) = split('-', $mirror_state->{state_history});
42 my $last_state = substr($history,-1,1);
48 while (($have_mirmon) && (my $line = <$fh>)) {
50 if ($line =~ /([^:]+): (.*)/) {
53 $data->{$key} = $value;
54 } elsif ($line eq '') {
55 my $url = sprintf ("http://%s%s", $data->{'site'}, $data->{'grml-http'});
56 next if get_last_state($url) eq 'f';
57 $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data;
60 print "Malformed line: $line\n";
65 my $url = sprintf ("http://%s%s", $data->{'site'}, $data->{'grml-http'});
66 $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data if get_last_state($url) eq 'f';
71 #rule to match git directorys
72 my $git = File::Find::Rule->directory
78 my $file_rule = File::Find::Rule->file();
81 my $symlink_rule = File::Find::Rule->symlink();
84 my @files = File::Find::Rule->or( $git, $file_rule, $symlink_rule )
88 my $tempdir = tempdir( CLEANUP => 1 );
89 make_path("$tempdir/out") or die "Could not create $tempdir/out: $!";
92 #initialize template toolkit
94 my $template = Template->new;
96 foreach my $file (@files) {
97 next if $file =~ /^$out_dir/;
98 next if $file =~ /$0$/;
99 if ($file =~ /\.tt2$/) {
101 $template->process($file, { mirrors => $mirrors }, \$output)
102 || die "Could not process file \"$file\": $!";
104 my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
105 make_path("$tempdir/out/$path") unless -d "$tempdir/out/$path";
106 open (my $fh, '>', "$tempdir/out/$path/$name")
107 or die "Could not write to $file: $!";
111 fcopy ($file, "$tempdir/out/$file") or die "Could not copy $file to $tempdir/out/$file: $!";
117 dirmove ($out_dir, $out_dir . ".bak")
118 or die "Could not move $out_dir to $out_dir.bak: $!";
120 if (! dirmove ("$tempdir/out", "$out_dir")) {
121 warn "Could not move $tempdir/out to $out_dir: $!";
123 remove_tree($out_dir);
124 dirmove ($out_dir . ".bak", $out_dir);
126 remove_tree($out_dir . ".bak");