#!/usr/bin/perl
# addapp.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';
&headerstta($text{'index_addapp'}, "", "addapp",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" ); 
print "<hr><p>\n";
&foreign_require("proc", "proc-lib.pl");
&ReadParse();
if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) {
     do "ldap-lib.pl";
}
 
 $type_cmd = $in{ttacmd};
 $tta_norun = $in{ttanorun};
 $appname=$in{appname};
 $grp_apps=$in{grpapps};
 $obj_descrip=$in{descrip};
 $aliasapp=$in{aliasapp};
 $appserv=$in{appserv};
 $maxsess=$in{maxsess};
 $tta_AppType= $in{ttaapptype};
 @appdef=&get_tta_appdef($tta_AppType);

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

 if (!$appname)  { &footHere; exit }  ;
    $alias=$aliasapp;
    %h_app=&list_apps();
    if ( $h_app{$aliasapp}->{'alias'} eq $aliasapp ) {
         $err_msg=$aliasapp.": ".$text{'list_err'};
         &footHere;
         exit;
    }
    $grp_name="";
    $grp_descrip="";
    $dn_grpapps="";
    $ens_grpapps="";
    %h_grpapps=&list_grpapps();
    if ( $h_grpapps{$grp_apps}->{'alias'} eq $grp_apps ) {
         $dn_grpapps=$h_grpapps{$grp_apps}->{obj};
         $ens_grpapps=$h_grpapps{$grp_apps}->{ens};
         $grp_name=$h_grpapps{$grp_apps}->{name};
         $grp_descrip=$h_grpapps{$grp_apps}->{descrip};
    } else {
         $err_msg="grp_apps: ".$text{'list_errno'};
         &footHere;
         exit;
    }
    if ( $dn_grpapps eq "" ) {
         $dn_app = "ou=$appname";
         $ens_app ="cn=$appname";
    } else {
         $dn_app = "ou=$appname,$dn_grpapps";
         $ens_app ="$ens_grpapps/cn=$appname";
    }
    $dnldap = "default";
    $default_obj="cn=$config{ldapcache_defaultobj}"; 
    $ens_obj = "--name \"".$config{tta_base}."/$ens_app\"";
    $dn_org=$dn;

    $| = 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(TEMP);
    $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 ( $tta_norun eq "true" ) { $type_cmd = "no_run $type_cmd"; };
    if (&run_tta_cmd($type_cmd,$ens_obj,$temp_f)) { 
            print "</pre>\n";
    } else { &html_errshow("tta")};
    if ( $config{'is_ldapcache'} eq "true" && $in{tta3tier_login} ) { 
            $ldapcache_server=1;
            &connect_cache ();
            @tta3tier=&set_tta3tier($default_obj,$dn_app,$dnldap,"add","app");
            if ( &cache_obj(\%tta3tier) ne 0 ) { 
                 &html_errshow("ldap");
                 &footHere; exit;
            };
            if ( $config{ldapcache2_server}.length gt 0 ) {
                     $ldapcache_server=2;
                     &connect_cache ();
                     if ( &cache_obj(\%tta3tier) ne 0 ) { &err_replication() }
            }
    }
    print "<tr></tr>\n";
    $lin_app=$aliasapp."::".$appname."::".$grp_apps."::".$dn_app."::".$ens_app."::".$maxsess."::".$tta_AppType."::".$obj_descrip."::";
    &out_objslist( "add", "addapps.cgi", "apps", "apps_list", $aliasapp, $lin_app, undef );
    print "<tr><p><b>".$text{'user_made'}.$appname." ->  ".$grp_name."</b></tr>\n";
    if ( $debug lt 2 )  { unlink($temp_f); }
 &footHere;
 exit;
