# Perl Script
my($line,$list,$l,@lists,$v1,$v2,$debug,$output);
my($conf) = $ARGV[0];
$debug=0;
if( not $conf ){
	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 ){
				$output .= "### $line\n";
			}
			$line = $_;
		}
	} elsif( $line and /^\[/ ){
		if( $line !~ /\%/ ){
			print STDERR $line if $debug;
			$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;
			$output .= "### $l\n";
		}
		$line = "";
	}
}
close CONF;

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 $output;
		print STDERR "YY $_" if $debug;
		print STDERR "ZZ $output" if $debug;
		$skip = 1;
	} elsif( $skip ){
		$skip = 0 if( /SUPPORTED END/ );
	}
	print if( not $skip );
	if( $debug ){ print STDERR "XX $_" if( not $skip ); }
}
