#!/usr/bin/perl
# ptc_glob      *** internal script
#
# @(#)ptc_glob  6.1.2  1995-09-20
#
# make a main program from module with the sizeof-statements made by
# ptoc (option -Y) and execute it
#
use Env;
use File::Basename;

do "$TOOL/bin/toolvars$TOOLEXT";
if ( $@ ) { print "$@"; exit }

$ARGV[0] =~ tr[\\][/];
$Param = $ARGV[0];
push @CCFLAGS, "-I$TOOL/incl";
$FFILE = "s_$Param";
$BASE = basename($Param);
($BASE) = $BASE =~ /(\.*[^\.]*)/;
$PROG = "$BASE.exe";
$SFILE = "$BASE.gsz";

$LC = `echo $SFILE | cut -c6`; chomp $LC;

CASE: {
	if ( $LC =~ /[0-9]/ ) {
		$LC1 =`echo $SFILE | cut -c2,4,5,6`; chomp $LC1;
		$LC2 =`echo $SFILE | cut -c2,3,4,5,6`; chomp $LC2;
		last CASE;
	}
	$LC1 = `echo $SFILE | cut -c2,4,5`; chomp $LC1;
	$LC2 = `echo $SFILE | cut -c2,3,4,5`; chomp $LC1;
}

open(FILE_OUT, ">$FFILE") || die "Can't open $FFILE: $!";
open(PIPE_IN. "$TOOL/pgm/ptc_delbody $Param |");
while(<PIPE_IN>) {
	print FILE_OUT $_ unless /extern/;
}
close(PIPE_IN);
print FILE_OUT "#include <stdio.h>";
print FILE_OUT "main()\n{";
open(FILE_IN, "<$Param") || die "Can't open $Param: $!";
while(<FILE_IN>) {
	next unless m!/\*size!;
	s!/\*size(.*)\*/!$1!;
	print FILE_OUT $_;
}
close(FILE_IN);
print FILE_OUT "return 0;";
print FILE_OUT "}";
close(FILE_OUT);

if ( system("$CC $CCFLAGS -o $PROG $FFILE") ) {

    system("./$PROG | egrep \"$LC1|$LC2|td\"  > $SFILE");
    system("echo                            >>$SFILE");
} else {
	# return 2!
    2;
}

