#!/usr/bin/perl

################################################################################
#
# File: $Id$
#
# When the derived XML source for a document we include the contents of
# all content files identified in an <@INC[]@> include tag. However, we
# can't just read all the tags willy-nilly, because some of them may be
# commented out. Commented out include tags should be ignored, because
# they may contain XML comments themselves, and nested XML comments are a
# no-no. This script solves the problem by simply stripping out all XML
# comments. This should have the side benefit of making the overall build
# process faster, because the XSLT processor doesn't have to process the
# comments.
#
################################################################################

use strict;

my $skip = 0;
my $paper_root = shift;
my $infilename = shift;

open INFILE,"<$infilename";

while (<INFILE>)
{
	# Switch skip mode on and off as we encounter open (<!--)
	# and close (-->) XML comment markers.
	$skip = 1 if /<!--/;
	$skip = 0 if /-->/;
	
	# Ignore everything while in skip mode.
	next if $skip || /-->/;
	
	# Otherwise, print stuff.
	if (/^(.*)<\@INC\[([^]]+)\]@>(.*)$/)
	{
		print "$1\n" . `cat $paper_root/$2` . "\n$3\n";
	}
	else
	{
		print;
	}
}

close INFILE;
