# lists-lib.pl
# Common functions 


# list_ous()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "ou"
sub list_ous
{
# read the OU list file
local(%rv, $_, $lnum, @lin);
	# start by reading OU File List
	$lnum = 0;
	open(FILEOUS, $config{'ou_list'});
	while(<FILEOUS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
                                    'alias' => $lin[0],
                                    'name' => $lin[1],
				    'obj' => $lin[2], 
                                    'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5]
                                     };
                        }
		$lnum++;
		}
	close(FILEOUS);
return %rv;
}

# list_apps()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "app"
sub list_apps
{
# read the APP list file
local(%rv, $_, $lnum, @lin);
	# start by reading APP File List
	$lnum = 0;
	open(FILEAPPS, $config{'app_list'});
	while(<FILEAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'maxsess' => $lin[5], 
				    'descrip' => $lin[6] 
				    };
			}
		$lnum++;
		}
	close(FILEAPPS);
return %rv;
}

# list_grpapps()
# Returns an array of hashtable, each containing info about one grpapps. Each hash
# will always contain the keys "alias" "name" "grpapps"
sub list_grpapps
{
# read the GRPAPPS list file
local(%rv, $_, $lnum, @lin);
	# start by reading GRPAPPS File List
	$lnum = 0;
	open(FILEGRPAPPS, $config{'grpapps_list'});
	while(<FILEGRPAPPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5]
				    };
			}
		$lnum++;
		}
	close(FILEGRPAPPS);
return %rv;
}

# list_domains()
# Returns an array of hashtable, each containing info about one ou. Each hash
# will always contain the keys "alias" "name" "domain"
sub list_domains
{
# read the DOMAIN list file
local(%rv, $_, $lnum, @lin);
	# start by reading DOMAIN File List
	$lnum = 0;
	open(FILEDOMAINS, $config{'domain_list'});
	while(<FILEDOMAINS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5] 
				    };
			}
		$lnum++;
		}
	close(FILEDOMAINS);
return %rv;
}

# list_grphosts()
# Returns an array of hashtable, each containing info about one grphosts. Each hash
# will always contain the keys "alias" "name" "grphosts"
sub list_grphosts
{
# read the GRPHOSTS list file
local(%rv, $_, $lnum, @lin);
	# start by reading GRPHOSTS File List
	$lnum = 0;
	open(COUNTSESSFILE, $config{'grphosts_list'});
	while(<COUNTSESSFILE>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2], 'ens' => $lin[3], 
				    'maxsess' => $lin[4], 
				    'descrip' => $lin[5] 
				    };
			}
		$lnum++;
		}
	close(COUNTSESSFILE);
return %rv;
}

# list_hosts()
# Returns an array of hashtable, each containing info about one hosts. Each hash
# will always contain the keys "alias" "name" "hosts"
sub list_hosts
{
# read the HOSTS list file
local(%rv, $_, $lnum, @lin);
	# start by reading HOSTS File List
	$lnum = 0;
	open(FILEHOSTS, $config{'hosts_list'});
	while(<FILEHOSTS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'grpalias' => $lin[2], 
				    'obj' => $lin[3], 'ens' => $lin[4], 
				    'ipname' => $lin[5], 
				    'maxsess' => $lin[6], 
				    'descrip' => $lin[7] 
				    };
			}
		$lnum++;
		}
	close(FILEHOSTS);
return %rv;
}

# list_hostsuser()
# Returns an array of hashtable, each containing info about one hostsuser. Each hash
# will always contain the keys "alias" "name" "hostsuser"
sub list_hostsuser
{
# read the HOSTSUSER list file
local(%rv, $_, $lnum, @lin);
	# start by reading HOSTS File List
	$lnum = 0;
	open(FILEHOSTSUSER, $config{'hostsuser_list'});
	while(<FILEHOSTSUSER>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'username' => $lin[0],'passwd' => $lin[1],
				    'name' => $lin[2]
				    };
			}
		$lnum++;
		}
	close(FILEHOSTSUSER);
return %rv;
}

# list_profcache()
# Returns an array of hashtable, each containing info about one hostsuser. Each hash
# will always contain the keys "alias" "name" "hostsuser"
sub list_profcache
{
# read the PROFCACHE list file
local(%rv, $_, $lnum, @lin);
	# start by reading PROFCACHE File List
	$lnum = 0;
	open(FILEPROFCACHE, $config{'profcache_list'});
	while(<FILEPROFCACHE>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],'name' => $lin[1],
				    'hostusername' => $lin[2],
				    'hostname' => $lin[3],
				    'grphost' => $lin[4],
				    'username' => $lin[5],
				    'ou' => $lin[6]
				    };
			}
		$lnum++;
		}
	close(FILEPROFCACHE);
