#!/usr/bin/perl -w
#
# chump-transformer: convert chump/churn XML output to HTML
#
# Gerald Oskoboiny, 6 Oct 2003
#
# source: http://impressive.net/people/gerald/2003/10/chump-transformer
#
# $Id: chump-transformer,v 1.1 2003/10/07 02:01:52 gerald Exp $
#

use strict;
use CGI::Carp qw(fatalsToBrowser);

my $xslfile	= '/home/gerald/www/weblogs/fogo/churn_html.xsl';
my $xslfile_mm	= '/home/gerald/www/weblogs/fogo/month_html.xsl';
my $xslfile_rss	= '/home/gerald/www/weblogs/fogo/rss10.xsl';
my $source	= '/home/gerald/www/people/gerald/2003/10/chump-transformer';
my $content_type = 'text/html; charset=iso=8859-1';

my $docroot = $ENV{DOCUMENT_ROOT};
my $req_uri = $ENV{REQUEST_URI};

# if invoked as myself, show my source
if ( $req_uri =~ /chump-transformer$/ ) {
    print "Content-Type: text/plain\n\n";
    open( ME, "< $source" ) or die "unable to open myself: $!";
    while (<ME>) { print };
    close( ME ) or warn "error closing myself: $!";
    exit;
}

# use a different xsl stylesheet for monthly overview pages
if ( $req_uri =~ m,[0-9]{4}/[0-9]{2}/$, ) {
    $xslfile = $xslfile_mm;
}

# use a different xsl stylesheet for RSS feed
if ( $req_uri =~ m,\.rss$, ) {
    $xslfile = $xslfile_rss;
    $content_type = "application/rdf+xml";
}

# default to index.html if no filename given
if ( $req_uri =~ m,/$, ) {
    $req_uri .= "index.html";
}

# source input for foo.html is the corresponding foo.xml
$req_uri =~ s/(html|rss)$/xml/;

print qq{Content-Type: $content_type\n\n};
open( XSLTPROC, "xsltproc $xslfile $docroot$req_uri |" ) or
    die "unable to open pipe to xsltproc: $!";
while (<XSLTPROC>) {
    print;
}
close( XSLTPROC ) or warn "error closing pipe to xsltproc: $!";