#!/usr/bin/perl
# tta_deldomain.cgi
# Delete OrgUnits from Tarantella Services

require './tarantella-lib.pl';
do "ldap-lib.pl";

&header($text{'index_deldomain'}, "", "");
print "<hr><p>\n";
if ( $config{'is_ldapcache'} ne "true" ) { &footCacheHere; exit }
&foreign_require("proc", "proc-lib.pl");
$ldapcache_server=1;
&connect_cache();
&ReadParse();

 $domainname=$in{domainname};
 print "<table width=100% border>\n";
 print "<tr $tb> <td><b>$text{'titidx_deldomain'} $domainname </b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

    if ($domainname ) {
        $descrip="";
        $ensdomain="";
        $dnsdomain="";
        foreach $domain (&list_domains()) {
             if ( $domain->{'alias'} eq $domainname) {
                $ensdomain=$domain->{ens};
                $dndomain=$domain->{obj};
                $descrip=$domain->{descrip};
                last;
             }
        }
    }
    if (!$dndomain ) { &footHere; exit }  ;
    $dn = "$dndomain,".$config{ldapcache_base};
    $ldapcache_server=1;
    &connect_cache ();
    if ( &del_cache_obj($dn) ne 0 ) { &footHere; exit }  ;
    if ( $config{ldapcache2_server}.length gt 0 ) {
             $ldapcache_server=2;
             &connect_cache ();
             if ( &del_cache_obj($dn) ne 0 ) { &err_replication() }
    }
    print "<tr></tr><tr></tr>\n";
    print "<tr><b>".$text{'user_delete'}."... LDAP CACHE ... </b></tr>\n";
    print "<tr><td><pre>\n";
    if ( $ensdomain ne "" ) {
         $ens_obj = "--name \"".$config{tta_ldapcacheApps}."/$ensdomain\" --children";
         $type_cmd="delete";
#        if (&run_tta_cmd("delete",$ens_obj)) { 
#             print "</pre><tr><p><b>$text{'user_delete'}... Tarantella </b></tr>\n";
              @listdomains=&list_domains();
              @list__domains = sort {$a->{name} cmp $b->{name}} @listdomains;
              &lock_file($config{'domain_list'});
	      open(FILEDOMAINS, ">$config{'domain_list'}");
                   foreach $domain (@list__domains) {
                       if ( $domain->{'alias'} ne $domainname) {
                            $lindomain=$domain->{'alias'}."::".$domain->{'name'}."::".$domain->{'obj'}."::".$domain->{'ens'}."::".$domain->{'descrip'}."\n";
                            print FILEDOMAINS "$lindomain";
                       }
                   }
               close (FILEDOMAINS);
               $str=$text{'user_delete'}.$domainname;
               &additional_log('deldomains.cgi', undef, $str);
               &webmin_log($config{'domain_list'}, undef, "delete");
               &unlock_file($config{'domain_list'});
#        }
         print "</tr></td></pre>\n";
    }

 &footHere; 
 exit;
