#!/usr/bin/perl

if (!($file=$ARGV[0])) {
  print "Usage: ssireduce <file>\n";
  exit 1;
}


# Grab in the entire file to $content

open(HTML,$file) or die "Couldn't open $file for reading: $!\n";
$slash=$/; undef $/; $content=<HTML>; close(HTML); $/=$slash;

# Zap SSIs
$content=~s/<!--START:([^>]*)-->.*<!--END:\1-->/<!--#include virtual=\"$1\"-->/sg;
($title)=($content=~m|<TITLE>(.*)</TITLE>|i);

# Throw away everything between the title and the start of the 'real'
# document. We may want to keep the META stuff at some point.

$content=~s|<TITLE>(.*)</TITLE>||isg;

$content=~s|<!--TITLE HERE-->|<TITLE>$title</TITLE>|;

if ($content=~/<!--DOCUMENT STARTS-->/sg) {
  $content=~s/^.*<!--DOCUMENT STARTS-->\n//sg;
} else {
  $content=~s/^(.*?)<!--#inc/<!--#inc/sg;
}

$content=~s/<!--DOCUMENT ENDS-->(.*)$//sg;

$content=~s/\r//sg;

open(OUT, "> $file") or die "Couldn't open $file for writing\n";
print OUT $content;
close(OUT);
