# inststta-lib.pl
# STTA Rev. 1.3 Copyright (c) 2001-2003 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051137390- [2003_4_24_003629]
# Common functions for Install STTA

sub step_defslists
{
   local $NumStep=$Step."_1";
   local @listaf=("ou","grphosts","hosts","hostsuser","grpapps","apps","domains","links","acts","profcache");

   # Reset Defs list to defaults
   if ( $StepCheck eq $NumStep ) {
        if ( $in{nameorg} ) {
            print "<tr><td valign=top>".&text('making_files',&hlink("$text{'stta_sync'}","sttasync"))." </td></tr>\n";
            foreach $item (@listaf) {
               print "<tr><td valign=top> $text{user_made} ".&hlink(eval("\$text{index_$item}"),$item)."</td>\n";
               local $namefile=$item."_list";
               open(LISTFILE,"$root_directory/$module_name/install/deflists/$namefile");
                  local @ary = <LISTFILE>;
               close (LISTFILE);
               @linePath= split(/\//,$config{$namefile});
               $line=$config{$namefile};
               $line=~s/\/$linePath[$#linePath]//g;
               # Be sure dir is there !!!
               &make_dir_ifnot($line);
               open(FILEINSTALL, ">$config{$namefile}");
               foreach $line (@ary) { 
                    $line=~s/ROOTORG/$in{nameorg}/g;
                    $line=~s/ROOTDESC/$in{descrip}/g;
                    print FILEINSTALL "$line"; 
               }
               close (FILEINSTALL);
               if ( $item eq "hostsuser" ) { system("chmod 400 $config{$namefile}"); }
               &task_log("making $item", "install", "$config{$namefile}");
            }
        }
        local $destFile="";
        opendir(DIR, "$root_directory/$module_name/install/deflists");
            foreach $f (readdir(DIR)) {
               next if ( -d $f );
               next if ( $f =~ /_list$/ );
               $destFile="";
               if ( $f eq "orgunit-defaults" ) { $destFile=$config{'tta_defaults_ou'}; }
               if ( $f eq "person-defaults" ) { $destFile=$config{'tta_defaults_obj'}; }
               if ( $f eq "hosts-defaults" ) { $destFile=$config{'tta_defaults_hosts'}; }
               if ( $f eq "webtops" ) { $destFile=$config{'tta_webtops'}; }
               if ( $f eq ".passwd" ) { $destFile=$config{'hostsuser_list'}; }
               next if ( $destFile eq "" );
               $line=$destFile;
               $line=~s/\/$linePath[$#linePath]//g;
               &make_dir_ifnot($line);
               system ("cp $root_directory/$module_name/install/deflists/$f $destFile/$f; chmod 644 $destFile/$f"); 
               if ( $f eq ".passwd" ) { system ("chmod  600 $destFile/$f"); }
               print "<tr><td valign=top> $text{user_made} $f </td>\n";
            } 
        closedir(DIR);
        $destFile=$config{'monitor_scripts_path'}; 
        $line=$destFile;
        $line=~s/\/$linePath[$#linePath]//g;
        &make_dir_ifnot($line);
        system ("cp -pr $root_directory/$module_name/install/deflists/scripts/* $destFile"); 
        print "<tr><td valign=top> $text{user_made} scripts </td>\n";
        &make_list_logs();
        return true;
   } 
    print "<tr><td valign=top> <h2>".&text('defslists_msg1',&hlink("$text{'stta_sync'}","sttasync"))."</h2> </td></tr><table border width=100%><tr><td><table>\n";
    foreach $item (@listaf) {
              print "<tr><td valign=top><b>".eval("\$text{index_$item}")."</b></td>\n";
              print "<td valign=top>".eval("\$config{$item"."_list}")."</td></tr>\n";
     }
     print "</table></td><td><table>\n";
     print "<tr><td valign=top>",&hlink("<b>$text{'f_nameorg'}</b>","fnameorg"),"&nbsp;</td>";
     print "<td valign=top><input TYPE=\"text\" NAME=\"nameorg\" VALUE=\"".$text{'deflist_rootorg'}."\" cols=\"50\" size=\"50\" </td></tr>\n";
     print "<tr><td valign=top>",&hlink("<b>$text{'f_descrip'}</b>","fdescrip"),"&nbsp;</td>";
     print "<td valign=top><input TYPE=\"text\" NAME=\"descrip\" VALUE=\"".$text{'deflist_rootdesc'}."\" cols=\"50\" size=\"50\" </td></tr>\n";
     print "</table></td></tr></table>\n";
     &stepcheck_button($NumStep);
}

sub make_list_logs
{
       local %fadata=();
       local $fpath="$root_directory/$module_name/dataconf/list-logs";
       local $fname="$fpath/head.$current_lang";

       if ( ! -r $fname ) { $fname="$fpath/head.en" };
       open(LISTLOGFILE,"$fname");
             local @aryhead = <LISTLOGFILE>;
       close (LISTLOGFILE);
       opendir(DIR, $fpath);
         foreach $f (readdir(DIR)) {
               next if ( -d $f );
               next if ( $f =~ /^head/ );
               &read_file("$fpath/$f", \%fdata);
               $fname=$f."_name";
               next if ( ! $config{$fname} );
               open(FILEINSTALL, ">$config{$fname}");
               foreach $line (@aryhead) { 
                    print FILEINSTALL "$line"; 
               }
               foreach $item (keys (%fdata)) {
                     local $fcfg=$fdata{$item};
                     if ( $fcfg && $fcfg ne "") {
                         $line="#".$item."=".$config{$fcfg};
                         print FILEINSTALL "$line\n"; 
                     }
               }
               close (FILEINSTALL);
               print "<tr><td valign=top>".&text('making_files',$f)." </td></tr>\n";
               &task_log("making $f", "install", "$config{$namefile}");
        }
       closedir(DIR);
}

sub copyfiles_tosetup
{
    local $setupfiles="$root_directory/$module_name/$task/ldap";
    local $ttaldapName, $ttacacheName, $ttaldapPort, $ttacachePort;
    local $f="iplanet";
    if ( ! -d $_[2]) { system("mkdir -p $_[2]") };
    if ( $_[1] eq "OpenLDAP" ) {
         $f="openldap";
         print "<tr><td valign=top>".&text('making_files',$text{rc_files})." -- $_[2]/$f </td></tr>\n";
         &task_log("making $text{rc_files}", "install", "$_[2]/$f");
         system ("cat $setupfiles/$f"."_head >$_[2]/$f ");
         open(FILEINSTALL, ">>$_[2]/$f");
             if ( $_[0] eq "all" ) {
                   print FILEINSTALL "    ServerNames=\"$in{ttaldap} $in{ttacache}\"\n";
                   print FILEINSTALL "fi\n";
                   print FILEINSTALL "$in{ttaldap}_args=\" -h ldap://\$HOSTNAME:$config{ldap_port}/ -f /etc/openldap/$in{ttaldap}.conf \$debug_args \"\n";
                   print FILEINSTALL "$in{ttacache}_args=\" -h ldap://\$HOSTNAME:$config{ldapcache_port}/ -f /etc/openldap/$in{ttacache}.conf \$debug_args \"\n";
             } elsif ( $_[0] eq "ldap" ) {
                   print FILEINSTALL "ServerNames=\"$in{ttaldap}\"\n";
                   print FILEINSTALL "$in{ttaldap}_args=\" -h ldap://\$HOSTNAME:$config{ldap_port}/ -f /etc/openldap/$in{ttaldap}.conf \$debug_args \"\n";
             } elsif ( $_[0] eq "ldapcache" ) {
                   print FILEINSTALL "     ServerNames=\"$in{ttacache}\"\n";
                   print FILEINSTALL "fi\n";
             }
             print FILEINSTALL "SLAPD_BIN=\"$in{slapdpath}\"\n";
             print FILEINSTALL "\n";
         close (FILEINSTALL);
         system ("cat $setupfiles/$f"."_body >>$_[2]/$f ");
         system ("chmod +x $_[2]/$f");
         if ( ! -d "$config{stta_tools_dir}" ) {
              system ("mkdir $config{stta_tools_dir}");
              chmod(0777, "$config{stta_tools_dir}");
         }
    } else {
         print "<tr><td valign=top>".&text('making_files',$text{rc_files})." -- $_[2]/$f </td></tr>\n";
         &task_log("making $text{rc_files}", "install", "$_[2]/$f");
         open(LISTFILE,"$setupfiles/iplanet");
                  local @ary = <LISTFILE>;
         close (LISTFILE);
         open(FILEINSTALL, ">$_[2]/$f");
             foreach $line (@ary) {
                $line=~s/LDAPDIR/$config{ldap_dir}/g;
                print FILEINSTALL "$line\n";
             }
         close (FILEINSTALL);
         system ("chmod +x $_[2]/$f");
         system ("cp -p $_[2]/$f $config{stta_tools_dir}/$f ");
    }
}

sub make_openldap_conf
{
   local $setupfiles="$root_directory/$module_name/$task/ldap";
   local $ldapPasswd, $ldapUser, $ldapDB, $f,$fpath;
   local @ary=();
   if ( $_[0] eq "ldap" ) {
      open(LISTFILE,"$setupfiles/slapd.conf");
           @ary = <LISTFILE>;
      close (LISTFILE);
      $fpath="$_[1]/$in{ttaldap}.conf";
      $ldapName=$in{ttaldap};
      $ldapPasswd=$config{ldap_passwd};          
      $ldapUser=$config{ldap_user};          
      $ldapDB=$in{ttaldapdb}; 
      $ldapBase=$config{ldap_base}; 
   } elsif ( $_[0] eq "ldapcache" ) {
      open(LISTFILE,"$setupfiles/slapdcache.conf");
            @ary = <LISTFILE>;
      close (LISTFILE);
      $fpath="$_[1]/$in{ttacache}.conf";
      $ldapName=$in{ttacache};
      $ldapPasswd=$config{ldapcache_passwd};          
      $ldapUser=$config{ldapcache_user};          
      $ldapDB=$in{ttacachedb}; 
      $ldapBase=$config{ldapcache_base}; 
      $f="stta.schema";
      system ("cp $setupfiles/$f $_[1]/$f ");
      if ( -d "/etc/openldap/schema" ) { system ("cp $setupfiles/$f /etc/openldap/schema "); }
      print "<tr><td valign=top>".&text('making_files',$f)."</td></tr>\n";
   }
   open(FILEINSTALL, ">$fpath");
         foreach $line (@ary) {
             $line=~s/DIRECTORY-MANAGER/$ldapUser/g;
             $line=~s/ROOT-PASSWORD/$ldapPasswd/g;
             $line=~s/DIRECTORY-DBSPATH/$ldapDB/g;
             $line=~s/LDAPBASE-NAME/$ldapBase/g;
             $line=~s/LDAP-NAME/$ldapName/g;
             print FILEINSTALL "$line";
         }
   close (FILEINSTALL);
   if ( -d "/etc/openldap" ) { system ("cp $fpath /etc/openldap "); }
   print "<tr><td valign=top>".&text('making_files',$text{f_conf})." -- $fpath</td></tr>\n";
   &task_log("making $text{f_conf}", "install", "$fpath");
}

sub step_ldapbasedns
{
   local $NumStep=$Step."_1";
   local $f_path="$config_directory/$module_name/setup/etc";
   local $linecache=$config{'ldapcache_type'}.":".$config{'ldapcache_server'}.":".$config{'ldapcache_port'};
   local $lineconn=$config{'ldap_type'}.":".$config{'ldap_server'}.":".$config{'ldap_port'};
   local $ldap_up=""; 
   local $ldapdnok=""; 
   local $ldapcache_up=""; 
   local $ldapcachednok=""; 
   local $ttacacheappok="";
   local $ldaprun=$text{notrunning}." -- ".$text{dn_no};
   local $ldapcacherun=$text{notrunning}." -- ".$text{dn_no};
   $config{output_cgis}=0;

   if ( &find_byname($config{'ldap_daemon'}) ) {
       $ldap_up="1";
       $ldaprun=$text{running}." -- ".$text{dn_no};
       $ldap_server=1;
       $ldap_notdie=1;
       &connect();
       if ( $conn &&  $in{makeldapdn} ne "true" ) {
            $dn=$config{ldap_base};
            $cur_entry=&sel_ldap_obj($dn,"o=*");
            if ( defined($cur_entry)  && $cur_entry->dn eq $dn) { 
                 $ldapdnok="1";
                 $ldaprun=$text{running}." -- ".$text{dn_ok};
            }
       }
   }
   if ( $ldapcache_ok eq true && &find_byname($config{'ldapcache_daemon'}) ) {
       $ldapcache_up="1";
       $ldapcacherun=$text{running}." -- ".$text{dn_no};
       $ldapcache_server=1;
       $ldapcache_notdie=1;
       &connect_cache();
       if ( $conncache  &&  $in{makeldapcachedn} ne "true" ) {
             $dn_cache=$config{ldapcache_base};
             $cur_entry=&sel_cache_obj($dn_cache,"o=*");
             if ( defined($cur_entry)  && $cur_entry->dn eq $dn_cache) {
                  $ldapcachednok="1";
                  $ldapcacherun=$text{running}." -- ".$text{dn_ok};
                  $schema = $ldapcacheObj->schema();
                  local @objects = $schema->objectclasses();
                  foreach ( @objects) {
                        $oid = $schema->name2oid( "$_" );
                        next if ( ref($oid) eq 'ARRAY');
                        if ( $oid =~ /ttacacheapp/ ) {
                             $ttacacheappok="1";
                             $ldapcacherun .=" -- ".$oid;
                             last;
                        }
                  }
             }
       }
   }
   if ( $StepCheck eq $NumStep ) {
        print "<tr><td valign=top>".&text('making_files',$text{'ldapbasedns'})." </td></tr>\n";
        if ( $in{ttaldap} ) {
             if ( $ldap_up ne "1" ) {
                  print "<tr><td valign=top><b>$text{no_ldaprunning}</b></td></tr>\n";
                  if ( $config{ldap_type} eq "OpenLDAP" && $in{openldapdb} ) {
                  }
                  &copyfiles_tosetup("ldap",$config{ldap_type},$f_path);
                  &make_openldap_conf("ldap",$f_path);
                  if ( $in{makeldapdb} eq "true" ) {
                     if ( ! -d $in{ttaldapdb} && $in{ttaldapdb} ne "" ) { 
                         system ("mkdir -p $in{ttaldapdb}");
                         &do_ldapbase_dn();
                     }
                  }
                  print "<tr><td valign=top><b>".&text('rc_ldaps',"$f_path")."</b></td>\n";
                  print "<tr><td valign=top><h2>". &text('startup_service',"LDAP")."</h2></td>\n";
            }
            if ( $in{makeldapdn} eq "true" && $ldapdnok ne "1" ) { &do_ldapbase_dn() }
            if ( $in{ttaldaplogin} eq "true" ) { &do_ttaldap_login() }
        }
        if ( $config{'is_ldapcache'} ne "true" ) { return true}
        if ( $in{ttacache} ) {
             if ( $ldapcache_up ne "1" ) {
                  print "<tr><td valign=top><b>$text{no_ldapcacherunning}</b></td></tr>\n";
                  if ( $config{ldap_type} eq $config{ldapcache_type} ) {
                       &copyfiles_tosetup("all",$config{ldap_type},$f_path);
                  } else { &copyfiles_tosetup("ldapcache",$config{ldapcache_type},$f_path); }
                  &make_openldap_conf("ldapcache",$f_path);
                  if ( $in{makeldapcachedb} eq "true" ) {
                     if ( ! -d $in{ttacachedb} && $in{ttacachedb} ne "" ) { 
                          system ("mkdir -p $in{ttacachedb}");i
                          &do_ldapcachebase_dn();
                     }
                  }
                  print "<tr><td valign=top><b>".&text('rc_ldaps',"$f_path")."</b></td>\n";
                  print "<tr><td valign=top><h2>". &text('startup_service',"LDAP CACHE")."</h2></td>\n";
             }
             if ( $in{makeldapcachedn} eq "true" && $ldapcchednok ne "1" ) { &do_ldapcachebase_dn() }
             if ( $in{ttacachefix} eq "true" ) { &do_ttacachefix() }
        }
        # In case of OpenLdap most of the work can be done
        # having this it is let us adjust TTA array
        return true;
   } 
   print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('ldapbasedns_msg1',$config{'ldap_servertitle'},$config{ldap_base})."</h2> </td></tr>\n";
   print "<tr><td><table><tr><td valign=top>$lineconn -- $ldaprun</td></tr><table border width=100%><tr><td><table>\n";
   if ( $ldap_up ne "1" && ! $in{ttaldap} ) {
        print "<tr><td valign=top><b>$text{no_ldaprunning}</b></td></tr>\n";
        if ( $config{ldap_type} eq "OpenLDAP" && -d "/etc/openldap" ) {
             print "<tr><td valign=top>",&hlink("<b>$text{'f_openldapname'}</b>","fopenldapname"),"&nbsp;</td>\n";
             print "<td valign=top><input TYPE=\"text\" NAME=\"ttaldap\" VALUE=\"ttaldap\" cols=\"50\" size=\"50\" </td></tr>\n";
             print "<tr><td valign=top>",&hlink("<b>$text{'f_openldapdb'}</b>","foenldapdb"),"&nbsp;</td>\n";
             print "<td valign=top><input TYPE=\"text\" NAME=\"ttaldapdb\" VALUE=\"/opt/ldapdbs/ttaldap\" cols=\"50\" size=\"50\" </td></tr>\n";
             print "<tr><td valign=top> SLAPD daemon Path &nbsp;</td>\n";
             print "<td valign=top><input TYPE=\"text\" NAME=\"slapdpath\" VALUE=\"/usr/local/libexec/slapd\" cols=\"50\" size=\"50\" </td></tr>\n";
             $slapdpath="1";
             print "<tr><td valign=top> $text{create} &nbsp;</td>\n";
             print "<td><input type=checkbox name=makeldapdb value=\"true\"  checked > DBS</td></tr>\n";
        }
    } else {
       print "  <input TYPE=\"hidden\" NAME=\"ttaldap\" VALUE=\"",$config{ldap_server},"\">\n";
#       if  ( $ldapdnok ne "1" ) {
            print "<tr><td valign=top> $text{create}&nbsp;</td>\n";
            print "<td><input type=checkbox name=makeldapdn value=\"true\" > $config{ldap_base}</td></tr>\n";
#       }
       print "<tr><td valign=top>".&hlink("<b>$text{'tta_ldaplogin'}</b>","ttaldaplogin")."&nbsp;</td>\n";
       print "<td><input type=checkbox name=ttaldaplogin value=\"true\" checked ></td></tr>\n";
    }
    if ( $config{'is_ldapcache'} ne "true" ) {
          print "</table></td><td><table>\n";
          print "</table></td></tr></table>\n";
          &stepcheck_button($NumStep);
          return;
    }
    print "</table></td></tr></table>\n";
    print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('ldapcachebasedns_msg1',$config{'ldapcache_servertitle'},$config{ldapcache_base})."</h2> </td></tr>\n";
    print "<tr><td><table><tr><td valign=top>$linecache -- $ldapcacherun </td></tr><table border width=100%><tr><td><table>\n";
   if ( $ldapcache_up ne "1" && ! $in{ttacache} ) {
        print "<tr><td valign=top><b>$text{no_ldapcacherunning}</b></td></tr>\n";
        if ( $config{ldapcache_type} eq "OpenLDAP" && -d "/etc/openldap" ) {
             print "<tr><td valign=top>",&hlink("<b>$text{'f_openldapname'}</b>","fopenldapname"),"&nbsp;</td>\n";
             print "<td valign=top><input TYPE=\"text\" NAME=\"ttacache\" VALUE=\"ttacache\" cols=\"50\" size=\"50\" </td></tr>\n";
             print "<tr><td valign=top>",&hlink("<b>$text{'f_openldapdb'}</b>","foenldapdb"),"&nbsp;</td>\n";
             print "<td valign=top><input TYPE=\"text\" NAME=\"ttacachedb\" VALUE=\"/opt/ldapdbs/ttacache\" cols=\"50\" size=\"50\" </td></tr>\n";
             if ( $slapdpath ne "1" ) {
                  print "<tr><td valign=top> SLAPD daemon Path &nbsp;</td>\n";
                  print "<td valign=top><input TYPE=\"text\" NAME=\"slapdpath\" VALUE=\"/usr/lib/openldapcache/slapd\" cols=\"50\" size=\"50\" </td></tr>\n";
             }
             print "<tr><td valign=top> $text{create} &nbsp;</td>\n";
             print "<td><input type=checkbox name=makeldapcachedb value=\"true\"  checked > DBS</td></tr>\n";
        }
    } else {
       print "  <input TYPE=\"hidden\" NAME=\"ttacache\" VALUE=\"",$config{ldapcache_server},"\">\n";
#       if  ( $ldapcachednok ne "1" ) {
          print "<tr><td valign=top> $text{create}&nbsp;</td>\n";
          print "<td><input type=checkbox name=makeldapcachedn value=\"true\" > $config{ldapcache_base}</td></tr>\n";
#       }
       print "<tr><td valign=top>".&hlink("<b>$text{'tta_cachefix'}</b>","ttacachefix")."&nbsp;</td>\n";
       print "<td><input type=checkbox name=ttacachefix value=\"true\" checked> $config{ldapcache_base}</td></tr>\n";
    }
    print "</table></td></tr></table>\n";
    &stepcheck_button($NumStep);
}

sub do_ldapbase_dn
{
    local $f_path="$config_directory/$module_name/setup";
    $dn = $config{ldap_base};
    $dn=~ s/ $//g;
    local @line = split(/,/, $dn);
    $ouname=$line[0];
    $ouname=~s/o=//;
    # Run script "slapadd" for OpenLDAP
    if ( $config{ldap_type} eq "OpenLDAP" ) {
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "dn: ".$dn."\n";
                print TEMPF "objectClass: top\n";
                print TEMPF "objectClass: organization\n";
                print TEMPF "o: $ouname\n";
         close(TEMPF);
         system ("$f_path/etc/openldap stop");
         $str="slapadd -f $config{ldap_conf}  -l $temp_f ";
         system ("cp $temp_f $f_path/ldap.ldif ");
         &run_cmd();
         system ("$f_path/etc/openldap start");
         return;
    }
    $ldap_server=1;
    &connect ();
    %ldapObjHash=();
    @ldapObjArr=();

    push (@ldapObjArr,'o',$ouname);
    push (@ldapObjArr,'objectClass',["top", "organizationalunit"]);
    %ldapObjHash=@ldapObjArr;
    $conn = $ldapObj->add ( $dn, attr => [ @ldapObjArr ] );
    if ( $err = $conn->code ) { $status_con=1; }
    &ldap_logerr("add ou");
    &task_log("making ldap DN", "install", "$config{ldap_base}");
}

sub do_ldapcachebase_dn
{
     local $f_path="$config_directory/$module_name/setup";
     $dn = $config{ldapcache_base};
     $dn=~ s/ $//g;
     local @line = split(/,/, $dn);
     $ouname=$line[0];
     $ouname=~s/o=//;

     # Run script "slapadd" for OpenLDAP
     if ( $config{ldapcache_type} eq "OpenLDAP" ) {
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "dn: ".$dn."\n";
                print TEMPF "objectClass: top\n";
                print TEMPF "objectClass: organization\n";
                print TEMPF "o: $ouname\n";
         close(TEMPF);
         system ("cp $temp_f $f_path/ldapcache.ldif ");
         system ("$config{stta_tools_dir}/openldap stop");
         $str="slapadd -f $config{ldapcache_conf}  -l $temp_f ";
         &run_cmd();
         system ("$config{stta_tools_dir}/openldap start");
         return;
     }
     @ldapObjArr=();
     %ldapObjHash=();

     $ldapcache_server=1;
     &connect_cache ();
     push (@ldapObjArr,'o',$ouname);
     push (@ldapObjArr,'objectclass',["top", "organizationalunit"]);
     push (@ldapObjArr,'description',$ouname);
     %ldapObjHash=@ldapObjArr;
     $conncache = $ldapcacheObj->add ($dn,attr => [ @ldapObjArr ]);
     if ( $err = $conncache->code ) { $status_con=-1; };
     $dn_cache=$dn;
     &ldapcache_logerr("add ou");
     &task_log("making ldap Cache DN", "install", "$config{ldapcache_base}");
}

sub do_ttaldap_login 
{
        $exec_str = "tta";
        $cmd_str = "config edit";
        $temp_f = &tempname();
        open(TEMPF, ">$temp_f");
                print TEMPF "--login-ens 1  --array\n";
                print TEMPF "--login-ldap-url \"ldap://$config{ldap_server}\" --array\n";
                print TEMPF "--login-ldap 1 --array\n";
        close(TEMPF);
        $str="$config{'tta_aliascmd'} config edit --file $temp_f";
        &run_cmd();
        $cmd_str = "passcache delete";
        $str="$config{'tta_aliascmd'} passcache delete --ldap\n";
        &run_cmd();
        $cmd_str = "passcache new";
        $str="$config{'tta_aliascmd'} passcache new --ldap --resuser \"$config{ldap_user}\" --respass \"$config{ldap_passwd}\"\n";
        &run_cmd();
        &task_log("making tta-ldap login", "install", "$config{ldap_base}" );
}

sub do_ttacachefix
{
     local $ttascript="$root_directory/$module_name/install/tta/ldapcachecfg.ttascript";
     $exec_str = "tta";
     $cmd_str = "config edit";
     $str="$config{'tta_aliascmd'} config edit --file $ttascript";
     &run_cmd();
     &task_log("making tta Array fix for cache", "install", "$config{tta_base}" );
}

sub do_make_ttadb
{
     $exec_str = "tta";
     $cmd_str = "ensrecreate";
     $str=$config{'tta_ensrecreate'}." ".$config{tta_base};
     if ( ! -x $config{'tta_ensrecreate'} ) {
          $str="$root_directory/$module_name/ensrecreate.sh ".$config{tta_base};
     }
     &run_cmd();
     &task_log("making tta DBS", "install", "$config{tta_base}" );
}

sub step_ttaensbase
{
   local $NumStep=$Step."_1";
   local $f_path="$config_directory/$module_name/setup/tta";
   local $tta_up=""; 
   local $ttaensok=""; 
   local $ttarun=$text{notrunning};
   if ( &find_byname($config{'tta_daemon'}) ) {
       $tta_up="1";
       $ttarun=$text{running};
   }
   if ( $StepCheck eq $NumStep  ) {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('ttaensbase_msg1',$config{tta_base})."</h2></td><td> $ttarun</td></tr>\n";
        print "<tr><td><table>\n";
        if ( $in{makettadb} eq "true" ) { &do_make_ttadb() }
        if ( $in{ttaldaplogin} eq "true" ) { &do_ttaldap_login() }
        if ( $in{ttacachefix} eq "true" ) { &do_ttacachefix() }

        print "</table></td><td><table>\n";
   } else {
        $config{output_cgis}=0;
        &check_ttabase();
        print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('ttaensbase_msg1',$config{tta_base})."</h2></td><td> $ttarun</td></tr>\n";
        print "<tr><td><table>\n";
    print "<tr><td valign=top>".&hlink("<b>$text{'tta_dbcreate'}</b>","ttadbcreate")."&nbsp;</td>\n";
    print "<td><input type=checkbox name=makettadb value=\"true\" > $config{tta_base}</td></tr>\n";
    print "<tr><td valign=top>".&hlink("<b>$text{'tta_ldaplogin'}</b>","ttaldaplogin")."&nbsp;</td>\n";
    print "<td><input type=checkbox name=ttaldaplogin value=\"true\" checked ></td></tr>\n";
    if ( $config{'is_ldapcache'} eq "true" ) {
       print "<tr><td valign=top>".&hlink("<b>$text{'tta_cachefix'}</b>","ttacachefix")."&nbsp;</td>\n";
       print "<td><input type=checkbox name=ttacachefix value=\"true\" checked> $config{ldapcache_base}</td></tr>\n";
    }
    &stepcheck_button($NumStep);
    print "</table></td><td><table>\n";
   }
}

sub step_makelogsdir
{
   local $NumStep=$Step."_1";
   if ( $StepCheck eq $NumStep  ) {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>$text{makelogsdir}</h2></td></tr>\n";
        &make_list_logs();
        # Use config.info to create config inputs
        &read_file("$module_dir/config.info", \%info, \@info_order);
        %einfo = %info;
        foreach $o (@lang_order_list) {
             &read_file("$module_dir/config.info.$o", \%info, \@info_order);
        }
        @info_order = &unique(@info_order);
        foreach $c (@info_order) {
	          @p = split(/,/, $info{$c});
	     @ep = split(/,/, $einfo{$c});
	     if (scalar(@ep) > scalar(@p)) {
		     push(@p, @ep[scalar(@p) .. @ep-1]);
		     }
	     $qw = $config{$c} =~ /'/ ? '"' : "'";
	     if ($to_init eq true ) { $p[0]=eval $p[0]; }
	     if ($p[1] == 7 ) {
                     if ( ! -d $config{$c})  {
                         system (" mkdir -p $config{$c}");
                         chmod(0777, $config{$c});
                         $str=$text{'user_made'}.$p[0]." ($c)->  ".$config{$c};
                         &webmin_log("make file", "$c", "str");
                         print "<tr><td>$str</td></tr>\n";
                     }
	     }
        }
        &task_log("making logs dirs", "install", "$config{tta_base}" );
        print "</td></tr><table>\n";
        return true;
   } else {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>$text{makelogsdir}</h2></td></tr>\n";
        print "<tr><td><table>\n";
        &stepcheck_button($NumStep);
        print "</table></td><td><table>\n";
  }
}

sub step_maketools
{
   local $NumStep=$Step."_1";
   if ( $StepCheck eq $NumStep  ) {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('tools_msg1',"$config{tta_base}")."</h2></td></tr>\n";
        print "<tr><td><table>\n";
        # make tools for commands 
        $str="cd $root_directory/$module_name; ./mktools.sh $current_lang ";
        if ( $debug eq "4" ) { print "$str <br>" };
        print "$text{user_made} <br> ";
        &run_cmd();
        &task_log("making tools", "install", "$config{tta_base}" );
        print "</table></td><td><table>\n";
        return true;
   } else {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>".&text('tools_msg1',$config{tta_base})."</h2></td></tr>\n";
        print "<tr><td><table>\n";
        &stepcheck_button($NumStep);
        print "</table></td><td><table>\n";
  }
}

sub step_expectchanges
{
   local $NumStep=$Step."_1";
   local $ttavers=&get_ttaversion();
   local $setupfiles="";
   
   if ( $ttavers =~ / 3.2/ ) {
        $setupfiles="$root_directory/$module_name/install/tta/expect/3.2";
   } elsif ( $ttavers =~ / 3.3/ ) {
        $setupfiles="$root_directory/$module_name/install/tta/expect/3.3";
   }
   local $ttaexpect="$config{tta_dir}/var/serverresources/expect";
   if ( $StepCheck eq $NumStep  ) {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>$ttavers</h2></td></tr>\n";
        print "<tr><td valign=top><h2>".&text('expect_msg1',"$config{tta_base}")."</h2></td></tr>\n";
        print "</table></td><td><table>\n";
        print "<tr><td><table>\n";
        system("mkdir $ttaexpect/org");
        system("mkdir $ttaexpect/new");
        opendir(DIR, $setupfiles);
            foreach $f (readdir(DIR)) {
               next if ( -d $f );
               if ( $f =~ /.exp$/ ) {
# If run this more than ones will overwrite expect org directory so use STTA org files instead !!!
#                 system ("cp $ttaexpect/$f $ttaexpect/org/$f ; chmod 755 $ttaexpect/org/$f"); 
                  system ("cp $setupfiles/org/$f $ttaexpect/org/$f ; chmod 755 $ttaexpect/org/$f");
                  system ("cat $setupfiles/$f | sed \"s,STTATOOLSDIR,$config{stta_tools_dir},g\" >$ttaexpect/$f ; chmod 755 $ttaexpect/$f"); 
                  system ("cat $setupfiles/$f | sed \"s,STTATOOLSDIR,$config{stta_tools_dir},g\" >$ttaexpect/new/$f ; chmod 755 $ttaexpect/new/$f"); 
                  print "<tr><td valign=top> $text{user_made} $f </td>\n";
               }
            } 
        closedir(DIR);
        print "<tr><td>".&text('expect_back',"$ttaexpect/org")."</td></tr>\n";
        print "<tr><td>".&text('expect_new',"$ttaexpect/new")."</td></tr>\n";
        &task_log("making expect", "install", "$config{tta_base}" );
        print "</table></td><td><table>\n";
        return true;
   } else {
        print "<tr><td><table border width=100%><tr><td valign=top><h2>$ttavers</h2></td></tr>\n";
        print "<tr><td valign=top><h2>".&text('expect_msg1',"$config{tta_base}")."</h2></td></tr>\n";
        if ( $debug eq "4" ) { print "<tr><td valign=top> -->".$setupfiles." </td></tr>\n"; }
        print "<tr><td><table>\n";
        &stepcheck_button($NumStep);
        print "</table></td><td><table>\n";
  }
}

# Setup error handler for remote hosts commands
sub stta_sync_error
{
   $remote_err_msg=join("", @_);
}

sub get_srvstta_files
{
    local %newconfig=(); 

    $config{'stta_sync'} .= $sttasrv;
    $config{'stta_hosts'} .= $sttasrv;
    $remote_err_msg="";
    &remote_error_setup(\&stta_sync_error);
    return if ( !$sttasrv || $sttasrv eq "" );
    &remote_foreign_require($sttasrv, "stta", "sttaremote-lib.pl");
    if ($remote_err_msg) { 
         &webmin_log("$sttasrv get_data sync", "install", "$remote_err_msg");
         &html_errshow("sync",$remote_err_msg);
              return -1;
    }
    local %remote_config=&remote_foreign_call($sttasrv,"stta", "get_dataconfig");
    if ( $remote_config{tta_base} && $remote_config{tta_base} ne "" ) {
         local $remoteCfgDir=&remote_foreign_call($sttasrv,"stta","get_cfgdir");
         if ( ! $remoteCfgDir || $remoteCfgDir eq "" ) { return -1 };
         local @ordercfgs=&remote_foreign_call($sttasrv,"stta", "get_ordercfgs");
         if ( $#ordercfgs le 0 ) { return -1 };
         local $d="";
         $configDir="$config_directory/$module_name/dataconf";
         if (!(-d $configDir )) { mkdir($configDir, 0777); }
         foreach $d (@ordercfgs) {
                 &remote_read($sttasrv, "$configDir/$d","$remoteCfgDir/$d");
                 if ($remote_err_msg) {
                     &webmin_log("$shost $_[2] sync", "remote_read","$remote_err_msg");
                     &html_errshow("sync",$remote_err_msg);
                 }
                 &webmin_log("$shost $_[2] sync", "remote_read","$remote_config{$_[2]}");
         }
         &remote_read($sttasrv, "$configDir/ordercfgs", "$remoteCfgDir/ordercfgs");
         local $remoteCfgDir=&remote_foreign_call($sttasrv,"stta","get_basecfgdir");
         $configDir="$config_directory/$module_name";
         &remote_read($sttasrv, "$configDir/config", "$remoteCfgDir/config");
         &read_file("$configDir/config", \%newconfig);
         $newconfig{'INSTALL'}="$now_is  $remote_user  [ $task ->  $sttasrv ] ";
         &write_file("$configDir/config", \%newconfig);
         print "<tr $cb> <td align=center>$text{user_made} .... $sttasrv  </td></tr>\n";
    }
    &remote_finished();
    if ( $debug gt 1 ) { print "</td></tr>"; }
}

1;  # Return true
