#!/usr/bin/perl

$task_type = $ARGV[0];

if ( $task_type ne "auto" && $task_type ne "task" ) { 
     print "Monitor: \"$task_type\" unknown option task \n";
     print "monitor [auto|task]\n";
     exit 1;
};

# where is config file for parameters .... (same as Webmin)
$config_directory="/etc/webmin/tarantella";
$debug=2; # if 0 it will try to get config file value

require './monitor-lib.pl';

$tta_logfile=$ENV{'TTA_LOGFILE'};
$default_lang = "en";
$current_lang = "es";
$module_name = "tarantella";
 
&initialize();
%text = &load_language(); 
 
$rule_limit=$config{cache_rulelimit};
$default_obj="cn=$config{ldapcache_defaultobj}";
$default_dnobj="$config{ldapcache_dndefaultobj}";
$chek_default=$config{cache_chkdefaults};
$has_monitor=$config{has_monitor};

 
if ($has_monitor ne true ) { exit } ;

$mon_scripts=$config{monitor_scripts_path};
$act_list=$config{act_list};
$mon_scripts_title=$config{mon_title};
$run_exec_user=$config{run_exec_user};

$UserName=$ENV{'TTA_UserName'};

#$ens_base=$config{'ens_base'}.$config{'tta_base'};
#$UserName=~s/$config{'tta_ldapUser'}//;
#$UserName=~s/,$config{'ldap_base'}//;
#$UserName=~s/$config{'ens_base'}//;
#$AppName=~s/$ens_base\///;

$nowTime=`date +%H%M%S`;    
$nowDate=`date +%d%m%y`;    

#if ( $debug eq 2 ) { print "$nowDate $nowTime \n"; }


#  Load actions list  ....

#  Is there anything to look at ????
   
    # As $task_type is already defined ... only task_type will be included in hash ...
    @all_actions=&list_actions();

    $sort_on="how";
    @actions = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_actions; 


#  Loading TTA Sessions ...

    $temp_f=$config{'tta_sessout_file'};
#    $str=`(tta emulatorsession  list >$temp_f) 2>/dev/null`;
    $temp_f="/tmp/emu-tta2";
    &load_tta_sess_info();
#    unlink($temp_f);
    if ($#all_ttasess < 0) {
        print " ->".$text{msg_1}."\n";
        exit 1;
    } 
    $sort_on="pe_id";
    $sort_on="application";
    $sort_on="user";
    $sort_on="startTime";
    $sort_on="resume";
    $sort_on="status";
    $sort_on="applicationServer";
    $sort_on="client";
    $sort_on="serverUser";

    $sort_on="user";
    @ttasess = sort {$a->{$sort_on} cmp $b->{$sort_on}} @all_ttasess;
    foreach $ttasess (@ttasess) { 
       $UserName=$ttasess->{'user'};
       $AppName=$ttasess->{'application'};
       $HostName=$ttasess->{'applicationServer'};
       &scan_envsess();
       &alias_envsess();
       &setcount_envsess();
       $pe_ID=$ttasess->{'pe_id'};

#      print "" . $ttasess->{'id'}.""; 
       print "$pe_ID"; 
       print "$NameUser ($OuName) $userType";
       print "" . $ttasess->{'client'}.""; 
       print "$NameApp";
       if ( $NameAppGrp.length ne 0 ) { print " ($NameAppGrp)"; }
       print "" . $ttasess->{'startTime'}.""; 
       print "" . $ttasess->{'resume'}."n"; 
       print "" . $ttasess->{'status'}.""; 
       print "$NameHost";
       if ( $NameHostGrp.length ne 0 ) { print " ($NameHostGrp)"; }
       print "" . $ttasess->{'serverUser'}."\n"; 
       if ( $debug gt 0 ) { &print_emulses(); }
    }
    &sum_grp_sess();
    foreach $countsess (@count_sess) {
       print "->".$countsess->{'name'}."##".$countsess->{'objalias'}.
             "##".$countsess->{'obj'}.
             "##".$countsess->{'objgrpalias'}."## =".$countsess->{'num'}."\n";
    }

exit 1;
#  Processing actions ...

   # mon_actions_value=run-script,logout,cancel-old-resume 
    @l_actions = split(/,/,$text{'mon_actions_value'}, -1);       

   # mon_target_value=user,ou,host,grphosts,app,grpapps,date,time 
    @l_targets = split(/,/,$text{'mon_target_value'}, -1);       

   # mon_conditions_value=gt,lt,eq,ne,noactive,disconnected 
    @l_conditions = split(/,/,$text{'mon_conditions_value'}, -1);       

   # mon_how_value=manual,auto,task 
    @l_how = split(/,/,$text{'mon_how_value'}, -1);       
    
#    foreach $item (@l_target) {
#                if ( $item eq $monTarget ) {
#                        $targetext='mon_'.$item.'_sess';
#                        $t_monTarget=$text{$targetext};
#                                last;
#                }
#    }



# .......................
&scan_env();

if ( $debug gt 0 ) { &print_results(); };

 &out_empty_data;  # just in case 
 
 for ( $i=1 ; $i< $rule_limit+1; $i++ ) {
    $obj=eval("\$config{cacherule_$i}");
    if ( "$obj" eq "apps" ) { $dn="ou=$NameApp,$NameAppGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "grpapps" ) { $dn="$NameAppGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "hosts" ) { 
         if ("$NameAppGrp" eq "$NameHostGrp" ) {
             $dn="$NameHostGrp,$config{ldapcache_base}";
         } else {
             $dn="ou=$NameHost,$NameHostGrp,$config{ldapcache_base}";
         }
    }
    if ( "$obj" eq "grphosts" ) { $dn="$NameHostGrp,$config{ldapcache_base}"; }
    if ( "$obj" eq "domains" ) { $dn="ou=$NameDomain,$config{ldapcache_base}"; }
    $dn=~s/cn=/ou=/g;
    # Trying  Username 
      $dn_obj="cn=$NameUser,$OuName,$dn";
      if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
      $entry = $conncache->browse ($dn_obj);
      if ( $entry )  { &outData ; $i=$rule_limit+1}
    # Trying  default_obj for UserOU 
      if ( "$chek_default" eq "true" ) {
           $dn_obj="$default_obj,$OuName,$dn";
           if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
           $entry = $conncache->browse ($dn_obj);
           if ( $entry )  { &outData ; $i=$rule_limit+1}
      }
    # Trying  $dn (OBJECT)  default_obj 
      if ( "$chek_default" eq "true" ) {
           $dn_obj="$default_obj,$dn";
           if ( $debug eq 2 ) { print DebugLog "($i) $obj -- $dn_obj \n" };
           $entry = $conncache->browse ($dn_obj);
           if ( $entry )  { &outData ; $i=$rule_limit+1}
      }
 }

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