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

#
# decomment-tex  ---  throw away comment lines in a TeX file
#
# (history at end)


#
# SYNOPSIS
#
# 	decomment-tex file [original]
#
# All comment and empty lines from 'file' (stdin if `-') are
# discarded. The resulting file is output to stdout.
#
# The output is prepended by a comment that explains that this is not
# the original file. A hint to the original file is included. If
# 'original' is not empty, it must be a text that can be appended to
# `may be retrieved'. If 'original' is empty, a hint is added that
# it's not available.
#     Within 'original', \n is transformed into a new line. If you
# want `\n', you must write `\\n'.


# standard setup

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


sub usage
{
    print STDERR "usage: $0 file [original]";
    exit (1);
}


#
# start of code
#

{

# check arguments
# store them in variables for better naming

    if ( $#ARGV == 1 ) {
        $original = "\
% The original file may be retrieved
% $ARGV[1].";
	$original =~ s/([^\\])\\n/\1\n% /g; # transform \n to new line
	$original =~ s/\\\\n/\\n/g;	# transform \\n to \n
        pop (@ARGV);
    } else {
	$original = "\
% Sorry, but the original file is not available for distribution.";
    }

    if ( $#ARGV == 0 ) {
	$file = $ARGV[0];
    } else {
	do usage ();
    }

# open file

    if ( $file eq '-' ) {
	$file = '<stdin>';
    } elsif ( open (STDIN, "<$file") ) {
	$file =~ s:.*/::;		# only base name
    } else {
	print STDERR "$0: $file: $!";
	do usage ();
    }

# print header and filter

    print <<_EOT_ ;
% This is a stripped version of $file,
% all comment lines and empty lines were discarded to save space. YOU
% ARE NOT ALLOWED to use this file for your own work. You may ONLY
% redistribute it as part of the complete distribution you received it.
% This file was created at $date.
$original

% ======================================================================
_EOT_

    while ( <> ) {
	chop;				# discard newline
	next  if ( /^\s*$/  ||  /^%/ );	# empty lines and TeX comment lines
	print;				# other lines
    }


# finished...

    exit (0);
}


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