#!/usr/local/bin/perl
# config.cgi
# Display a form for editing the configuration of a module.
# Each module has a number of configurable parameters (stored in the file
# config.txt in the module directory). Descriptions and possible values for
# each option are stored in the file config.info in the module directory.
# Each line of config.info looks like
# name=desc,type[,options]
#  desc - A description of the parameter
#  type - Possible types (and options) are
#	0 - Free text
#	1 - One of many (options are possibilities)
#	2 - Many of many (options are possibilities)
#	3 - Optional free text
#	4 - Like 1, but uses a pulldown menu
#	5 - User name
#	6 - Group name
#	7 - Directory
#	8 - File
#	9 - Multiline text
#	10 - Like 1, but with free text option
#	11 - Section header
#	12 - Password free text

require './web-lib.pl';
&init_config();
$m = $ARGV[0];
%access = &get_module_acl(undef, $m);
$access{'noconfig'} &&
	&error($text{'config_ecannot'});
%module_info = &get_module_info($m);
&header($text{'config_title'}, "", undef, 0, 1);
print "<center><font size=+2>",&text('config_dir', $module_info{'desc'}),
      "</font></center>\n";
print "<hr>\n";

print "<form action=\"config_save.cgi\">\n";
print "<input type=hidden name=module value=\"$m\">\n";
print "<table border>\n";
print "<tr $tb> <td><b>",&text('config_header', $module_info{'desc'}),
      "</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";
&read_file("$config_directory/$m/config", \%config);

if (-r "$m/config_info.pl") {
	# Module has a custom config editor
	&foreign_require($m, "config_info.pl");
	&foreign_call($m, "config_form", \%config);
	}
else {
	# Use config.info to create config inputs
	&read_file("$m/config.info", \%info, \@info_order);
	%einfo = %info;
	foreach $o (@lang_order_list) {
		&read_file("$m/config.info.$o", \%info, \@info_order);
		}
	@info_order = &unique(@info_order);

	foreach $c (@info_order) {
		@p = split(/,/, $info{$c});
		@ep = split(/,/, $einfo{$c});
		if (scalar(@ep) > scalar(@p)) {
			push(@p, @ep[scalar(@p) .. @ep-1]);
			}
		if ($p[1] == 11) {
			print "<tr><td colspan=3 $tb>\n" ;
			print "\t<b>" . $p[0] . "</b>\n</td></tr>\n" ;
			next;
			}
		print "<tr> <td valign=top><b>$p[0]</b></td>\n";
		print "<td nowrap>\n";
		$qw = $config{$c} =~ /'/ ? '"' : "'";
		if ($p[1] == 0) {
			$size = $p[2] ? "size=$p[2]" : "size=40";
			$max = $p[3] ? "maxlength=$p[3]" : "";
			print "<input name=\"$c\" $size $max value=$qw$config{$c}$qw>\n";
			}
		elsif ($p[1] == 1) {
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=radio name=\"$c\" value='$1' %s>\n",
					$config{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				}
			}
		elsif ($p[1] == 2) {
			local %sel;
			map { $sel{$_}++ } split(/,/, $config{$c});
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=checkbox name=\"$c\" value='$1' %s>\n", $sel{$1} ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				}
			}
		elsif ($p[1] == 3) {
			$none = $p[2] ? $p[2] : $text{'config_none'};
			printf "<input type=radio name=\"%s_none\" value=1 %s> $none\n",
				$c, $config{$c} eq "" ? "checked" : "";
			print "&nbsp;&nbsp;\n";
			printf "<input type=radio name=\"%s_none\" value=0 %s>\n",
				$c, $config{$c} eq "" ? "" : "checked";
			print "<input name=\"$c\" size=20 value=$qw$config{$c}$qw>\n";
			}
		elsif ($p[1] == 4) {
			print "<select name=\"$c\">\n";
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<option value=\"$1\" %s>$2\n",
					$config{$c} eq $1 ? "selected" : "";
				}
			print "</select>\n";
			}
		elsif ($p[1] == 5) {
			if ($p[2]) {
				printf
				   "<input type=radio name=${c}_def value=1 %s>$p[2]\n",
				   $config{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $config{$c} eq "" ? "" : "checked";
				}
			print &unix_user_input($c, $config{$c});
			}
		elsif ($p[1] == 6) {
			if ($p[2]) {
				printf
				   "<input type=radio name=${c}_def value=1 %s>$p[2]\n",
				   $config{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $config{$c} eq "" ? "" : "checked";
				}
			print &unix_group_input($c, $config{$c});
			}
		elsif ($p[1] == 7) {
			print "<input name=\"$c\" size=40 value=$qw$config{$c}$qw> ",
			      &file_chooser_button($c, 1);
			}
		elsif ($p[1] == 8) {
			print "<input name=\"$c\" size=40 value=$qw$config{$c}$qw> ",
			      &file_chooser_button($c, 0);
			}
		elsif ($p[1] == 9) {
			local $cols = $p[2] ? $p[2] : 40;
			local $rows = $p[3] ? $p[3] : 5;
			local $sp = $p[4] ? eval "\"$p[4]\"" : " ";
			print "<textarea name=\"$c\" rows=$rows cols=$cols>",
				join("\n", split(/$sp/, $config{$c})),
				"</textarea>\n";
			}
		elsif ($p[1] == 10) {
			local $fv = $config{$c};
			for($i=2; $i<@p; $i++) {
				($p[$i] =~ /^(\S*)\-(.*)$/) || next;
				printf "<input type=radio name=\"$c\" value=\"$1\" %s>\n",
					$config{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				$fv = undef if ($config{$c} eq $1);
				}
			printf "<input type=radio name=\"$c\" value=free %s>\n",
				$fv ? "checked" : "";
			if ($p[$#p] !~ /^(\S*)\-(.*)$/) {
				print $p[$#p],"\n";
				}
			print "<input name=\"${c}_free\" value='$fv'>\n";
			}
		elsif ($p[1] == 12) {
			print "<input type=radio name=\"${c}_nochange\" value=1 checked> $text{'config_nochange'}\n";
			print "<input type=radio name=\"${c}_nochange\" value=0> $text{'config_setto'}\n";
			$size = $p[2] ? "size=$p[2]" : "size=40";
			$max = $p[3] ? "maxlength=$p[3]" : "";
			print "<input name=\"$c\" type=password $size $max>\n";
			}
		print "</td> </tr>\n";
		}
	}
print "</table></td></tr></table>\n";
print "<input type=submit value=\"$text{'save'}\"></form>\n";
print "<hr>\n";
&footer("/$m", $text{'index'});

