#!/usr/bin/perl
# stta_installupd.cgi
# ...  Init Update Services with Tarantella.
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

# all this is to not load "stta-lib.pl"  as nothing can be installed !!!!
require '../web-lib.pl';
do 'web-lib.pl';
do 'global-lib.pl';
do 'lists-lib.pl';
do 'html-lib.pl';
do 'ttasess-lib.pl';
do 'monitor-lib.pl';
do 'print-lib.pl';
do "config_info.pl";
do "ldap-lib.pl";
do "help-lib.pl";
do "installupd-lib.pl";

&init_config();
&load_dataconf();
$white="#ffffff";
&set_variables_cfg();

$logInsUpd="$config_directory/$module_name/installupd.log";

# Loading rules (alias paranoia rules) --- defaults &  local $task rules & tmp rules
$rulesFile= "$root_directory/stta/install/rules";
&read_file("$rulesFile", \%rules);
$rulesFile= "$root_directory/stta/$task/rules";
&read_file("$rulesFile", \%rules);
$rulesFile= "$root_directory/stta/$task/tmp/rules";
&read_file("$rulesFile", \%rules);

$hostname = &get_system_hostname();
$ver = &get_webmin_version();
&get_miniserv_config(\%miniserv);
$config{check_services}="false";  # force no services check

if ($gconfig{'real_os_type'}) {
        if ($gconfig{'os_version'} eq "*") {
                $ostr = $gconfig{'real_os_type'};
                }
        else {
                $ostr = "$gconfig{'real_os_type'} $gconfig{'real_os_version'}";
                }
        }
else {
        $ostr = "$gconfig{'os_type'} $gconfig{'os_version'}";
        }
%access = &get_module_acl();
%ttaprops=();
@ttahosts=();
%sttaparams=();
$cginame= $0 =~ /([^\/]+)$/ ? $1 : '-';   
$ldap_ok=true;
$ldapcache_ok=true;
$tta_ok=true;
$button_lock=0;
@access_types = $access{'types'} eq '*' ? (0 .. 100)
				        : split(/\s+/, $access{'types'});
map { $access_types{$_}++ } @access_types;
$debug=$config{debug_code};

&foreign_require("proc", "proc-lib.pl");
@procs = &foreign_call("proc", "list_processes");


&ReadParse();
%rulesHash=();
$task=$in{task} || "";
$cgi = $in{cgi};
$msg = $in{msg};
$tdone = $in{td} || "";
$lockDo = $in{lck} || "";
$nextStep=$in{next} || "";
$redir=$in{rd} || "" ;
$Step=$in{step} || "" ;
$StepCheck=$in{stepcheck} || "";
$fileName=$in{file} || "" ;
$sttasrv=$in{sttasrv} || "" ;
$now = time();
$STTA_LOCK=0;
$JScodeInit="";   # to  hold code for initJs  config by example
$excludeTasks="install masterstta joinarray";

if ( ! $access{'global'} || $access{'admin'} ne 1) { $redir="stta" };

