#!/usr/bin/perl
die("I only understand docbook\n") unless @ARGV && $ARGV[0] eq 'docbook';
#my $ii = '//';
#my $io = '//';
#my $si = '**';
#my $so = '**';
my $ii = '';
my $io = '';
my $si = '';
my $so = '';
while() {
chomp;
my $in = '';
my $out = '';
s/^\s+//;
s/\s+$//;
if (/^(.*)(\s*\/\*.*?\*\/\s*?)$/) {
$out = $2;
$_ = $1;
}
if (/^(my\s+)(.*?)$/) {
$in = $1;
$_ = $2;
}
if (/(?$1<-I>/g;
} else {
s/(?<=[^a-zA-Z_\&:\.\'\";])(?!solv\W|Solv\W|Pool\W)([\$\@a-zA-Z_][a-zA-Z0-9_]*)(?=[^a-zA-Z0-9_\(;\[])(?!::)(?! [^=])/<-S>$1<-I>/g;
}
# fixup for perl bare words
s/{<-S>([a-zA-Z_][a-zA-Z0-9]*)<-I>}/{$1}/g;
# fixup for callbackfunctions
s/\\(&[a-zA-Z_]+)/\\<-S>$1<-I>/;
# fixup for stringification
s/\$<-S>/<-S>\$/g;
# fixup for %d
s/%<-S>d<-I>\"/%d\"/;
s/%<-S>d<-I>\\<-S>n<-I>/%d\\n/;
# iterators
s/^ //;
s/ $//;
s/^(for (?:my )?)(\S+) /$1<-S>$2<-I> /;
}
$_ = "$_<-S>";
s/(\s*)<-S>/$1/g;
s/<-S>(\s*)/$1/g;
s/(\s*)<-I>/$1/g;
s/<-I>(\s*)/$1/g;
s/(\s+)/$1/g;
s/(\s+)<-S>/<-S>$1/g;
s/(\s+)/$1/g;
s/(\s+)<-I>/<-I>$1/g;
s//$si/g;
s/<-S>/$so/g;
s//$ii/g;
s/<-I>/$io/g;
print "$in$_$out\n";
}