#! /usr/bin/perl -w
# $Id$
#
# Run doxygen with a default configuration file for YaST
# but allow overriding some values


=head1 NAME

ydoxygen - Runs doxygen as configured for YaST.

=head1 SYNOPSIS

 ydoxygen -h|--help|--man
 ydoxygen [-d] [-t <template>] [<param>=<value> ...]

=head1 OPTIONS AND ARGUMENTS

=over

=item B<-h>

Show this help screen

=item B<-t> I<file>, B<--template>=I<file>

Use a template file different from <y2datadir>/devtools/Doxyfile

=item B<--prefix>=I<dir>

Use this prefix for the template, don't deduce it from the script's path.

=item B<-d>, B<--debug>

Show what is substituted.
Keep doxygen.conf.

=item I<param>=I<value>

Replace a parameter in the template configuration file.
Setting PROJECT_NAME also sets GENERATE_TAGFILE to $PROJECT_NAME.tag

ydoxygen sets HTML_FOOTER to .../footer-notimestamp.html to avoid
unnecessary rebuilds. To re-enable the default, pass "HTML_FOOTER="
(bnc#474281).

=back

=cut

use strict;

use Pod::Usage;
use Getopt::Long;

my $help = 0;
my $man = 0;
my $debug = 0;
# prefix is an intermediate to find the template
my $prefix = "";
my $template;

sub debug
{
    print STDERR @_ if ($debug);
}

# parse command line
# - options
Getopt::Long::Configure ("bundling");
GetOptions (
	    "help|h" => \$help,
	    "man" => \$man,

	    "prefix=s" => \$prefix,
	    "template|t=s" => \$template,
	    "debug|d" => \$debug,
	   ) or pod2usage (2);
pod2usage (1) if $help;
pod2usage (-exitstatus => 0, -verbose => 2) if $man;

# compute paths
# - if the prefix is not given, thake the same that this script has
if ($prefix eq "")
{
    $prefix = $0;
    $prefix =~ s:/lib[^/]*/YaST2/bin/[^/]+::;
    # if we were not called with an absolute path name, take the default prefix
    if ($prefix =~ m:^[^/]:)
    {
	$prefix = "/usr";
    }
}
my $y2datadir = "${prefix}/share/YaST2/data";
# - if the teplate is specified, use it, otherwise deduce from the prefix
$template ||= "${y2datadir}/devtools/Doxyfile";


# - arguments
my %replacements = ();

$replacements{"HTML_FOOTER"} = "${y2datadir}/devtools/footer-notimestamp.html";

foreach (@ARGV)
{
    my ($param, $value) = split ('=', $_, 2);
    $replacements{$param} = $value;
    if ($param eq "PROJECT_NAME")
    {
	$replacements{"GENERATE_TAGFILE"} = "$value.tag";
    }
    print "'$param'='$value'\n" if $debug;
}

# open input file
open (IN, "<$template") or die "Cannot open $template: $!";

# open output
my $conf = "doxygen.conf";
open (OUT, ">$conf") or die "Cannot open $conf for writing: $!";

# $_ holds the whole definition of a parameter
# that may be split over multiple lines

# a single physical line
my $single;

# process input, passing it through, substituting what we want
while (defined ($single = <IN>))
{
    chomp $single;		# strip \n

    $_ .= $single;
    debug "\$s $single\n";
    debug "\$_ $_\n";

    # check if this line continues (by trying to cut the continuation mark off)
    if (s/\\$//)
    {
	debug "SKIP\n";
	next;
    }

    if (m/^([A-Z_]+)(\s*)=\s*(.*)$/)
    {
	my ($param, $ws, $value) = ($1, $2, $3);
	debug "MATCH:$param:$ws:$value\n";

	if (defined ($replacements{$param}))
	{
	    $value = $replacements{$param};
	    debug "SUBST:$value\n";
	}

	$_ = "$param$ws= $value";
    }
    print OUT "$_\n" or die "Cannot write to $conf: $!";
    $_ = "";			# this line does not continue
}

close(OUT) or die "Cannot close $conf: $!";
close(IN);

# config is ready, run it:
my $doxygen = "doxygen";
print "$doxygen $conf\n";
system($doxygen, $conf);

if ($? == -1) {
    die "Cannot run $doxygen: $!";
}
elsif ($? & 127) {
    my $s = $? & 127;
    die "$doxygen died with signal $s";
}
elsif ($? != 0) {
    my $e =  $? >> 8;
    die "$doxygen exited with value $e, see doxygen.log";
}

unlink $conf unless $debug;
