#!/usr/local/bin/perl
# 
# texcat: TeX ファイルの {\input|\include} を考慮した連結．
# 
#     [1995/06/04] OSHIRO Naoki.
#     [1996/10/02] OSHIRO Naoki. Comment ignore option (-i).
#     [1998/09/28] OSHIRO Naoki. Fix recursive \input/\include.
#       Changed behaviour file open from system() to internal subroutine.
#     [1999/01/29] OSHIRO Naoki. Add message for file open error.
#                                Fix $cur_filehandle initialization.
# 
#     $Log:$
#

#
# Usage: texcat [-i] tex-top-main-file.tex > foo.tex
#

require "getopts.pl";
&Getopts('i'); # i:comment ignore

$cur_filehandle="fh00";
foreach $i (@ARGV) {
    &texcat($i);
}

sub texcat {
    local($f)=@_;
    local($fh, $catfile);

    $fh=$cur_filehandle++;
    if (!open($fh, "$f")) {
	print STDERR "texcat: Warning: Not found `$f'.\n";
	print "% texcat: Warning: Not found `$f'.\n";
	return;
    }
    print "% texcat: $f >>>\n";
    while (<$fh>) {
	next if ($opt_i && /%.*\\(input|include)/);
	if (!/\\(input|include){(.+)}/) {
	    print;
	    next;
	}
	$catfile=$2;
	$catfile.=".tex" unless ($catfile=~/\.tex/);
	&texcat($catfile);
    }
    print "% texcat: $f end.\n";
    close($fh);
}
# major-mode: perl
