#!/usr/bin/perl -i

my($line,$list,$l,@lists,$v1,$v2,$debug,$output);
my($conf) = shift @ARGV;
$debug=0;
if( not $conf or not $ARGV[0] ){
	print STDERR "use: $0 ifhp.conf file\n";
	exit 1;
}
open( CONF, "<$conf") or die "cannot open '$conf'";
while( <CONF> ){
	chomp();
	if( /^# PRINTER / ){
		s/^# PRINTER //;
		if( /^-/ ){
			s/-//;
			$line .= $_;
		} else {
			if( $line ){
			$line =~ s,^(\S*) - (.*)$,<row><entry><literal>$1</literal></entry><entry>$2</entry></row>,;
				$output .= "$line\n";
			}
			$line = $_;
		}
	} elsif( $line and /^\[/ ){
		if( $line !~ /\%/ ){
			print STDERR $line if $debug;
			$line =~ s,^(\S*) - (.*)$,<row><entry><literal>$1</literal></entry><entry>$2</entry></row>,;
			$output .= "$line\n";
			$line = "";
			next;
		}
		@lists = split(' ',$_);
		foreach $list (@lists){
			next if not $list;
			next if $list =~ /\[/;
			next if $list =~ /\]/;
			$l = $line;
			print STDERR "X $list $l\n" if $debug;
			$l =~ s/%/$list/;
			print STDERR "$l\n" if $debug;
			if( $l =~ m,{/(.*)/(.*)/}, ){
				print STDERR "$1, $2\n" if $debug;
				$v1 = $1;
				$v2 = $2;
				eval "\$list =~ s/$v1/$v2/";
				print STDERR "Match '$list'\n" if $debug;
				$l =~ s/{.*}/$list/;
				print STDERR "Final $l\n" if $debug;
			}
			print STDERR "$l\n" if $debug;
			$l =~ s,^(\S*) - (.*)$,<row><entry><literal>$1</literal></entry><entry>$2</entry></row>,;
			$output .= "$l\n";
		}
		$line = "";
	}
}

print STDERR "OUTPUT ". $output if $debug;
$skip = 0;
print STDERR "XX START\n" if $debug;
while( <> ){
	print STDERR "INPUT $_" if $debug;
	if( /SUPPORTED START/ ){
		print;
		print <<'EOF' ;
<table frame=all id=configentries>
<title><literal/ifhp.conf/ Configuration Entries </title>
<tgroup cols=2 colsep=1 rowsep=1 align=left >
<thead><row><entry/Configuration/<entry/Printer Supported/</row></thead>
<tbody>
EOF
		print $output;
		print STDERR "YY $_" if $debug;
		print STDERR "ZZ $output" if $debug;
		print <<'EOF' ;
</tbody>
</tgroup>
</table>
EOF
		$skip = 1;
	} elsif( $skip ){
		$skip = 0 if( /SUPPORTED END/ );
	}
	print if( not $skip );
	if( $debug ){ print STDERR "XX $_" if( not $skip ); }
}
