#!/usr/bin/perl -w
use strict;
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw/:all escapeHTML/;

my $figlet = '/home/i/l/iltzu/figlet22/figlet';
my $fonts = '/home/i/l/iltzu/figlet22/fonts';

#
# Read list of available fonts
#
opendir FONTS, $fonts
    or die "Can't open font directory: $!\n";

my %fonts;
@fonts{grep s/\.flf$// => readdir FONTS} = ();

closedir FONTS
    or die "Error closing font directory: $!\n";

#
# Check parameters
#
param('font' => 'standard')
    unless exists $fonts{param('font') || ''};
param('width' => 80)
    unless (param('width') || 0) > 0;

#
# Generate form
#
print(header(),
      start_html('FIGLet WWW Interface'),
      startform(),
      table(Tr(td('Text:'),
               td({-colspan => 4},
                  textfield(-name => 'text',
                            -size => 60))),
            Tr(td('Font:'),
               td(popup_menu(-name => 'font',
                             -Values => [sort keys %fonts])),
               td('Screen width:'),
               td(textfield(-name => 'width',
                            -size => 5)),
               td({-align => 'right'},
                  submit(-value => 'FIGLetize!')))),
      endform());

#
# FIGLetize input
#
if (defined param('text') and length param('text')) {
    my $pid = open FIGLET, "-|";
    die "Can't fork" unless defined $pid;
    exec($figlet,
         '-d' => $fonts,
         '-f' => scalar param('font'),
         '-w' => scalar param('width'),
         scalar param('text')) unless $pid;
    
    print "<HR>\n<PRE>\n";
    while (<FIGLET>) {
        print escapeHTML($_);
    }
    print "</PRE>\n";
    close FIGLET
        or die "Error running FIGLet: $!\n";
}

print(end_html());

__END__
