1#!@PERL@
2use strict;
3for (@ARGV){
4	my($hfile,$cfile,$s,$t,$h);
5	#print "arg '$_'\n";
6	$hfile = $_;
7	($cfile = $hfile) =~ s/.*\///;
8	$cfile =~ s/\.h$/.c/;
9	if( not -f $cfile ){
10		my @files = glob( "*/$cfile" );
11		if( @files > 1 ){
12			warn "too many matching sourc3 files - @files\n";
13			exit 1;
14		}
15		if( @files == 0 ){
16			warn "no matching source files\n";
17			next;
18		}
19		$cfile = $files[0];
20	}
21	#print "cfile '$cfile', hfile '$hfile'\n";
22	if( !open( CFILE, "<$cfile") ){
23		warn "cannot open '$cfile'";
24		next;
25	}
26	while (<CFILE>) {
27		chomp;	# strip record separator
28		if (/^[A-Za-z]/ .. /^{/) {
29			chomp;
30			if( /{/ ){
31				$s .= ";\n";
32				$t .= $s;
33				$s = "";
34			} elsif( $s ){
35				$s .= "\n" . $_;
36			} else {
37				$s = $_;
38			}
39		}
40		if (/VARARGS[0-9]/ .. /^{/) {
41			chomp;
42			if( /{/ ){
43				$s .= "\n;\n";
44				$t .= $s;
45				$s = "";
46			} elsif( $s ){
47				$s .= "\n" . $_;
48			} else {
49				$s = $_;
50			}
51		}
52	}
53	close CFILE ;
54	$t .= "\n#endif\n";
55	#print $t;
56	open( HFILE, "<$hfile") or die "cannot open '$hfile'";
57	while( <HFILE> ){
58		$h .= $_;
59		if( /PROTOTYPE/ ) {
60			$h .= $t;
61			last;
62		}
63	}
64	# print $h;
65	`cp $hfile $hfile.bak`;
66	open( HFILE,">$hfile") or die "cannot open '$hfile'";
67	print HFILE $h;
68	close HFILE;
69}
70