#!/usr/bin/perl
use strict;
use IO::File;
use Getopt::Std;
use vars qw($opt_c);

# A tiny configure script

getopts ("c");
our $CMDLINE = defined $opt_c;

my $config = "config.h";
my $f;

if ($CMDLINE) {
    $f = new IO::File ("> /dev/stdout");
}
else {
    $f = new IO::File ("> $config");
}

if (not defined $f) { 
    print STDERR "* Could not open $config for writing!\n";
    exit 1;
}

my %CONF_SETTINGS = (
    "check debian-ness" => \&CheckDebian,
);

print $f "#ifndef CONFIG__H\n#define CONFIG__H\n\n";
foreach my $key (keys %CONF_SETTINGS) { 
    my $func = $CONF_SETTINGS {$key};
    my $string = &$func ();
    print $f "/* $key */\n$string\n\n";
}
print $f "\n#endif // CONFIG__H\n";
close ($f);

sub CheckDebian {
    my $grep = `cat /etc/issue | egrep 'Ubuntu|Debian'`;
    chomp ($grep);

    if ($grep ne "") { 
	return "#define DEBIAN";
    }
    else {
	return "#undef DEBIAN";
    }
}
