#!/usr/bin/perl

$DEFAULT_CLASS = 'Text::Forge';

%CLASS_ABBREV = (
                  cgi => 'Text::Forge::CGI',
                );

use strict;
use vars qw( %OPT $DEFAULT_CLASS %CLASS_ABBREV );
use Getopt::Long qw( GetOptions );

sub usage {
  print STDERR "Usage: $0 [--template-path=] [--class=] [--noheader] ";
  print STDERR "<template-filename>\n";
  exit 1;
}

&GetOptions(\%OPT, 'template-path=s', 'class=s', 'help', 'usage', 'header!');
usage if $OPT{help} or $OPT{usage};
$OPT{header} = 1 unless exists $OPT{header};
$OPT{class} ||= $DEFAULT_CLASS;
$OPT{class} = $CLASS_ABBREV{ lc $OPT{class} } 
  if exists $CLASS_ABBREV{ lc $OPT{class} };

my $forge = eval "require $OPT{class}; new $OPT{class}"; 
die "error loading class $OPT{class} : $!\n" if $@;

$forge->template_path( $OPT{'template-path'} ) if $OPT{'template-path'};

my $template = shift @ARGV or usage;
my $doc = $forge->generate( $template );
print $doc->as_string;

