#!/usr/bin/perl

$debug=0; 
$do_reload = $ARGV[0];
 
if ( $do_reload eq "?" || $do_reload eq "help" | $do_reload eq "-h"  ) {
     print "ttasess: \"$task_type\" unknown option task \n";
     print "ttasess [ -r | reload | true]\n";
     exit 1;
};
$is_reload=false;
if ( $do_reload eq "reload" || $do_reload eq "true" | $do_reload eq "-r"  ) {
     $is_reload=true;
}
# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin"
 
do './base-lib.pl';
do './global-lib.pl';
do './lists-lib.pl';
do './print-lib.pl';
require  './ttasess-lib.pl';
 
$tool_name="ttasess";
$tta_logfile=$ENV{'TTA_LOGFILE'};
$tta_logfile="/tmp/.webmin/stta_log";
$default_lang = "en";
$current_lang = "es";
$module_name = "stta";
$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;
 
&initialize();
&load_dataconf();
#$DebugFile="/var/webmin/webmin.log";
&start_debug();
%access = &get_module_acl();
%text = &load_language();

# globals to keep STTA info
  %ttaprops=();
  @ttahosts=();
  %sttaparams=();
  $ldap_ok=true;
  $ldapcache_ok=true;
  $tta_ok=true;
 
  &load_ttaprops();
  $mhost=$sttaparams{'tta'}{'masthost'};
  &scanSTTA_services("all");
  if ( $debug gt 0 ) { 
       &out_params();
       &checkSTTA_services("all","warn");
  }
 

  $run_exec_user=$config{run_exec_user};  
 
if ( $config{has_cronsess} ne "true" || $config{'countsessout_file'} eq "" ) {
         $err_msg=$text{nohascountsess_err};
         &webmin_log($tool_name, "has_cronsess",$err_msg); 
         exit;
}

$ahora=&set_now();    

# call to ttasess_tables()
#
# To load TTA session Tables
# if "has_cronsess" and "tta_sessout_file"  it will try to use stored info
# $is_reload force to reload in case of true
# $counter_time & $counter_date are checked to match upon defined $config{sessout_timeout}# $config{'tta_sessout_file'} & $config{'countsessout_file'} are read, clean or write
# %h_countsess for counters and @tta_sess order ($sort_on) from @all_ttasess are set
# There are many Globals in this module ...
# CGI calling this function should pay attention to:
# $is_reload (true|false) as default
# $sort_op ( "pe_id"; "application"; "user"; "startTime"; "resume"; "status";
#             "applicationServer"; "client"; "serverUser"; "user"; )
# should use;
# $counter_time, $ttasess_time    %h_countsess  @tta_sess order on $sort_on @all_ttasess

 $status_ret=&ttasess_tables();
 if ( $status_ret eq -2 ) {
      # if ( $is_reload ne true && "$counter_time" ne "$ttasess_time" ) 
      $status_ret=&ttasess_tables();
      if ( $status_ret ne 0 ) {
           $err_msg=$text{sessdiff_err};
           &webmin_log($tool_name, "load_ttasess",$err_msg); 
           exit;
      }
 }
 if ( $status_ret eq -1 || $#all_ttasess < 0) { 
     $err_msg=$text{nosess_err};
     &webmin_log($tool_name, "load_ttasess",$err_msg); 
     exit;
 }

if ( $debug gt 0 ) { &endclose_debug(); } 
exit;
