Generate output path for template files
[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);
9 use File::Basename qw (fileparse);
10 use File::Copy::Recursive qw(fcopy);
11
12 my $out_dir = "out/";
13
14 #find all files
15
16 my @files = File::Find::Rule->file()
17                             ->in('.');
18
19
20 #initialize template toolkit
21
22 my $template = Template->new;
23
24 if (! -d $out_dir) {
25         make_path($out_dir) or die "Could not create outdir $out_dir: $!";
26 }
27
28 foreach my $file (@files) {
29         next if $file =~ /^$out_dir/;
30         next if $file =~ /$0$/;
31         if ($file =~ /\.tt2$/) {
32                 my $output;
33                 $template->process($file, undef, \$output)
34                         || die "Could not process file \"$file\": $!";
35
36                 my ($name,$path,$suffix) = fileparse($file,qw (.tt2));
37         make_path("$out_dir/$path") unless -d "$out_dir/$path";
38                 open (my $fh, '>', "$out_dir/$path/$name")
39                         or die "Could not write to $file: $!";
40                 print $fh $output;
41                 close($fh);
42         } else {
43                 fcopy ($file, "$out_dir/$file") or die "Could not copy $file to $out_dir/$file: $!";
44         }
45 }