#!/usr/bin/perl
# config_info.pl
# configure Services with Tarantella 
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL

require '../web-lib.pl';
do 'global-lib.pl';

&init_config();
$dataConf="dataconf";
$listfcfgs="ordercfgs";
$configValues="sttavalues";
$defaultcfg="defaultcfg";
$scancachecfg="scancache";
$scanlistcfgs="scanlistcfgs";
$now_is=&set_now();

sub config_form
{
  $m = "stta";
  &set_variables_cfg();

  if (!(-d $configDir )) {
        &read_file("$configDef/$defaultcfg", \%sttaconfig);
	# Use config.info to create config inputs
	&read_file("$module_dir/configinit.info", \%info, \@info_order);
	%einfo = %info;
	foreach $o (@lang_order_list) {
		&read_file("$module_dir/configinit.info.$o", \%info, \@info_order);
		}
	@info_order = &unique(@info_order);
        if ( $main::config{'tta_dir'} ne "" ) { 
             print "<tr><td><h2>$text{migrate_title} $text{config_title} <h2></td><tr>\n";
        }
        &set_values_from;
   } else {
	&read_file("$module_dir/config.info", \%infoall, \@info_orderall);
	foreach $o (@lang_order_list) {
		&read_file("$module_dir/config.info.$o", \%infoall, \@info_orderall);
		}
        if ( $fileCFG ne "" ) {
                 %listfcfgs=();
                 @orderfcfgs=();
                 &read_file("$configDir/$listfcfgs", \%listaf);
                 foreach $d (keys (%listaf) ) {
                       if ( $d eq $fileCFG ) {
                            push(@orderfcfgs, $d ); 
                            $listfcfgs{$d}=$listaf{$d};
                            last;
                       }
                 }
        } else {
             &read_file("$configDir/$listfcfgs", \%listfcfgs, \@orderfcfgs);
             print "<table border=0>\n";
             print "<tr><td><ul></td></tr>\n";
             foreach $d (@orderfcfgs) {
                     $item_title=$d."_title";
                     $item_help=$listfcfgs{$d};
                     print "<tr><td valign=center><li><a href=\"#$d\">$text{$item_title}</a></td><td>",&hlink("?","$item_help"),"</td><td align=rigth> - $d&nbsp;</li></td></tr>\n";
             }
             print "</ul></table>\n";
        }
        @orderf_cfgs = @orderfcfgs;
        @orderfcfgs = sort @orderf_cfgs;
        
        foreach $d (@orderfcfgs) {
                %sttaconfig=();
                %info=();
	        @info_order=();
                &read_file("$configDir/$d", \%sttaconfig);
                $item_title=$d."_title";
                $item_help=$listfcfgs{$d};
                print "<tr><td>&nbsp;<b>$text{$item_title}<a name=\"$d\"></b></td></tr>\n"; 
                print "<tr><td align=center><table border>\n";
                print "<tr $tb> <td><b>",&hlink("<b>$text{$item_title}</b>","$item_help"),"</b> - $d&nbsp;</td></tr>\n";
                print "<tr $cb> <td><table width=100%>\n";
                foreach $c (keys (%sttaconfig)) {
                     push(@info_order, $c ); 
                     $info{$c}=$infoall{$c};
                }
                $einfo=%info;
			@info_order = &unique(@info_order);
                &set_values_from();
	        print "</table></table></td></tr>\n";
        }
   }

}

