Merge branch 'master' into nonophp
[grml.org.git] / gen_website
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Template;
7 use File::Find::Rule;
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);
12
13 my $out_dir = shift || "out/";
14
15 #find all files
16
17 #rule to match git directorys
18 my $git = File::Find::Rule->directory
19                           ->name(".git")
20                           ->prune
21                           ->discard;
22
23 #matches all files
24 my $file_rule = File::Find::Rule->file();
25
26 #combine both
27 my @files = File::Find::Rule->or( $git, $file_rule )
28                             ->in('.');
29
30 #create a tempdir
31 my $tempdir = tempdir( CLEANUP => 1 );
32 make_path("$tempdir/out") or die "Could not create $tempdir/out: $!";
33
34
35 #initialize template toolkit
36
37 my $template = Template->new;
38
39 foreach my $file (@files) {
40         next if $file =~ /^$out_dir/;
41         next if $file =~ /$0$/;
42         if ($file =~ /\.tt2$/) {
43                 my $output;
44                 $template->process($file, undef, \$output)
45                         || die "Could not process file \"$file\": $!";
46
47                 my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
48                 make_path("$tempdir/out/$path") unless -d "$tempdir/out/$path";
49                 open (my $fh, '>', "$tempdir/out/$path/$name")
50                         or die "Could not write to $file: $!";
51                 print $fh $output;
52                 close($fh);
53         } else {
54                 fcopy ($file, "$tempdir/out/$file") or die "Could not copy $file to $tempdir/out/$file: $!";
55         }
56 }
57
58 $out_dir =~ s/\/$//;
59 if (-d $out_dir) {
60     dirmove ($out_dir, $out_dir . ".bak")
61         or die "Could not move $out_dir to $out_dir.bak: $!";
62 }
63 if (! dirmove ("$tempdir/out", "$out_dir")) {
64         warn "Could not move $tempdir/out to $out_dir: $!";
65         warn "Rollback";
66         remove_tree($out_dir);
67         dirmove ($out_dir . ".bak", $out_dir);
68 } else {
69         remove_tree($out_dir . ".bak");
70 }