#!/usr/local/bin/perl -- # -*- Perl -*-

#
# texfix: texfix.c の perl 版
#
#    Jun.27,'95. N.Oshiro.
#

$jdvi2kps=1;

$pagecom="%%Page:";
$bsp="@beginspecial";
$esp="@endspecial";

$bop=($jdvi2kps)? "@bop0":"BP";
$eop=($jdvi2kps)? "@eop0":"EP";

while (<>) {
    if (/$pagecom/) {
	print;
	last if (&pageout);
    } else {
	print;
    }
}

sub pageout {
    local($fflag);
    while (<>) {
	if (/$bop/) {
	    print;
	    $fflag=1;
	} elsif (/$eop/) {
	    print @tmp;
    	    print;
	    return 0;
	} elsif (/^$bsp$/) {
	    print;
    	    return 1 if (&specialout)
	} elsif ($fflag) {
	    push(@tmp,$_);
	} else {
	    print;
	}
    }
    return 1;
}

sub specialout {
    local($inspecial);
    if (/$bsp$/) {
	print;
	$inspecial++;
    } elsif (/^$esp$/) {
	print;
	return 0 if (--$inspecial==0);
    } else {
	print;
    }
    return 1;
}