sub config_save
{
  $m = $in{module};
  $fileCFG = $in{fcfg} || "";
  &set_variables_cfg();

#header($text{'config_title'}, "", undef, 0, 1);

   if (!(-d $configDir )) {
        mkdir($configDir, 0777);
        &init_dataconf();
        if ( $main::config{'tta_dir'} ne "" ) {
              &test_minfo("migrate","html");
              system("cat $config_directory/$m/config > $configDir/config.old");
              %main::config=();  # Reset
              $main::config{'MIGRATE'}="$now_is by $remote_user from dataconf/config.old";
        } else {
              %main::config=();  # Reset
        }
        $main::config{'CONFIG'}="look at dataconf directory --- everything is there !!!";
        $main::config{'INSTALL'}="$now_is by $remote_user";
    } else {
	&read_file("$module_dir/config.info", \%infoall, \@info_orderall);
        if ( $fileCFG ne "" ) {
                 %listfcfgs=();
                 @orderfcfgs=();
                 &read_file("$configDir/$listfcfgs", \%listaf);
                 foreach $d (keys (%listaf) ) {
                       if ( $d eq $fileCFG ) {
                            push(@orderfcfgs, $d ); 
                            $listfcfgs{$d}=$listaf{$d};
                            $main::config{$fileCFG}="$now_is by $remote_user";
                            last;
                       }
                 }
        } else {
             &read_file("$configDir/$listfcfgs", \%listfcfgs, \@orderfcfgs);
             $main::config{'UPDATEFULL'}="$now_is by $remote_user";
        }
        foreach $d (@orderfcfgs) {
                %sttaconfig=();
                %info=();
	        @info_order=();
                &read_file("$configDir/$d", \%sttaconfig);
                foreach $c (keys (%sttaconfig)) {
                     push(@info_order, $c ); 
                     $info{$c}=$infoall{$c};
                }
	        @info_order = &unique(@info_order);
                %sttaconfig=();
                &get_values_tosave();
                &unlock_file("$configDir/$d");
                &write_file("$configDir/$d", \%sttaconfig);
                &unlock_file("$configDir/$d");
        }
    }
    &make_scancfg()
}

sub set_variables_cfg
{
     $module_dir="$root_directory/stta";

     $configDir="$config_directory/$m/$dataConf";
     $configDef="$module_dir/$dataConf/defs";
     
     foreach $l (@lang_order_list) {
                     &read_file("$module_dir/lang/$l.cfg", \%text);
                     }
     foreach $k (keys %text) {
             $text{$k} =~ s/\$([A-Za-z0-9\.\-\_]+)/text_subs($1,\%text)/ge;
     }
}

sub make_scancfg
{
    local %scanconfig=();
    local %listfcfgs=();
    local @scanfcfgs=();
    local $d="";

    &read_file("$configDir/$scanlistcfgs", \%listfcfgs, \@scanfcfgs);
    foreach $d (@scanfcfgs) {
          &read_file("$configDir/$d", \%scanconfig);
    }
    &write_file("$configDir/$scancachecfg", \%scanconfig);
}

