0
  5   ˆl–ß=¿J
j"T% íÆŸ¸ ©‹Fÿ�þDË„Y²ÆèŸ ŸšóÉƒ|=ÈÁË #l    #!/usr/bin/perl -w
#========================================================================
#
# tpage
#
# DESCRIPTION
#   Script for processing and rendering a template document using the 
#   Perl Template Toolkit. 
#
# AUTHOR
#   Andy Wardley   <abw@kfs.org>
#
# COPYRIGHT
#   Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
#   Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#------------------------------------------------------------------------
#
# $Id$
#
#========================================================================

use strict;
use Template;
use Template::Directive;
use AppConfig;

my $NAME     = "tpage";
my $VERSION  = 2.70;
my $HOME     = $ENV{ HOME } || '';
my $RCFILE   = $ENV{"\U${NAME}rc"} || "$HOME/.${NAME}rc";
my $TTMODULE = 'Template';

# read .tpagerc file and any command line arguments
my $config   = read_config($RCFILE);

# unshift any perl5lib directories onto front of INC
unshift(@INC, @{ $config->perl5lib });

# get all template_directive_* options from the config
my %ttdirectiveopts = $config->varlist('^template_directive_', 1);

# get all template_* (except template_directive_*) options from the config and fold keys to UPPER CASE
my %ttopts   = $config->varlist('^template_(?!directive_)', 1);
my $ttmodule = delete($ttopts{ module });
my $ttenvvars = delete($ttopts{ envvars });
my $ucttopts = {
    map { my $v = $ttopts{ $_ }; defined $v ? (uc $_, $v) : () }
    keys %ttopts,
};

# load custom template module 
if ($ttmodule) {
    my $ttpkg = $ttmodule;
    $ttpkg =~ s[::][/]g;
    $ttpkg .= '.pm';
    require $ttpkg;
}
else {
    $ttmodule = $TTMODULE;
}

# load custom Template::Directive configuration
map {
  my $v = $ttdirectiveopts{ $_ };
  if( defined $v ) {
    ${ $Template::Directive::{ uc $_ } } = $ttdirectiveopts{ $_ }
  }
} keys %ttdirectiveopts;

# add current directory to INCLUDE_PATH
unshift(@{ $ucttopts->{ INCLUDE_PATH } }, '.');

# read from STDIN if no files specified
push(@ARGV, '-') unless @ARGV;

my $template = $ttmodule->new($ucttopts)
    || die $ttmodule->error();

my %ttvars = ();
if ($ttenvvars) {
    $ttvars{'env'} = \%ENV;
}

# process each input file 
foreach my $file (@ARGV) {
    $file = \*STDIN if $file eq '-';
    $template->process($file,\%ttvars)
	|| die $template->error();
}


sub read_config {
    my $file = shift;

    my $config = AppConfig->new(
        { 
            ERROR  => sub { die(@_, "\ntry `$NAME --help'\n") }
        }, 
        'help|h'      => { ACTION => \&help },
        'template_absolute|absolute' => { DEFAULT => 1 },
        'template_relative|relative' => { DEFAULT => 1 },
        'template_module|module=s',
        'template_anycase|anycase',
        'template_eval_perl|eval_perl',
        'template_load_perl|load_perl',
        'template_interpolate|interpolate',
        'template_pre_chomp|pre_chomp|prechomp',
        'template_post_chomp|post_chomp|postchomp',
        'template_trim|trim',
        'template_variables|variables|define=s%',
        'template_include_path|include_path|include|I=s@',
        'template_pre_process|pre_process|preprocess=s@',
        'template_post_process|post_process|postprocess=s@',
        'template_process|process=s',
        'template_wrapper|wrapper=s',
        'template_recursion|recursion',
        'template_expose_blocks|expose_blocks',
        'template_default|default=s',
        'template_error|error=s',
        'template_debug|debug=s',
        'template_start_tag|start_tag|starttag=s',
        'template_end_tag|end_tag|endtag=s',
        'template_tag_style|tag_style|tagstyle=s',
        'template_compile_ext|compile_ext=s',
        'template_compile_dir|compile_dir=s',
        'template_plugin_base|plugin_base|pluginbase=s@',
        'perl5lib|perllib=s@',
        'template_envvars|envvars',
        'template_directive_debug',
        'template_directive_pretty',
        'tem