#!/usr/local/bin/perl
#############################################################################
#
# File:		htmlpp
# Purpose:	HTML preprocessor.  Runs files of the form *.html.cpp
#		through the C preprocessor (cpp) and produces files of
#		the form *.html.
# Calling
# Sequence:	htmlpp [options] input_files 
#
# Parameters:	[options] an optional list of option flags to be passed
#		to the C preprocessor (cpp).  Options lead with a "-"
#		character and have no embedded spaces.  Options are passed
#		verbatim to cpp after the initial options specified in
#		$cpp_command.
#
#		input_files is a list of filenames in the form *.cpp.  Each
#		input file will be processed through cpp and writen to an
#		output file corresponding to *.  For example, mypage.html.cpp
#		will be processed, and written out to mypage.html.
#
# Author:	Tom Parris
#
############################################################################

############################################################################
# default cpp options.
#	-C 	do not strip C stule comments
#	-P 	do not add line control information normally passed to the C
#	   	compiler.
#	-undef	undefine all predefined symbols and macros.
############################################################################

############################################################################
# hunt down the executable for cpp.  Location varies on flavor of Unix.
############################################################################

if (-e "/lib/cpp") 
{
	$cpp_command = "/lib/cpp" ;
} 
elsif (-e "/usr/lib/cpp")
{
	$cpp_command = "/usr/lib/cpp -undef" ;
}
else
{
	die "Could not find location of cpp.\n" ;
}

$cpp_command .= " -C -P" ;

############################################################################
# strip off all of the leading options, add them to $cpp_opts
# remember, arguments start with a "-" character and have no embedded blanks
############################################################################
while(($arg = shift(@ARGV)) =~ /^\-/) 
{
	$cpp_command .= " $arg" ;
}

#we've shifted one too many off of @ARGV, so put it back on
unshift(@ARGV,$arg) ;

############################################################################
# the rest of the arguments should be file names in the form *.cpp
############################################################################
foreach $file (@ARGV)
{
	if ( $file !~ /(.*)\.htmlpp$/ )
	{
		die "\"$file\" does not end with .htmlpp!\n" ;
	}

	# the output file for $file is $file.html
	$output_files{$file} = "$1.html" ;
}

############################################################################
# open up a pipe to a csh session and process each of the files
############################################################################
open(CSH,"|csh") || die "Could not open pipe to csh: $!\n" ;
foreach $file (@ARGV)
{
	$command  = "touch $output_files{$file} >& /dev/null; ";
	$command .= "chmod 664 $output_files{$file}; ";
	$command .= "hideq.pl $file | " ;
#	$command .= "wrapper.pl | " ;
	$command .= "$cpp_command | " ;
#	$command .= "wrapper.pl | " ;
	$command .= "unhideq.pl >! $output_files{$file}\n" ;
	print "$command" ;
	print CSH $command ;
}

close(CSH) ;
