#!/usr/bin/perl
#
# Globals
#
use vars qw/$deftext %opt /;

#
# Command line options processing
#
sub init()
{
	use Getopt::Std;
	my $opt_string = 'hpci:o:t:j:';
	getopts( "$opt_string", \%opt ) or usage();
	usage() if $opt{h};
}

#
# Message about this program and how to use it
#
sub usage()
{
	print STDERR << "EOF";

touchhtml -- Create a barebones HTML file

usage: $0 [-hpc][-i <FILE>][-o <FILE>][-t <TITLE>]

 -h        : This (help) message
 -p        : Add <pre> tags 
 -c	   : Print "Content-type" at the start (for CGI)
 -i FILE   : Read body from given filename 
 -o FILE   : Output to given filename 
 -t TITLE  : Set the HTML <TITLE> field to given title
 -j FILE   : Include this JavaScript filename in the body

EOF
	exit;
}

init();


if (!$opt{o}) {
	open(FILEH, ">-");
}
else
{
	open(FILEH, ">$opt{o}") || die "Cannot open file for writing!";
}

if ($opt{c}) {
	print "Content-type: text/html\n\n";
}

print FILEH <<FOO;
<HTML>
<HEAD>
<TITLE>$opt{t}</TITLE>
</HEAD>
<BODY>
FOO
print FILEH "<pre>\n" if $opt{p};
if ($opt{i})
{
	open (INFILE, "<$opt{i}") || die "Cannot open file for reading!";
	while (<INFILE>)
	{
		print FILEH;
	}
	close(INFILE);
}
elsif (!(-t STDIN))
{
	while (<STDIN>)
	{
		print FILEH;
	}
}
print FILEH "</pre>\n" if $opt{p};
print FILEH "<script src='$opt{j}'>\n</script>\n" if $opt{j};
print FILEH <<FOO;
</BODY>
</HTML>
FOO

close(FILEH);
