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

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 "installupd-lib.pl";

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

# Loading rules (alis 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);
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;
@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");

&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} || "" ;
$fileName=$in{file} || "" ;
$now = time();
$JScodeInit="";   # to  hold code for initJs  config by example

if ( $redir ne "" ) { 
    &headerstta($text{'index_title'}, "images/$current_lang/stta_stta.gif", "sttaintro",undef, 1, 0,$text{headpage_rigth} );
    do "index/insupd.pl";
    do "footer.pl";
    exit;
}
if ( $task ne "" )  { $DirInstall= "$root_directory/stta/$task"; }
&set_hashes_rules();

if ( $lockDo eq "lockstta" ) {  &lock_insupd_stta($lockDo); }
if ( $lockDo eq "unlockstta") { &unlock_insupd_stta($lockDo); }
if ( ! -r "$config_directory/$module_name/.install"  &&  
       $Step eq "lockstta"  )  { &lock_insupd_stta("lock"); }

&headerstta($text{'index_installupd'}, "", "insupd" ,undef,1,1,$text{headpage_rigth},"","onLoad='initJs();'" );
&out_javascript();
 print "<hr><p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td valig=top><b> $text{'titidx_installupd'}</b>";
 print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
 print "Webmin $ver -  $hostname  -  $ostr";
 print "</td> </tr>\n";
 
 &out_head_instupd();
 
# Seleccionar Task .... (new request)
if ( $task eq "" ) { I
     &out_sel_task(); 
     &footHere; 
     &out_initJS;
     exit
};
# If file to load get Name ... (new request)
if ( $task ne "install" && $fileName eq "" && $Step eq ""  ) { I
     &get_file_updpatch();  # paso 0 from no-file for some task not install
     &footHere; 
     &out_initJS;
     exit
}

# If file to load  Unpack it ... and keep going 
if ( $task ne "install" && $fileName ne "" && $Step eq ""  ) {   
    &running_fileName(); # unpack
}

# Always "do" perl file for $task in $InstallDir/$task/tmp 
&do_instupdfile();

if ( ! -r "$config_directory/$module_name/.install" ) { 
     &check_visit_tasks();
     &make_steps_tables();
     if ( $task ne "" && $Step ne "" ) { 
          print "<table width=100% border><tr $cb><td>\n";
          print "<tr><td><h2><font color='#000099'><b>$text{warn_lockstta2}</b></font></h2></td></tr><tr>\n";
          print "</td></tr></table>\n";
     }
} else {
     if (  $Step ne "" ) {
           &check_visit_tasks();
           &doStep();
     }
     &make_steps_tables();
}

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


 $config{iferror_services}="warn";

&show_tta_serv_table();
&check_ps_serv();
&check_ttabase();

   if ( $debug eq 0 ) { $debug=2 }

print "<tr $cb> <td><table width=100%><tr $cb><td>\n";
   $ldap_server=$in{ldapserver} || "1";
   print " <tr $tb> <td><big><b>\n";
   if ( "$ldap_server" eq "1" ) {
        print $config{ldap_type}.":".$config{ldap_server}.":".$config {ldap_port};
   } elsif ( "$ldap_server" eq "2" ) {
        print $config{ldap2_type}.":".$config{ldap2_server}.":".$config {ldap2_port};
   }
   print "</b></big></td></tr>\n";
   if ( $ldap_ok eq true ) {
        print "<tr $cb><td><b><big>LDAP:  </big>$text{chk_srv_title} - $config{ldap_base} - </b></td></tr>\n";
        &connect();
        $dn=$config{ldap_base};
        $cur_entry=&sel_ldap_obj($dn,"o=*");
        if ( !defined($cur_entry)  ||  $cur_entry->dn ne $dn) {
             &html_errshow("ldap")
        } else { 
             &ldap_logerr("browse $dn");
             print "<tr $cb><td>".&text('viewlog_action',$stta_sid)."</td></tr>\n";
        }
   } else { print "<tr $cb><td><b><big>LDAP:  </big> - $config{ldap_base} - ???? </b></td></tr>\n";}
print "</table>\n";

print "<tr $cb> <td><table width=100%><tr $cb><td>\n";
   $ldapcache_server=$in{ldapcacheserver} || "1";
   print " <tr $tb> <td><big><b>\n";
   if ( "$ldapcache_server" eq "1" ) {
        print $config{ldapcache_type}.":".$config{ldapcache_server}.":".$config {ldapcache_port};
   } elsif ( "$ldapcache_server" eq "2" ) {
        print $config{ldapcache2_type}.":".$config{ldapcache2_server}.":".$config {ldapcache2_port};
   }
   print "</b></big></td></tr>\n";
   if ( $ldapcache_ok eq true ) {
        print "<tr $cb><td><b><big>LDAP Cache:  </big>$text{chk_srv_title} - $config{ldapcache_base} - </b></td></tr>\n";
        &connect_cache();
        $dnCache=$config{ldapcache_base};
        $cur_entry=&sel_cache_obj($dnCache,"o=*");
        if ( !defined($cur_entry)  ||  $cur_entry->dn ne $dnCache) { 
             &html_errshow("ldapcache")
        } else { 
             &ldapcache_logerr("browse $dn");
             print "<tr $cb><td>".&text('viewlog_action',$stta_sid)."</td></tr>\n";
        }
   } else {  print "<tr $cb><td><b><big>LDAP Cache:  </big>$text{chk_srv_title} - $config{ldapcache_base} - ???? </b></td></tr>\n"};
 print "</table>\n";

 $text_str="";
 $text_str=~s/STTA:/<tr><td><li>/g;
 $text_str=~s/\n/<\/b><\/td><\/tr>/g;
 $text_str=~s/==/<td><b>/g;
 print $text_str;

 print "</td></tr></table></table>\n";
 &footer($config{'back_return'},$text{'index'});
 do "footer.pl";
 &out_initJS;
 exit;