if ( $redir ne "" ) { 
    &headerstta($text{'index_title'}, "images/$current_lang/stta_stta.gif", "sttaintro",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
    do "index/insupd.pl";
    do "footer.pl";
    exit;
}
if ( $task eq "brand" ) {
     &headerstta($text{'brand_title'}, undef, "brandinfo",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
   
     print "<td><table width=100% border>\n";
     print "<tr $tb> <td valign=top><font color='$revtext'>$text{branding}&nbsp;&nbsp;&nbsp;<b>$text{$Step}</b></font></td></tr>\n";
     print "<tr $cb > <td valign=top><table width=100%>\n";
     print "<tr bgcolor=$white><td valign=top><pre>\n";
     &dump_brandinfo($in{key},$fileName,1);
     print "</pre></td></tr>\n";  
     &footHere;
     exit;
}
if ( -r "$config_directory/$module_name/.insupd.lock" )  { $STTA_LOCK=1 };  
if ( $task ne "" )  { $DirInstall= "$root_directory/stta/$task"; }

&set_hashes_rules();

if ( $lockDo eq "lockstta" ) {  
     &lock_insupd_stta($lockDo);
     $Step=$lockDo;
} elsif ( $lockDo eq "unlockstta") {
     &unlock_insupd_stta($lockDo);
} elsif ( $Step eq "unlockstta" ) {
     &unlock_insupd_stta($lockDo);
}
$helpFile="sec_installupd";
if ( $task ne ""  ) { $helpFile="inst_".$task }
&headerstta($text{'index_installupd'}, "", $helpFile ,undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,"onLoad='initJs();'","$text{head_below}");
&out_javascript();
 print "<hr><p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td valig=top><font color='$revtext'><b> $text{'titidx_installupd'}</b>";
 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
 print "Webmin $ver -  $hostname  -  $ostr";
 print "</td></td> </tr>\n";
 
 &out_head_instupd();
 
# Seleccionar Task .... (new request)
if ( $task eq "" ) { I
     &head_lock_button();
     &out_sel_task(); 
     &out_initJS;
     &footHere; 
     exit
};
if ( ( $task eq "masterstta" || $task eq "joinarray" ) &&  $sttasrv eq "" ) {
     &head_lock_button();
     &out_sel_sttasrv(); 
     &out_initJS;
     &footHere; 
     exit
}

# If file to load get Name ... (new request)
if ( $excludeTasks =~ /$task/ ) {
    do "inststta-lib.pl";
} else {
    # If file to load  Unpack it ... and keep going 
    if ( ! -r "$DirInstall/tmp/$task.pl"  && $fileName ne ""  ) {   
         &head_lock_button();
         &running_fileName(); # unpack
    }
    if ( ! -r "$DirInstall/tmp/$task.pl" ) {   
         print "<tr><td><h2> $text{f_loadfile} \"$task.pl\" $text{not_found} </h2></td></tr>\n";
         &get_file_updpatch();  # paso 0 from no-file for some task not install
         &out_initJS;
         &footHere; 
         exit
    }
}

# Always "do" perl file for $task in $InstallDir/$task/tmp 
&do_instupdfile();
# More paranoia rules ... to support changes on the fly 
$rulesFile= "$root_directory/stta/$task/tmp/rules";
&read_file("$rulesFile", \%rules);

     
&head_lock_button();
if ( ( $task eq "masterstta" || $task eq "joinarray")  &&  $sttasrv ne "none" ) {
#     &lock_insupd_stta("lockstta");
     &get_srvstta_files();
#     &unlock_insupd_stta("unlockstta");
     $sttasrv="none";
}
if ( $STTA_LOCK eq 0  ) { 
     print "<form METHOD=POST name=\"f_stepinstallupd\" ACTION=\"stta_installupd.cgi\" onSubmit=\"return checkStep(this);\">\n";
     &make_steps_tables();
     if ( $task ne "" && $Step ne "" ) { 
          print "<tr $cb><td><table $cb width=100%><tr $cb><td>\n";
          print "<tr $cb><td><h2><font color='#000099'><b>$text{warn_lockstta2}</b></font></h2></td></tr><tr>\n";
          print "</td></tr></table></td></tr>\n";
     }
} else {
     print "<form METHOD=POST name=\"f_stepinstallupd\" ACTION=\"stta_installupd.cgi\" onSubmit=\"return checkStep(this);\">\n";
     if (  $Step ne ""  ) { &doStep(); }
     &check_visit_tasks();
     &make_steps_tables();
}

if ( $Step ne "checklist" && $task ne "check" ) {  &out_initJS; &footHere; exit }  ;
 #  From here all checking staff .....
# print "<tr $cb> <td align=center><table $cb><tr $cb><td>\n";

#   $ReportOut="/tmp/out";
#   open (REPORTOUT,">$ReportOut");
   &make_STTA_Report();
#   close(REPORTOUT);
# print "<tr $cb> <td align=center>$text{user_made} </td></tr>\n";

 &footer($config{'back_return'},$text{'index'});
 &out_initJS;
 do "footer.pl";
 exit;