return %rv;
}

# list_actions()
# Returns an array of hashtable, each containing info about actions. Each hash
# will always contain the keys "alias" "name", etc. 
sub list_actions
{
# read the ACCTIONS list file
local(%rv, $_, $lnum, @lin);
	# start by reading ACCTIONS File List
	$lnum = 0;
	open(FILEACCTIONS, $config{'act_list'});
	while(<FILEACCTIONS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
                        if ( $task_type eq "" || $task_type eq $lin[9] ) {
			$rv{$lin[0]} = {
			                  'alias' => $lin[0],'name' => $lin[1],
			                  'target' => $lin[2],
			      	          'object' => $lin[3],
				          'condition' => $lin[4],
				          'conditionValue' => $lin[5],
				          'action' => $lin[6],
				          'script' => $lin[7],
				          'when' => $lin[8],
				          'how' => $lin[9]
				    };
                       }
		}
		$lnum++;
	}
	close(FILEACCTIONS);
        return %rv;
}

# list_links()
# Returns an array of hashtable, each containing info about one link. Each hash
# will always contain the keys "alias" "name" "link"
sub list_links
{
# read the Links list file
local(%rv, $_, $lnum, @lin);
	# start by reading Links File List
	$lnum = 0;
	open(FILELKS, $config{'tta_links'});
	while(<FILELKS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2]
				    };
			}
		$lnum++;
		}
	close(FILELKS);
return %rv;
}

# list_webtops()
# Returns an array of hashtable, each containing info about one webtop. Each hash
# will always contain the keys "alias" "name" "link"
sub list_webtops
{
# read the Webtops list file
local(%rv, $_, $lnum, @lin);
	# start by reading Webtops File List
	$lnum = 0;
	open(FILEWEBTOPS, $config{'tta_webtops'});
	while(<FILEWEBTOPS>) {
		s/\r|\n//g;
		if (/\S/ && !/^[#\+\-]/) {
		        chomp($lin);
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
			            'alias' => $lin[0],	'name' => $lin[1],
				    'obj' => $lin[2]
				    };
			}
		$lnum++;
		}
	close(FILEWEBTOPS);
return %rv;
}

