Source

mikmod-website / mikmod-website / process.pl

#!/usr/bin/perl

use strict;
use warnings;

use Template;
use File::Find::Object;

use File::Path qw( mkpath );
use File::Spec;
use File::Copy qw( copy );

my $template = Template->new(
    {
        INCLUDE_PATH => ".",
        POST_CHOMP => 1,
        RELATIVE => 1,
        ENCODING => 'utf8',
    }
);

my $vars = {};

my $tree = File::Find::Object->new({}, '.');

while (my $result = $tree->next_obj())
{
    if ($result->is_dir())
    {
        if ($result->path() eq './dest')
        {
            $tree->prune();
        }
        else
        {
            mkpath(File::Spec->catdir(
                    File::Spec->curdir(), "dest", @{$result->full_components()}
                )
            );
        }
    }
    else
    {
        my $basename = $result->basename;
        if ($basename =~ s/\.tt2\z/.php/)
        {
            $template->process($result->path(), $vars,
                File::Spec->catfile(File::Spec->curdir(), "dest",
                    @{$result->dir_components()}, $basename)
            )
                or die $template->error();
        }
        elsif ($basename !~ /~\z/)
        {
            copy($result->path,
                File::Spec->catfile(File::Spec->curdir(), "dest",
                    @{$result->dir_components()}, $basename),
            );
        }
    }
}