#!/usr/local/bin/perl
# texar.pl					22 Apr 94
#------------------------------------------------------------

#
# texar  ---  make a LaTeX2e archive
#
# (history at end)


# SYNOPSIS
#
#	texar document file ...
#
# Adds all 'file's to 'document'. The latter is expected to be a
# LaTeX2e document and must have a \documentclass tag in the first
# column. The files are inserted in a filecontents environment just
# before the documentclass tag.
#
# Thereby we get a self-contained document. The result is written to
# stdout.


# standard setup

$0 =~ s#.*/##;                          # basename of command
chop( $date = `date -u` );		# a handy constant


sub usage
{
    print STDERR "usage: $0 document file ...\n";
    exit (1);
}


#
# start of code
#

{

# check arguments
# store them in variables for better naming

    if ( $#ARGV <= 0 ) {
        do usage ();
    }

    $document = shift (@ARGV);


# open the document, print the text until \documentclass

    open (DOC, "<$document")
	||  die "$0: $document: $!.\n";

    while ( $doc_line = <DOC> ) {
	last  if ( $doc_line =~ /^\\documentclass/ );
	print $doc_line;
    }
    if ( eof(DOC) ) {
	print STDERR "$0: No \\documentclass tag in first column.\n";
	exit (2);
    }


# insert the other files, explain that one did it.

    print <<_EOT_ ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%	start of automatically included files,
%	inclusion done at $date.
%

_EOT_

    foreach $file ( @ARGV ) {
	open (FILE, "<$file")
	    || die "$0: $file: $!.";

	print "\\begin{filecontents}{$file}\n";
	while ( <FILE> ) {
	    print;
	}
	print "\\end{filecontents}\n\n";

	close (FILE);
    }

    print <<_EOT_ ;
%
%	end of automatically included files
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



_EOT_


# print rest of document

    print $doc_line;			# with \documentclass

    while ( <DOC> ) {
	print;
    }


# finished...

    exit (0);
}


#======================================================================
#
# Log:
#
# 22 Apr 94 js  Initial revison
