#!/usr/bin/perl
# modapps.cgi
# ...  Services with Tarantella.
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL 
# stta: (@#) 1.31051043346- [2004_01_29_102036]

require './stta-lib.pl';
do 'app-lib.pl';
&ReadParse();
if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) {
     do "ldap-lib.pl";
}
&foreign_require("proc", "proc-lib.pl");

&headerstta($text{'index_modapp'}, "", "modapp",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );
print "<hr><p>\n"; 

 $type_cmd = $in{ttacmd};
 print "<table width=100% border>\n";
 print "<tr $tb> <td><font color='$revtext'><b>$text{'titidx_modapp'}</b></td> </tr>\n";
 print "<tr $cb> <td><table width=100%>\n";
 print "<center>\n";

 $status_cmd=0;
 $descrip=$in{descrip};
 $maxsess=$in{maxsess};
 $aliasapp=$in{aliasapp};
 $appname=$in{appname};
 $grpalias=$in{grpapps};
 $appserv=$in{appserv};
 $tta_AppType= $in{ttaapptype};
 @appdef=&get_tta_appdef($tta_AppType);

 if ( !$aliasapp) { &footHere; exit }  ;
 $alias=$aliasapp;
 %h_apps=&list_apps();
 if ( $h_apps{$aliasapp}->{'alias'} ne $aliasapp ) {
         $err_msg=$text{'list_errno'};
         &footHere;
         exit;
 }
 $dn_app=$h_apps{$aliasapp}->{'obj'};
 $organizationname=$h_apps{$aliasapp}->{'name'};
 $ens_app=$h_apps{$aliasapp}->{'ens'};     
 $grpalias=$h_apps{$aliasapp}->{'grpalias'};     
 if ( $dn_app eq "" ) {
       $dn_org = "ou=$appname";
       $ens_org= "cn=$appname";
 } else {
       $dn_org= $dn_app;
       $ens_org= $ens_app;
 }
 $ens_obj = "--name \"".$config{tta_base}."/$ens_org\"";
 $obj_descrip=$in{descrip};

 $| = 1;
 $temp = &tempname();
 open(TEMP, ">$temp");
      print TEMP "#!/bin/sh\n";
      print TEMP $env;
      print TEMP "export $export\n" if ($export);
      print TEMP "$str\n";
 close(TEMPF);
 $temp_f = &tempname();
 open(TEMPF, ">$temp_f");
     print TEMPF "$ens_obj \\\n";
     print TEMPF "  --description \"$obj_descrip\"\\\n"; 
     foreach $data (@appdef) {
         next  if ( $data->{att} eq "name" );
         next  if ( $data->{att} eq "description" );
         next  if ( $data->{att} eq "appserv" );
         $fname=$data->{att};
         if ( $in{$fname} ne ""  && $in{$fname} ne "none" && $in{$fname} ne $data->{ops} ) {
              print TEMPF "  --$fname \"$in{$fname}\"\\\n"; 
         }
     }
     %h_hosts=&list_hosts();
     $hosts_list="";
     $appserv =~ s/\r|\n/ /g;
     foreach $h (split(/ /, $appserv)) {
             next if ( $h eq "" );
             next if ( $h_hosts{$h}->{ens} eq "" );
             if ( $hosts_list ne "" ) { $hosts_list .= " " }
             $hosts_list .="\"$config{tta_base}/".$h_hosts{$h}->{ens}."\"";
     }
     print TEMPF "  --appserv $hosts_list\\\n";
     print TEMPF "\n"; 
 close(TEMPF);
 if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
       print "</pre><tr $cb><td><b>$text{'user_update'}... Tarantella </td></tr>\n";
 } else { &html_errshow("tta")};
 # Default object para LDAP cache ...
 if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
         $dnldap = "default";
         $default_obj="cn=$config{ldapcache_defaultobj}";
         $dn_cache=$dn_org;
         $task="mod";
         $ldapcache_server=1;
         &connect_cache();
         $dn_wk=$default_obj.",".$dn_cache.",".$config{ldapcache_base};
	 $cur_entry=&sel_cache_obj($dn_wk,"cn=*");
         if ( !defined($cur_entry) || $cur_entry->dn ne $dn_wk) { $task="add"; };
         @tta3tier=&set_tta3tier($default_obj,$dn_cache,$dnldap,$task,"app");
         if ( &cache_obj(\%tta3tier) eq 0 ) {
              if ( $config{ldapcache2_server}.length gt 0 ) {
                   $ldapcache_server=2;
                   &connect_cache ();
                   if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
              }
         } else { &html_errshow("ldap")};
 } 
 $linapp=$aliasapp."::".$appname."::".$grpalias."::"."$dn_org"."::"."$ens_org"."::".$maxsess."::".$tta_AppType."::".$descrip."::";
 &out_objslist( "modify", "modapps.cgi", "apps", "apps_list", $aliasapp, $linapp, \%h_apps );
 print "<tr><p><b>".$text{'user_update'}.$aliasapp." ->  ".$appname."</b></tr>\n";
 print "<tr></tr>\n";
 print "<tr><tr></table></table><hr><p>\n";
 &footer($config{'back_return'},$text{'index'});
 print "<p><p><p>\n";
 do "footer.pl";
 exit;
