#!/usr/pkg/bin/perl
#####
# NAME
#	add-html-signature - insert "signature" file into the HTML file
# VERSION
#	$Id$
# CHANGELOG
#	$Log$
# SYNOPSYS
#	add-html-signature source-file signature-file
# DESCRIPTION
#  This utility is used to insert custom signature file to the HTML file.
#  This application output the content of "source-file" and "signature-file". 
#  The content of "signature-file" is inserted before </BODY> tag in 
#  source-file.
# COPYRIGHT
#  Copyright (C) 2000,2001 Steel Wheels Project.
#  This file is a apart of the papaya utilities. 
#  If you need the information of copyright of this file, see COPYING
#  file distributed with file or see http://www.asahi-net.or.jp/~em7t-hmd
#  web page.

# include papaya library
$this = "add-html-signature" ;
$prefix = "/usr/pkg" ;
$exec_prefix = "${prefix}" ;

main:{
	local($srcname, $signame) ;
	local($srcline, $sigline) ;

	# check number of arguments
	if($#ARGV != 1){
		&usage ;
	}

	# open source file
	if(($srcname = $ARGV[0]) eq '-'){
		# use standard input
	} elsif( -f $srcname ){
		close(STDIN) ;
		open(STDIN, "<$srcname") || 
		  &error("can not read source file \"$srcname\"") ;
	} else {
		&error("can not open source file \"$srcname\"") ;
	}

	# open signature file
	if( -f ($signame = $ARGV[1]) ){
		open(SIGFD, "<$signame") ||
			die "can not happen(2)\n" ;
	} else {
		&error("can not read signature file \"$signame\"") ;
	}

	# print source file until the line which include "</BODY> tag
	while($srcline = <STDIN>){
		if($srcline =~ /<\/BODY>/){
			last ;
		}
		print $srcline ;
	}
	if($srcline eq ''){
		&warning("</BODY> tag is not found.\n") ;
	}

	# print signature
	while($sigline = <SIGFD>){
		print $sigline ;
	}

	# print rest of source file
	while($srcline){
		print $srcline ;
		$srcline = <STDIN> ;
	}

	# close input, signature files
	close(SIGFD) ;
	close(STDIN) ;
	exit 0 ;
}

sub 	error {
	local($msg) = @_ ;
	die "$this [ERROR] $msg\n" ;
}

sub 	warning {
	local($msg) = @_ ;
	print STDERR "$this [WARNING] $msg\n" ;
}

sub	usage {
	print STDERR "[usage] add-html-signature srcfile signature-file\n" ;
	exit 1 ;
}

