#!/usr/bin/perl
# tta_delhosts.cgi
# Delete Hosts from Tarantella Services

 require './tarantella-lib.pl';
&foreign_require("proc", "proc-lib.pl");
do "ldap-lib.pl";
&ReadParse();

 $hostname=$in{hostname};
 &header($text{'index_delhosts'}, "", "");
 print "<hr><p>\n"; 
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_delhosts'} $hostname </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

if ($hostname ) {
    $organizationname="";
    $ens_hosts="";
    $dns_hosts="";
    foreach $hosts (&list_hosts()) {
         if ( $hosts->{'alias'} eq $hostname) {
            $ens_hosts=$hosts->{ens};
            $dn_hosts=$hosts->{obj};
            $organizationname=$hosts->{name};
            last;
         }
    }
}
if ( $dn_hosts ne "" ) {
    $dn = "$dn_hosts,".$config{ldap_base};
              print "<tr></tr><tr></tr>\n";
              print "<tr><td><pre>\n";
              if ( $ens_hosts ne "" ) {
                   $ens_obj = "--name \"".$config{tta_base}."/$ens_hosts\"\n";
                   $type_cmd="delete";
                   if (&run_tta_cmd("delete",$ens_obj)) { 
                      print "</pre><tr><p><b>$text{'user_delete'}... Tarantella </b></tr>\n";
                      @listhosts=&list_hosts();
                      @list__hosts = sort {$a->{'name'} cmp $b->{'name'}} @listhosts;
                      &lock_file($config{'hosts_list'});
	              open(FILEHOSTS, ">$config{'hosts_list'}");
                      foreach $hosts (@list__hosts) {
                         if ( $hosts->{'alias'} ne $hostname) {
                             $lin_hosts=$hosts->{'alias'}."::".$hosts->{'name'}."::".$hosts->{'grpalias'}."::".$hosts->{'obj'}."::".$hosts->{'ens'}."::".$hosts->{'ipname'}."::".$hosts->{'maxsess'}."::".$hosts->{'descrip'}."::\n";
                             print FILEHOSTS "$lin_hosts";
                            
                         }
                      }
                      close (FILEHOSTS);
                      $str=$text{'user_delete'}.$hostname;
                      &additional_log('delhosts.cgi', undef, $str);
                      &webmin_log($config{'hosts_list'}, undef, "delete");
                      &unlock_file($config{'hosts_list'});
                   }
              print "</tr></td></pre>\n";
    }
}
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