sub get_values_tosave
{
	foreach $c (@info_order) {
		@p = split(/,/, $info{$c});
		if ($p[1] == 0 || $p[1] == 7 || $p[1] == 8) {
			# Free text input
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 1 || $p[1] == 4) {
			# One of many
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 5 || $p[1] == 6) {
			# User or group
			$sttaconfig{$c} = ($p[2] && $in{$c."_def"} ? "" : $in{$c});
			}
		elsif ($p[1] == 2) {
			# Many of many
			$in{$c} =~ s/\0/,/g;
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 3) {
			# Optional free text
			if ($in{$c."_none"}) { $sttaconfig{$c} = ""; }
			else { $sttaconfig{$c} = $in{$c}; }
			}
		elsif ($p[1] == 9) {
			# Multilines of free text
			local $sp = $p[4] ? eval "\"$p[4]\"" : " ";
			$in{$c} =~ s/\r//g;
			$in{$c} =~ s/\n/$sp/g;
			$in{$c} =~ s/\s+$//;
			$sttaconfig{$c} = $in{$c};
			}
		elsif ($p[1] == 10) {
			# One of many or free text
			if ($in{$c} eq 'free') {
				$sttaconfig{$c} = $in{$c.'_free'};
				}
			else {
				$sttaconfig{$c} = $in{$c};
				}
			}
		}
}
sub set_values_from
{
	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 = $sttaconfig{$c} =~ /'/ ? '"' : "'";
		if ($p[1] == 0 || $p[1] == 12) {
			$size = $p[2] ? "size=$p[2]" : "size=40";
			$max = $p[3] ? "maxlength=$p[3]" : "";
			$type = $p[1] == 12 ? "type=password" : "";
			print "<input name=\"$c\" $size $max $type value=$qw$sttaconfig{$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",
					$sttaconfig{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				}
			}
		elsif ($p[1] == 2) {
			local %sel;
			map { $sel{$_}++ } split(/,/, $sttaconfig{$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, $sttaconfig{$c} eq "" ? "checked" : "";
			print "&nbsp;&nbsp;\n";
			printf "<input type=radio name=\"%s_none\" value=0 %s>\n",
				$c, $sttaconfig{$c} eq "" ? "" : "checked";
			print "<input name=\"$c\" size=20 value=$qw$sttaconfig{$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",
					$sttaconfig{$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",
				   $sttaconfig{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $sttaconfig{$c} eq "" ? "" : "checked";
				}
			print &unix_user_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 6) {
			if ($p[2]) {
				printf
				   "<input type=radio name=${c}_def value=1 %s>$p[2]\n",
				   $sttaconfig{$c} eq "" ? "checked" : "";
				printf "<input type=radio name=${c}_def value=0 %s>\n",
				   $sttaconfig{$c} eq "" ? "" : "checked";
				}
			print &unix_group_input($c, $sttaconfig{$c});
			}
		elsif ($p[1] == 7) {
			print "<input name=\"$c\" size=40 value=$qw$sttaconfig{$c}$qw> ",
			      &file_chooser_button($c, 1);
			}
		elsif ($p[1] == 8) {
			print "<input name=\"$c\" size=40 value=$qw$sttaconfig{$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/, $sttaconfig{$c})),
				"</textarea>\n";
			}
		elsif ($p[1] == 10) {
			local $fv = $sttaconfig{$c};
			for($i=2; $i<@p; $i++) {
				$p[$i] =~ /^(\S*)\-(.*)$/;
				printf "<input type=radio name=\"$c\" value=\"$1\" %s>\n",
					$sttaconfig{$c} eq $1 ? "checked" : "";
				print "$2 &nbsp;&nbsp;\n";
				$fv = undef if ($sttaconfig{$c} eq $1);
				}
			printf "<input type=radio name=\"$c\" value=free %s>\n",
				$fv ? "checked" : "";
			print "<input name=\"${c}_free\" value='$fv'>\n";
			}
		print "</td> </tr>\n";
		}
}
sub init_dataconf
{
        %sttaconfig=();
	# Use config.info to parse config inputs
	&read_file("$module_dir/configinit.info", \%info, \@info_order);
	%einfo = %info;
	foreach $o (@lang_order_list) {
		&read_file("$module_dir/configinit.info.$o", \%info, \@info_order);
		}
	@info_order = &unique(@info_order);
        &get_values_tosave;

        &write_file("$configDef/$configValues", \%sttaconfig);

        if ( $in{ldap_type} eq "OpenLDAP" ) {
             $suffixcfg=".openldap";
        } else {
             $suffixcfg=".iplanet";
        }
        &read_file("$configDef/$listfcfgs$suffixcfg", \%listfcfgs, \@orderfcfgs);
        @orderfcfgs = &unique(@orderfcfgs);
        &read_file("$configDef/$configValues", \%sttaValues);

        foreach $d (@orderfcfgs) {
              %config=();
              $nomFile=$d;
              $nomFile=~ s/$suffixcfg//g;
              &lock_file("$configDir/$nomFile");
              &read_file("$configDef/$d", \%config);
	      foreach $o (@lang_order_list) {
                &read_file("$configDef/$d.$o", \%config);
              }
              foreach $c (keys (%config)) {
                   foreach $v (keys (%sttaValues)) {
                       if ( $config{$c} =~ /$v/ ) { 
                            $config{$c}=~s/$v/$sttaValues{$v}/g;
                       }
                       if ( $c eq $v &&  $c ne "" ) { $config{$c}=$sttaValues{$v} }
                   }
                   if ( $main::config{'tta_dir'} ne "" && $main::config{$c} ne "" ) {
                        $config{$c}=$main::config{$c};  # this is to migrate config file
                   }
              }
              &write_file("$configDir/$nomFile", \%config);
              &unlock_file("$configDir/$nomFile");
        }
        &write_file("$configDir/$configValues", \%sttaValues);
        system("cat $configDef/$listfcfgs$suffixcfg | sed \"s/$suffixcfg//g\" >  $configDir/$listfcfgs");
        system("cat $configDef/$scanlistcfgs$suffixcfg | sed \"s/$suffixcfg//g\" >  $configDir/$scanlistcfgs");
}

1;  # Return true
