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


$task_type = $ARGV[0];
$opc = $ARGV[1];

if ( $task_type ne "dir" && $task_type ne "file"  && $task_type ne "list" && $task_type ne "ALL" ) { 
     print "setup: \"$task_type\" unknown option task \n";
     print "setup [dir|file|list|ALL] -force\n";
     exit 1;
};

# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin/stta";
$debug=2; 

do './base-lib.pl';
do './global-lib.pl';
do './lists-lib.pl';
do './print-lib.pl';

$tool_name="setup";
$tta_logfile=$ENV{'TTA_LOGFILE'};
$tta_logfile="/tmp/.webmin/stta_log";
$default_lang = "en";
$current_lang = "es";
$module_name = "stta";
$remote_user="admin";
$base_remote_user="admin";
$session_id="$$";
$hostname=`hostname`;
$hostname=~s/\n//g;
$ENV{'REMOTE_HOST'}=$hostname;
$action_id_count=0;
%ttaprops=();
@ttahosts=();
%sttaparams=();
$cginame= $0 =~ /([^\/]+)$/ ? $1 : '-';
$ldap_ok=true;
$ldapcache_ok=true;
$tta_ok=true;
$to_init=false;
$DebugFile="/var/webmin/webmin.log";
# if ( "" ) $config{stta_debugname}.$tool_name.".log";

&initialize();
%access = &get_module_acl();
%text = &load_language(); 
 
# globals to keep STTA info
  %ttaprops=();
  @ttahosts=();
  %sttaparams=();
  $ldap_ok=true;
  $ldapcache_ok=true;
  $tta_ok=true;
 
$m=`pwd`;
chomp($m);

&read_file("$m/$remote_user.acl", \%access);
#$access{'noconfig'} &&
#	&error($text{'config_ecannot'});

%minfo=&load_minfo();
$config_dir="$config_directory";
&read_file( "$config_dir/module.info",\%cinfo) ;
if ( $minfo{version} eq $cinfo{version} ) {
   if ( $opc eq "-force" ) {
       print "$minfo{name} Module ... $minfo{version} \n";
   } else {
       print "$minfo{name} Module $text{'user_update'}... $minfo{version} \n";
       exit;
   }
}

if (!(-d $config{'tta_dir'})) {
    $to_init=true;
    &lock_file("$config_directory/config.init");
    &read_file("$config_directory/config.init", \%config);
    &read_file("$m/config.infoinit", \%info, \@info_order);
    %einfo = %info;
    &read_file("$m/config.infoinit.$current_lang", \%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]);
		}
	$qw = $config{$c} =~ /'/ ? '"' : "'";
	print "$p[0]:\n";
	print "$c [$config{$c}] : ";
        $nom_file = <STDIN>;
        chomp($nom_file);
        if ( $nom_file ne "" ) { $config{$c}=$nom_file; }
    }
    &write_file("$config_directory/config", \%config);
    &unlock_file("$config_directory/config");
}

&read_file("$m/config.info", \%info, \@info_order);
%einfo = %info;
&read_file("$m/config.info.$current_lang", \%info, \@info_order);
@info_order = &unique(@info_order);

&read_file("$m/config", \%config);
&lock_file("$config_directory/config");
&read_file("$config_directory/config", \%config);
foreach $c (@info_order) {
	@p = split(/,/, $info{$c});
	@ep = split(/,/, $einfo{$c});
	if (scalar(@ep) > scalar(@p)) {
		push(@p, @ep[scalar(@p) .. @ep-1]);
		}
	$qw = $config{$c} =~ /'/ ? '"' : "'";
	if ($to_init eq true ) { $p[0]=eval $p[0]; }
	if ($p[1] == 7 && ($task_type eq "dir"|| $task_type eq "ALL" ) ) {
	        print "$p[0]:\n";
		print "$c [$config{$c}] : ";
                $nom_dir = <STDIN>;
                chomp($nom_dir);
                if ( $nom_dir ne "" ) { $config{$c}=$nom_dir; }
                mkdir($config{$c}, 0777)  || next; 
                chmod(0777, $config{$c});
                $str=$text{'user_made'}.$p[0]." ($c)->  ".$config{$c};
                &webmin_log("setup.pl", "$c", "str");
                print "$str\n";
	}
	elsif ($p[1] == 8 ) {
	   if ( $task_type eq "file" || $task_type eq "ALL" ) {
	        print "$p[0]:\n";
		print "$c [$config{$c}] : ";
                $nom_file = <STDIN>;
                chomp($nom_file);
                if ( $nom_file ne "" ) { $config{$c}=$nom_file; }
#                     chown(0, 0, $nom_file);
#                     chmod(0755, $nom_file);
#                $str=$text{'user_update'}.$p[0]." ($c)->  ".$nom_file;
#                &webmin_log("setup.pl", "$c", "str");
#                print "$str\n";
           }
	   elsif ( ($task_type eq "list" || $task_type eq "ALL" )&& $c =~ /_list/ ) {  
	        print "$p[0]:\n";
		print "$c [$config{$c}] : ";
                $nom_file = <STDIN>;
                chomp($nom_file);
                if ( $nom_file ne "" ) { $config{$c}=$nom_file; }
#                   if ( $c =~ /grp/ ) {  }
           }
	}
}

&write_file("$config_directory/config", \%config);
&unlock_file("$config_directory/config");

&write_file("$config_directory/$remote_user".".acl", \%access);
 
&lock_file( "$config_dir/module.info") ;
open (MODINFO, ">$config_dir/module.info");
foreach $item (keys (%minfo)) {
    print MODINFO "$item=".$minfo{$item}."\n";
}
close (MODINFO);
&webmin_log("_config_", undef, undef, "update", $module_name);

