Merge remote-tracking branch 'origin/github/pr/45'
[grml.org.git] / gen_website
index e095187..7c26751 100755 (executable)
@@ -5,12 +5,66 @@ use warnings;
 
 use Template;
 use File::Find::Rule;
-use File::Path qw(make_path);
-use File::Basename qw (fileparse);
-use File::Copy::Recursive qw(fcopy);
+use File::Path qw(make_path remove_tree);
+use File::Basename qw (fileparse dirname);
+use File::Copy::Recursive qw(fcopy dirmove);
 use File::Temp qw (tempdir);
 
-my $out_dir = "out/";
+use lib '/usr/share/perl5/';
+
+my $have_mirmon = 0;
+my $masterlist = '/usr/local/src/grml-mirrors/Mirrors.masterlist';
+my $mirmonconf = '/etc/mirmon.conf';
+
+my $fh;
+my $m;
+my $conf;
+my $state;
+if (eval {require Mirmon; 1 } && -f $masterlist && -f $mirmonconf) {
+       $have_mirmon = 1;
+       open ($fh, '<', $masterlist) or die "Could not open $masterlist: $!";
+       $m = Mirmon ->new($mirmonconf);
+       $conf  = $m->conf  ; # a Mirmon::Conf object
+       $state = $m->state ; # the mirmon state
+} else {
+       print "Skipping mirmon, it's either not installed or not configured.\n";
+}
+
+
+my $out_dir = shift || "out/";
+
+my $mirrors; 
+
+sub get_last_state ($) {
+       my $url = shift;
+       my $mirror_state = $state->{ $url } ; # a Mirmon::Mirror object
+       my ($time, $history) = split('-', $mirror_state->{state_history});
+       my $last_state = substr($history,-1,1);
+       return $last_state;
+}
+
+
+my $data;
+while (($have_mirmon) && (my $line = <$fh>)) {
+    chomp $line;
+    if ($line =~ /([^:]+): (.*)/) {
+        my $key = lc($1);
+        my $value = $2;
+        $data->{$key} = $value;
+    } elsif ($line eq '') {
+       my $url = sprintf ("https://%s%s", $data->{'site'}, $data->{'grml-http'});
+               next if get_last_state($url) eq 'f';
+       $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data;
+        $data = undef;
+    } else {
+        print "Malformed line: $line\n";
+    }
+}
+
+if ($data) {
+       my $url = sprintf ("https://%s%s", $data->{'site'}, $data->{'grml-http'});
+       $mirrors->{ $data->{'country'} }->{ $data->{'site'} } = $data if get_last_state($url) eq 'f';
+}
 
 #find all files
 
@@ -23,33 +77,51 @@ my $git = File::Find::Rule->directory
 #matches all files
 my $file_rule = File::Find::Rule->file();
 
-#combine both
-my @files = File::Find::Rule->or( $git, $file_rule )
+#match symlinks
+my $symlink_rule =  File::Find::Rule->symlink();
+
+#combine all rules
+my @files = File::Find::Rule->or( $git, $file_rule, $symlink_rule )
                             ->in('.');
 
+#create a tempdir
+my $tempdir = tempdir( CLEANUP => 1 );
+make_path("$tempdir/out") or die "Could not create $tempdir/out: $!";
+
+
 #initialize template toolkit
 
 my $template = Template->new;
 
-if (! -d $out_dir) {
-       make_path($out_dir) or die "Could not create outdir $out_dir: $!";
-}
-
 foreach my $file (@files) {
        next if $file =~ /^$out_dir/;
        next if $file =~ /$0$/;
        if ($file =~ /\.tt2$/) {
                my $output;
-               $template->process($file, undef, \$output)
+               $template->process($file, { mirrors => $mirrors }, \$output)
                        || die "Could not process file \"$file\": $!";
 
                my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
-        make_path("$out_dir/$path") unless -d "$out_dir/$path";
-               open (my $fh, '>', "$out_dir/$path/$name")
+               make_path("$tempdir/out/$path") unless -d "$tempdir/out/$path";
+               open (my $fh, '>', "$tempdir/out/$path/$name")
                        or die "Could not write to $file: $!";
                print $fh $output;
                close($fh);
        } else {
-               fcopy ($file, "$out_dir/$file") or die "Could not copy $file to $out_dir/$file: $!";
+               fcopy ($file, "$tempdir/out/$file") or die "Could not copy $file to $tempdir/out/$file: $!";
        }
 }
+
+$out_dir =~ s/\/$//;
+if (-d $out_dir) {
+    dirmove ($out_dir, $out_dir . ".bak")
+        or die "Could not move $out_dir to $out_dir.bak: $!";
+}
+if (! dirmove ("$tempdir/out", "$out_dir")) {
+       warn "Could not move $tempdir/out to $out_dir: $!";
+       warn "Rollback";
+       remove_tree($out_dir);
+       dirmove ($out_dir . ".bak", $out_dir);
+} else {
+       remove_tree($out_dir . ".bak");
+}