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