#! /usr/bin/perl

$license = <<EOT;
menuconfigure 0.01pre -- simple, interactive ./configure frontend
Copyright (C) 2005 Christoph Sommer

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
EOT

print "\n".$license."\n\n";

# --- parse "configure --help" output ---------

print "Running ./configure --help to obtain parameters...\n";

@lines = qx{./configure --help};

if ($? != 0) {
	print "./configure exited with status ".$?.", exiting\n";
	die();
}

$l = join("", @lines);

$shortflag = '(-[A-Za-z_], )?';
$flag = '--([A-Za-z_-]*)';
$param = '((\[=[A-Za-z_-]+\])|(=[A-Za-z_-]+))?';
$text = '(([^\n]*)\n';
$text .= '( +[^ -]([^\n]*)\n)*)';
$regex = '^(\s+'.$shortflag.$flag.$param.'\s+'.$text.')(.*)';

@options = ();
while ($l =~ /$regex/msg) {
	$full = $1;
	$flag = $3;
	$param = $4;
	$text = $7;
	$l = $11;

	next if ($flag eq 'help');
	next if ($flag eq 'enable-FEATURE');
	next if ($flag eq 'disable-FEATURE');
	next if ($flag eq 'with-PACKAGE');
	next if ($flag eq 'without-PACKAGE');

	$text =~ s/\n */ /msg;
	$text =~ s/ +$//;
	$text =~ s/\'/´/;

	$option = {};
	$option->{'flag'} = $flag;
	$option->{'param'} = $param;
	$option->{'text'} = $text;
	$option->{'full'} = $full;
	$option->{'isset'} = 0;
	$option->{'value'} = "";

	push(@options, $option);
}


# --- load saved .config ---------

print "Loading .config...\n";
if (open(F, "< .config")) {
	@lines = <F>;
	$l = join("", @lines);
	$flag = '--([A-Za-z_-]+)';
	$param = "(=([^ ]+))?";
	$regex = $flag.$param."(.*)";
	while ($l =~ /$regex/msg) {
		$flag = $1;
		$value = $3;
		for $option (@options) {
			if ($option->{'flag'} eq $flag) {
				$option->{'isset'}=1;
				$option->{'value'}=$value;
			}
		}
		
		$l = $4;
	}  
}
close(F);


# --- display chooser-dialog until $exit ---------

print "Running dialog...\n";

$save = 0;
$lastItem = "";
while (1) {
	$cmdline = 'dialog';
	$cmdline .= ' --ok-label Select';
	if ($lastItem) {
		$cmdline.= ' --default-item \''.$lastItem.'\'';
		}
	$cmdline .= ' --extra-button --extra-label Save';
	$cmdline .= ' --menu \'Available Options:\' 0 0 0';
	for $option (@options) {
		my $label = $option->{'flag'};
		my $value = '['.($option->{'isset'}?"X":" ").']';
		if ($option->{'param'}) {
			$value .= " ".($option->{'value'}?$option->{'value'}:'(default)');
		}
		$cmdline .= ' \''.$label.'\' \''.$value.'\'';
	}

	$r = qx{$cmdline 3>&1 1>&2 2>&3 3>&-};

	if ($? == 768) {
		$save = 1;
		last;
	}
	if ($? != 0) {
		$save = 0;
		last;
	}
	
	for $option (@options) {
		if ($option->{'flag'} eq $r) {
			$lastItem = $option->{'flag'};

			if ($option->{'param'} eq "") {
				$cmdline = 'dialog';
				$cmdline .= ' --no-collapse --cr-wrap';
				$cmdline .= ' --ok-label Yes --cancel-label No';
				$cmdline .= ' --yesno \''.$option->{'flag'}.': '.$option->{'text'}.'\' 0 0';
			} else {
				$cmdline = 'dialog';
				$cmdline .= ' --ok-label Yes --cancel-label No';
				$cmdline .= ' --extra-button --extra-label Browse';
				$cmdline .= ' --inputbox \''.$option->{'flag'}.''.$option->{'param'}.': '.$option->{'text'}.'\' 0 0 \''.$option->{'value'}.'\'';
			}

			$r = qx{$cmdline 3>&1 1>&2 2>&3 3>&-};

			if ($? == 768) {
				$cmdline = 'dialog';
				$cmdline .= ' --ok-label Yes --cancel-label No';
				$cmdline .= ' --fselect \''.$option->{'value'}.'\' 0 0';
				$r = qx{$cmdline 3>&1 1>&2 2>&3 3>&-};
			}

			$option->{'isset'}=($? == 0);
			$option->{'value'}=$r;
		}
	}
}


# --- build configure command line ---------

$cmdline = './configure';  
for $option (@options) {
	if ($option->{'isset'}) {
		$cmdline .= ' --'.$option->{'flag'};
		if ($option->{'value'} ne '') {
			$cmdline .= '='.$option->{'value'}.'';
		}
	}
}


# --- save and display cmdline, then quit ---------

print "\n".$license."\n\n";

if ($save) {
	print "saving to .config:\n";
	open(F, "> .config") or die "Can't open .config for writing: $!";
	print(F $cmdline."\n");
	close(F);
}

print "\n".$cmdline."\n\n";


# --- EOF; here be dragons ---------

