#! /usr/bin/perl -w
# $Id: limaldoxygen 367 2005-04-27 23:09:18Z mt $
#
# Run doxygen with a default configuration file for LiMaL
# but allow overriding some values


=head1 NAME

limaldoxygen - Runs doxygen as configured for LiMaL.

=head1 SYNOPSIS

 limaldoxygen -h|--help|--man
 limaldoxygen [-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 <limaldatadir>/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.
Write doxygen.log and 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 and adds the namespace to PREDEFINES.

=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;
my $namespace;

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[^/]*/limal/bin/[^/]+::;
    $prefix =~ s:/bin/[^/]+::;
    # if we were not called with an absolute path name, take the default prefix
    if ($prefix =~ m:^[^/]:)
    {
	$prefix = "/usr";
    }
}
my $limaldatadir = "${prefix}/share/limal";
# - if the teplate is specified, use it, otherwise deduce from the prefix
$template ||= "${limaldatadir}/devtools/Doxyfile";


# - arguments
my %replacements = ();
foreach (@ARGV)
{
    my ($param, $value) = split ('=', $_, 2);
    $replacements{$param} = $value;
    if ($param eq "PROJECT_NAME")
    {
	my $ns_lc = $value;
	   $ns_lc =~ s/^limal-//ig;
	   $ns_lc =~ s/-/_/g;
	my $ns_uc = uc($ns_lc) . '_NAMESPACE';

	$replacements{"+PREDEFINED"} = "\"$ns_uc=$ns_lc\"";
	$replacements{"GENERATE_TAGFILE"} = "$value.tag";
    }
    print "'$param'='$value'\n" if $debug;
}

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

# open output - pipe
my $run_doxygen = "|doxygen -";
if ($debug)
{
    $run_doxygen = "|tee doxygen.conf ". $run_doxygen;
}
open (OUT, $run_doxygen) or die;

# $_ holds the whole definitioin 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";
	}
	elsif( defined ($replacements{"+".$param}))
	{
	    $value = $replacements{"+".$param}." ".$value;
	    debug "SUBST:$value\n";
	}

	$_ = "$param$ws= $value";
    }
    print OUT "$_\n";
    $_ = "";			# this line does not continue
}

close(OUT);
close(IN);
