#!/usr/bin/perl 
# Test that every configuration option is documented in 'man gnump3d.conf'.
#
#  (This is a scary test!)
#

#
#  A hash of keys which don't need to be documented.
#
my %EXCEPTIONS;
$EXCEPTIONS{ "plugin_bug" }      = 1;
$EXCEPTIONS{ "plugin_last" }     = 1;
$EXCEPTIONS{ "plugin_playlist" } = 1;
$EXCEPTIONS{ "plugin_search" }   = 1;
$EXCEPTIONS{ "plugin_stats" }    = 1;
$EXCEPTIONS{ "plugin_theme" }    = 1;
$EXCEPTIONS{ "plugin_now" }      = 1;


my $configFile = "../etc/gnump3d.conf";

open( CONFIG, "<$configFile" ) or die "Cannot read configuration file: $!";
my @lines = <CONFIG>;
close( CONFIG );

my %KEYS;

foreach my $line ( @lines )
{
    chomp( $line );

    if ( $line =~ /([^=]+)=(.*)/ )
    {
	my $key = $1;
	my $val = $2;

	if ( $key =~ /^#[ \t]+(.*)/ )
	{
	    $key = $1;
	}

	# Strip leading and trailing whitespace.
	$key =~ s/^\s+//;
	$key =~ s/\s+$//;
	$val =~ s/^\s+//;
	$val =~ s/\s+$//;

	if ( $key =~ /^#(.*)/ ) { $key = $1; }

	# Skip keys starting with '$' - this is a bug in the script.
	next if ( $key =~ /^\$/ );

	$KEYS{ $key } = $val;
    }
}


#
#  Read in our man page
#
open( MANPAGE, "../man/gnump3d.conf.1" ) or die "Cannot open manpage: $!";
my @DOCUMENTATION = <MANPAGE>;
close( MANPAGE );

#
#  Remove any exceptions we might have.
#
foreach my $undocumented (keys ( %EXCEPTIONS ) )
{
    delete( $KEYS{ $undocumented } );
}

#
#  Now that we have a line test that the man page expects it.
#
my $failed = 0;
foreach my $key (sort( keys( %KEYS ) ) )
{
    my $found = 0;
    foreach my $line ( @DOCUMENTATION )
    {
	if ( $line =~ /$key/ ) 
	{
	    $found += 1;
	}
    }

    if ( $found eq 0 )
    {
	$failed += 1;
	print "Key '$key' not documented\n";
    }
}

if ( $failed )
{
    exit 1;
}

exit 0;
