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

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

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

if ($userou ) {
    $organizationname="";
    $ens_ou="";
    $dns_ou="";
    foreach $ou (&list_ous()) {
         if ( $ou->{'alias'} eq $userou) {
            $ens_ou=$ou->{ens};
            $dn_ou=$ou->{obj};
            $organizationname=$ou->{name};
            last;
         }
    }
}
if ( $dn_ou ne "" ) {
    $dn = "$dn_ou,".$config{ldap_base};

    $ldap_server=1;
    &connect ();
    if ( &del_ou() eq 0 ) { 

        if ( $config{ldap2_server}.length gt 0 ) {
             $ldap_server=2;
             &connect ();
             if ( &del_ou() ne 0 ) { &err_replication() }
        }
        print "<tr></tr><tr></tr>\n";
        print "<tr><b>".$text{'user_delete'}."... LDAP ... </b></tr>\n";
        print "<tr><td><pre>\n";
        if ( $ens_ou ne "" ) {
             $ens_obj = "--name \"".$config{tta_base}."/$ens_ou\" --children";
             $type_cmd="delete";
             if (&run_tta_cmd("delete",$ens_obj)) { 
                print "</pre><tr><p><b>$text{'user_delete'}... Tarantella </b></tr>\n";

                $lin_ou=$aliasou."::".$ouname."::"."ou=$ouname,$dn_ou"."::"."$ens_ou/ou=$ouname";
                @listous=&list_ous();
                @list__ous = sort {$a->{'name'} cmp $b->{'name'}} @listous;
                &lock_file($config{'ou_list'});
                open(FILEOUS, ">$config{'ou_list'}");
                foreach $ou (@list__ous) {
                   if ( $ou->{'alias'} ne $userou) {
                       $lin_ou=$ou->{'alias'}."::".$ou->{'name'}."::".$ou->{'obj'}."::".$ou->{'ens'}."\n";
                       print FILEOUS "$lin_ou";
                            
                   }
                }
                close (FILEOUS);
                $str=$text{'user_delete'}.$userou;
                &additional_log('delou.cgi', undef, $str);
                &webmin_log($config{'ou_list'}, undef, "delete");
                &unlock_file($config{'ou_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;
