#!/usr/bin/perl
use Cwd qw(realpath);
use File::Basename;
use File::Path qw(mkpath);
use File::ShareDir;
use Getopt::Long;
use Mason;
use Mason::Util qw(write_file);
use strict;
use warnings;

my $share_dir;
GetOptions( "share-dir=s" => \$share_dir, );
$share_dir ||= File::ShareDir::dist_dir('Mason-Plugin-PSGIHandler');
$share_dir = realpath($share_dir);

my $dir = shift(@ARGV) or die "usage: $0 root_dir";
die "$dir already exists; will not overwrite" if -e $dir;

my $comp_root = "$share_dir/mason_psgi_setup.skel";
my $interp =
  Mason->new( comp_root => $comp_root, autoextend_request_path => 0, top_level_regex => qr/./ );
my @paths = $interp->all_paths()
  or die "could not find template components";
foreach my $path (@paths) {
    my $output = $interp->run($path)->output;
    my $dest   = $dir . $path;
    print "writing $dest\n";
    mkpath( dirname($dest), 0, 0775 );
    write_file( $dest, $output );
}
mkpath( "$dir/data", 0, 0775 );
print "Run\n\n    cd $dir; plackup -r\n\nto start your server.\n";

__END__

=head1 NAME

mason_psgi_setup - create a skeleton Mason/PSGI site

=head1 SYNOPSIS

   % mason_psgi_setup /path/to/server
   % cd /path/to/server
   % plackup -r

=head1 DESCRIPTION

Creates a directory with a small, but fully functioning, Mason/PSGI site.

=cut
