#!/usr/local/bin/perl
#
# change the above as appropriate
#
# $Id: pec2ec,v 1.5 1993/11/23 17:23:08 bill Exp $
#
#  This file is part of the isqlperl system.
#
#  (c) Copyright 1993 by William Hails
#      All rights reserved.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful, 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  You can email me as bill@tardis.co.uk or write to William Hails, CLI
#  Connect Ltd., 19, Quarry Street, Guildford, Surrey, GU1 3UY. England.
#
#
# Quick hack to allow configuring of MAXCURSORS from the Makefile
# (expands case statements etc.)

($progname = $0) =~ s#.*/##;

sub usage {
    die "use: $progname <no-of-cursors> <file>\n";
}

$maxcursors = shift || &usage();
$maxcursors =~ /^\d+$/ || &usage();
$file = shift || &usage();

open(PEC, $file) || die "$file: $!";

print <<EOF;
/* Automatically generated by $progname from $file
 * with MAXCURSORS configured to be $maxcursors
 * PLEASE DO NOT EDIT!
 */

EOF

print qq!#line 1 "$file"\n!;

while (<PEC>) {
    s/MAXCURSORS/$maxcursors/g;

    if (/^INTERVAL/ .. /^END/) {
	if (/^INTERVAL/) {
	    $line = $. + 1;
	    $accum = '';
	} elsif (/^END/) {
	    for ($i = 0; $i < $maxcursors; ++$i) {
		($copy = $accum) =~ s/THISNUMBER/$i/g;
		print qq!#line $line "$file"\n!;
		print $copy;
	    }
	    printf qq!#line %d "$file"\n!, $. + 1;
	} else {
	    $accum .= $_;
	    $accum .= sprintf(qq!#line %d "$file"\n!, $. + 1) if /^\s*\$/;
	}
    } else {
	print;
	do { printf qq!#line %d "$file"\n!, $. + 1; } if /^\s*\$/;
    }
}