# load_ttaobj($temp_f)
# Returns an array of hashtable, each containing info about one webtop. Each hash
# will always contain the keys "alias" "name" "link" etc .
sub load_ttaobj
{
# read the obj properties from file
   local(%ttaobj, $_, $lnum, @lin);
 
	# start by reading from file List

	$lnum = 0;
	open(FILETTAOBJ, $temp_f);
	while(<FILETTAOBJ>) {
		 if (/\S/ && !/^[#\+\-]/ && $lnum gt 0 ) {
		        chomp($lin);
			@lin = split(/: /, $_, -1);
		        $lin[1]=~ s/"//g;
		        $is_space=chop($lin[1]);
                        if ( $is_space eq " " ) { chop($lin[1]); }
			$ttaobj{$lin[0]} = {
			                'property' => $lin[0],
				    'value' => $lin[1]
				    };
			}
		$lnum++;
		}
	close(FILETTAOBJ);
	unlink($temp_f);
    return %ttaobj;
}

sub load_scriptlist
{
# read the obj properties from file
   local(%script_list, $_, $d, $lnum, $title);
# Get Scripts List
	opendir(DIR, $config{'monitor_scripts_path'});
	foreach $d (readdir(DIR)) {
        	if ( $d =~ /(\.tcl)$/ ) {
              	$file_scr=$config{'monitor_scripts_path'}."/$d";
              	$lnum=0;
              	open(FILESCR, $file_scr);
              	while (<FILESCR>) {
                 	if (/^$config{'mon_title'}/ && $lnum gt 0 ) {
                        	$title=$_;
                        	$title=~s/$config{'mon_title'}//;
			        $script_list{$lin[0]} = {
                        	                   { 'desc' => $title,
                                    	             'name' => $d
				                   };
                        	last;
                        	}
                	$lnum++;
              	}
              	close(FILESCR);
        	}
	}
	closedir(DIR);
#	@script_list= sort { $a->{'desc'} cmp $b->{'desc'} } @script_list;
  return %script_list;
}

# Returns an array of hashtable, each containing info about one file. Each hash
# will always contain the keys "alias" "name", etc
sub list_files
{
# read the filelist from file
  local ($wherepath); $wherepath = $_[0];   
  local ($temp_f, $elem);    
  local (%rv, $_, $lnum, @lin);
  local ($f_perm , $f_lsnum, $f_owner, $f_group, $f_size, $f_month);
  local ($f_day, $f_time, $f_name);

  $no_out=1;
  $temp_f = &tempname();
  $str=`(cd $wherepath ; ls -lt $ls_match >$temp_f) 2>/dev/null`;
  &webmin_log("list", $wherepath, $config{run_exec_user}, "$str");
#  if ( $str) {
	$lnum = 0;
	open(FILELIST, $temp_f);
	while(<FILELIST>) {
		s/\r|\n//g;
		@lin = split(/ /, $_, -1);
                $pos=0;
                $ifield=0;
                foreach $elem (@lin) {
                   if ( $elem ne "" ) {
                        if ( $pos eq 0 ) { $f_perm=$elem; }
                        if ( $pos eq 1 ) { $f_lsnum=$elem; }
                        if ( $pos eq 2 ) { $f_owner=$elem; }
                        if ( $pos eq 3 ) { $f_group=$elem; }
                        if ( $pos eq 4 ) { $f_size=$elem; }
                        if ( $pos eq 5 ) { $f_month=$elem; }
                        if ( $pos eq 6 ) { $f_day=$elem; }
                        if ( $pos eq 7 ) { $f_time=$elem; }
                        if ( $pos eq 8 ) { $f_name=$elem; }
                        $pos++;
                   }
                   $ifield++;
                }
			$rv{$lin[0]} = {
                                'perm' => $f_perm , 'lsnum' => $f_lsnum,
                                'owner' => $f_owner, 'group' => $f_group,
                                'size' => $f_size, 'month' => $f_month,
                                'day' => $f_day, 'time' => $f_time,
                                'name' => $f_name
				    };
                $lnum++;
	}
	close(FILELIST);
#   }
return %rv;
}
# list_countsess()
sub list_countsess
{
# read the countsess list file
local(%rv, $_,  $lnum, @lin, $datetime_tilte);
	# start by reading COUNTSESS File List
	$lnum = 0;
        $datetime_title="#datetime: ";
	open(COUNTSESSFILE, $config{'countsessout_file'});
	while(<COUNTSESSFILE>) {
		s/\r|\n//g;
                if (/^$datetime_title/ && $lnum eq 0 ) {
			$counter_time=$_;
			$counter_time=~ s/$datetime_title//g;
                }
		if (/\S/ && !/^[#\+\-]/) {
			@lin = split(/::/, $_, -1);
			$rv{$lin[0]} = {
                                    'numsess' => $lin[0], 
                                    'descrip' => $lin[1] ,
                                    'name' => $lin[2] ,
                                    'objalias' => $lin[3] ,
                                    'alias' => $lin[4] ,
                                    'maxsess' => $lin[5] ,
                                    'objgrpalias' => $lin[6] ,
                                    'grpalias' => $lin[7] 
				    };
		} 
		$lnum++;
        }
	close(COUNTSESSFILE);
return %rv;
}

# load_tta_emulses($temp_f)
# Returns an array of hashtable, each containing info about one webtop. Each hash
# will always contain the keys "alias" "name" "link" etc .
sub load_tta_emulses
{
# read the obj properties from file
   local(%ttasess, $_, $lnum, @lin);
 
	# start by reading from file List

	$lnum = 0;
        $datetime_title="#datetime: ";
	open(FILETTASESS, $temp_f);
	while(<FILETTASESS>) {
		s/\r|\n//g;
                if (/^$datetime_title/ && $lnum eq 0 ) {
			$ttasess_time=$_;
			$ttasess_time=~ s/$datetime_title//g;
                        if ( "$_[0]" eq "getdate" )  { last; };
                }
	        if (/\S/ && !/^[#\+\-]/ && $lnum gt 0 ) {
			@lin = split(/: /, $_, -1);
		        $lin[1]=~ s/"//g;
#		        $is_space=chop($lin[1]);
#                        if ( $is_space eq " " ) { chop($lin[1]); }
			$ttasess{$lin[0]} = {
			                 'property' => $lin[0],
				         'value' => $lin[1]
				         };
			}
		$lnum++;
		}
	close(FILETTASESS);
    return %ttasess;
}

1;  # Return true
