# installupd-lib.pl
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL
# Common functions for Install / update

sub show_tta_serv_table
{
 &load_ttaprops();
 &scanSTTA_services("all");
$text_str="";
 &checkSTTA_services("all","status",\$text_str);
 &printSTTA_config(\$text_str,"=");
 $text_str=~s/STTA:/<tr><td><li>/g;
 $text_str=~s/\n/<\/b><\/td><\/tr>/g;
 $text_str=~s/==/<td><b>/g;
 $html_str.="<tr $cb><td><table width=100% border><tr $tb> <td><big><b>".$text{srvSTATUS}."</b></big></td></tr>";
 $html_str.="<tr $cb> <td><table width=100%><td>";
 $html_str.="<ul>$text_str</ul>";
 $html_str.="</table></table>";
 print $html_str;
 print "</td></tr>\n";
}

sub show_procServ
{
  my ($serv) = $_[0] || return undef;
  my ($outhtml) = $_[1] || 1;
  my ($edit_ps) = $_[2] || 1;

  $title_list=$serv."ps_list_title";
  $title_idx=$serv."ps_title";
  print "<tr $tb> <td align><big><b>$text{$title_idx}</b></big></td> </tr>\n";

  $daemon=$serv."_daemon";
  @line=split(/\//, $config{$daemon}, -1);
  $daemonStr=$line[$#line];

  return &get_processes_list($daemonStr,$outhtml,$edit_ps);
}

sub check_ps_serv
{
 print "<tr $cb> <td><table width=100%><td>";
 @ttaps=&show_procServ("tta");
 print "</tr></td></table><tr $cb> <td><table width=100%><td>";
 @ldapps=&show_procServ("ldap");
 print "</tr></td></table><tr $cb> <td><table width=100%><td>";
 @ldapcacheps=&show_procServ("ldapcache");
 print "</tr></td></table>\n";
}

sub check_ttabase
{
 if ( $debug eq 0 ) { $debug=1 };
 print "<tr $cb> <td><table width=100%>\n";
 print "<tr $tb><td><b><big>Tarantella:  </big>$text{chk_srv_title} - $config{tta_base} - </b></td></tr>\n";
   # loading object attributes ...
 $ens_obj=$config{tta_base};
    $op="attributes";
    $type_cmd="attributes";
    $status_cmd=&run_tta_cmd($type_cmd,$ens_obj);
    if ( !$status_cmd ) { 
         print "<tr><td> ".$text{'tta_noobject'}."</td></tr>";
         &html_errshow("tta");
    } else {
        # Assigning and collecting values to be used in the form ...
        %h_ttaobj=&load_ttaobj();
    }
    if ( $debug lt 2 )  { unlink($temp_f); }
    print "<tr $cb><td>".&text('viewlog_action',$stta_sid)."</b></td></tr>\n";
 print "</tr></td></table>\n";
}

sub out_javascript
{
    print "<SCRIPT SRC=\"global.js\">\n";
    print "</SCRIPT>\n";
    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
    print "var msg0=\"".$text{'msg_0'}."\";\n";
    print "var msg1=\"".$text{'msg_1'}."\";\n";
    print "var msg2=\"".$text{'msg_2'}."\";\n";
    print "var msg3=\"".$text{'msg_3'}."\";\n";
    print "var msg4=\"".$text{'msg_4'}."\";\n";
    print "var msg5=\"".$text{'msg_5'}."\";\n";
    print "var msg6=\"".$text{'msg_6'}."\";\n";
    print "var msg7=\"".$text{'msg_7'}."\";\n";
    print "/******************************************************************\n";
    print "   showWin(item)\n";
    print "*******************************************************************/\n";
    print "function showWin(item,args) {\n";
    print "     var str=item+\".cgi\"+args;\n";
    print "          window.open(str,item,",
                       "'toolbar=no,menubar=no,scrollbars=yes,width=1000,height=500,resizable=yes')\n";
    print "}\n";
    print "/******************************************************************\n";
    print "   Chek_fields(obj)\n";
    print "*******************************************************************/\n";
    print "function Chek_fields(obj) {\n";
    print "     if ( emptyField(obj.file) == true ) {\n";
    print "          alert(obj.file.name+\":\"+msg1+\"->\"+obj.file.value+\"<-\");\n";
    print "                    obj.file.focus();\n";
    print "          return false;\n";
    print "     }         \n";
    print "     return true;\n";
    print "}\n";
    print "//-->\n";
    print "</SCRIPT>\n";
}

sub out_initJS
{
    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
    print "/******************************************************************\n";
    print "   initJs()\n";
    print "*******************************************************************/\n";
    print "function initJs() {\n";
    print " $JScodeInit \n";
    print "}\n";
    print "</SCRIPT>\n";
}

sub out_head_instupd
{
     print "<tr $cb> <td><table width=100%>\n";
     print "<tr><td valig=top><h1><img SRC='images/duke_warn.gif' height=66 width=100 align=top>";
     print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
     print "<font color='#000099'>$text{installupd_title}</font></h1></td>\n";
     if ( $task eq "" ) { return };
     $opLock="unlockstta";
     if ( ! -r "$config_directory/$module_name/.install" ) { $opLock="lockstta"; }
     $opLock_msg=$opLock,"_msg";"unlockstta_m";
     print "<td align=rigth valign=top><form METHOD=POST name=installupd ACTION=\"stta_installupd.cgi?lck=$opLock&task=$task&step=$r&next=$nextStep&td=$tdone\" onSubmit=\"alert('".eval("\$text{$opLock.'_msg'}")."');\">\n";
     print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input TYPE=\"submit\" NAME=\"",$text{$opLock},"\" VALUE=\"",$text{$opLock},"\">".&hlink("<img SRC='images/info.gif' align=top border=0>",$opLock)."\n";
     print "</form>\n";
     print "</td></tr>\n";
}

#To set $tdone and ruleHash
sub set_hashes_rules
{
    if ( $task eq "" ) { return };

    my $task_proc = $task."_proc";
    my $task_steps = $task."_steps";
    my $task_brand = $task."_brand";

    $rulesHash{'proc'}=$rules{$task_proc};
    if ( defined($rules{$task_brand} ) ) { $rulesHash{'brand'}=$rules{$task_brand};}

    my @ruleline = split(/,/,$rules{$task},-1);
    my @tdoneline = split(/,/,$tdone,-1);
    my @stepsline = split(/,/,$rules{$task_steps},-1);
     
     if ( $tdone eq ""  ) {  
          $tdone="";
          for ( $i = 0 ; $i < $#ruleline ; $i++ ) { $tdone.= "0,"; }
          $tdone.= "0";
     }
     @tdoneline = split(/,/,$tdone,-1);
     $i=0;
     foreach $r (@ruleline) { 
        $rulesHash{$r}=$stepsline[$i].":".$tdoneline[$i];
        $i++;
     }
     $rulesHash{'ntask'}=$i;
     if ( $nextStep eq "" ) { $nextStep=$ruleline[0] };
}

# To track visited links !!! and set Step & nextStep
sub check_visit_tasks
{
    my @ruleline = split(/,/,$rules{$task},-1);
    my @lineStep =();

    if ( $Step eq "" ) { $Step=$ruleline[0] };
    if ( $nextStep eq "" ) { $nextStep=$ruleline[0] };

    $tdone="";
    $i=0;
    foreach $r (@ruleline) { 
        my @lineStep = split(/:/,$rulesHash{$r},-1);
        if ( $Step eq $r ) { 
             if ( $Step eq $nextStep ) { 
                  $j=$i+1;
                  if ( $j gt $#ruleline ) { $j=0 };
                  $nextStep=$ruleline[$j];
             }
             $lineStep[1]++; i
        }
        $tdone.= $lineStep[1].","; 
        $i++;
     }
}

sub make_steps_tables
{
     my  @statStep =();
     my @ruleline = split(/,/,$rules{$task},-1);

     print "</td></tr></table><tr $cb> <td><table width=100%>\n";
     print "<tr><td valign=top><table width=100% border align=center>\n";
     print "<tr $tb> <td valign=top><b>$text{$task}</b></td> </tr>\n";
     print "<tr $cb> <td valign=top><table><tr $cb><td><b>$text{work_secuence}:</b> </td></tr>\n";
     print "<tr><td valign=top><ul>\n";
     $i=0;
     foreach $r (@ruleline) {
        print "<li><a href=\"stta_installupd.cgi?task=$task&step=$r&next=$nextStep&td=$tdone\">";
        if ( $nextStep eq $r ) { print "<img SRC='images/next.gif'width=18 heigth=18 align=top border=0> <font color='#9830d2'><b>"; }
        print $text{$r};
        if ( $nextStep eq $r ) { 
             print "</b></font></a>($text{nextstep})\n";
        } else {
             print "</b></a>\n"; 
        }
        @statStep = split(/:/,$rulesHash{$r},-1);
        print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
        if ( $statStep[0] eq 1  && $statStep[1] gt 0 ) { print "<img SRC='images/ok.gif' align=top width=18 heigth=18 border=0>($text{'done'})"; }
        if ( $statStep[1] gt 0 ) { print "($text{'visited'})"; }
        print "<\li>\n"; 
        $i++;
     }
     print "</td></tr></table></table></td>\n";
     print "<td><table width=100% border>\n";
     print "<tr $tb> <td valign=top><b>$text{$Step}</b></td> </tr>\n";
     print "<tr $cb > <td valign=top><table width=100%>\n";
     print "<tr bgcolor=$white><td valign=top><pre>\n";

     # First try languages in order for particular $task/tmp/info .. if got it.. last
     # Second try languages in order for global $task/info.. if got it  ... last
     $infoTmpDirInstall= "$DirInstall/tmp/info";
     $infoDirInstall= "$DirInstall/info";
     foreach $l (@lang_order_list) {
           if ( &out_htmlfile("$infoTmpDirInstall/$l/$Step.html") eq 0 ) { last; };
           if ( &out_htmlfile("$infoDirInstall/$l/$Step.html") eq 0 ) { last; };
     }
     print "</pre></td></tr>\n";
     print "</td></tr></table></table></td></tr>\n";
     return 0;
}

sub out_sel_task 
{
     print "</td></tr></table><tr $cb> <td><table border align=center>\n";
     print "<tr $tb><td><b>$text{f_taskinstupd}  </b>\n";
     print "<form METHOD=POST name=installupd ACTION=\"stta_installupd.cgi?lck=$opLock\" onSubmit=\"alert('$text{warn_lockstta}');\"></td><td></td></tr>\n";
     print "<tr><td valign=top><table>\n";
     print "   <tr $cb><td valign=top>",&hlink("<b>$text{'f_taskinstupd'}</b>","ftaskinstupd"),"&nbsp;</td><td valign=top ><select NAME=\"task\">\n";
     foreach $item (keys (%rules)) {
         next if ( $item =~ /_/ );
         print "    <option VALUE=\"".$item."\" >".$text{$item}."  ";
     } 
     print "        </select></td></tr>\n";
     print "    <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\"></td>\n";
     print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
     print "</form>\n";
     print "</td></tr></table></table></td></tr>\n";
     print "\n";
}
sub get_file_updpatch
{
if ($file) { return };
    print "<table border align=center>\n";
    print "<tr $tb><td><b><big></big>".&text('tit_fileupdpatch',$task)."  </b>\n";
    print "<tr $cb> <td><table align=center>\n";
    print "<tr></tr>\n";
    print "<form METHOD=POST name=fupdpatch ACTION=\"stta_installupd.cgi?lck=$opLock\" onSubmit=\"return Chek_fields(this)\"></td><td></td></tr>\n";
    print "<td valign=top>",&hlink("<b>$text{'f_fileupdpatch'}</b>","ftargetfile"),"&nbsp;\n";
    print "<td valign=top><input TYPE=\"text\" NAME=\"file\" VALUE=\"\" cols=\"52\" size=\"52\" ALIGN=left ", &file_chooser_button("file",0,1,"/"),"</td></tr>\n";
    print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\">\n";
    print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
    print "  <input TYPE=\"hidden\" NAME=\"task\" VALUE=\"",$task,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"step\" VALUE=\"",$Step,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"next\" VALUE=\"",$nextStep,"\">\n";
    print "  <input TYPE=\"hidden\" NAME=\"td\" VALUE=\"",$tdone,"\">\n";
    print "      </form>\n";
 print "<tr></tr></table></table>\n";
 print "<tr $tb><td><b>$text{'warn_updpatch'}</b></td></tr>\n";
}

sub do_instupdfile 
{
   my $wkdir="$DirInstall/tmp";
   if ( -r "$wkdir/$task.pl" ) { do "$wkdir/$task.pl"; }
}

sub run_instupd_procedure
{
   if ( $rulesHash{'proc'} ne "" ) { eval ("&$rulesHash{proc}()") }
}

sub running_fileName
{
    my $wkdir="$DirInstall/tmp";
    if ( $wkdir eq "/tmp") { 
          print &text('dir_erraccess  DirInstall ',$wkdir)."\n";
          return;
    }
    print "<tr><td><table border align=center>\n";
    print "<tr $tb><td><b><big></big>".&text('tit_runfilename',$fileName)."  </b>\n";
    print "<tr $cb> <td><table align=center>\n";
    print "<tr $cb><td align=center valign=top><pre>\n";
    if ( ! -r $fileName || $fileName =~ /^(\/\*)/ ) {
         print &text('file_erraccess',$fileName)."  </b>\n";
    } else {
       if ( ! -d $wkdir ) { system("mkdir -p $wkdir")  }
       if ( ! -d $wkdir ) { 
               print &text('dir_erraccess',$wkdir)."\n";
       } else {
          system("rm -f $wkdir/* 2>/dev/null");
          $str="cd $wkdir; ";
          if ( $fileName =~  /.tar.gz$/ ) { $str.="cat $fileName | gunzip | tar xvf -" }
          elsif ( $fileName =~  /.cpio.gz$/ ) { $str.="cat $fileName | gunzip | cpio -idumv " }
          elsif ( $fileName =~  /.gz$/ ) { 
                 @line=split(/\//, $fileName, -1);
                 $nomf=$line[$#line];
                 $nomf=~s/.gz$//g;
                 $str.="cat $fileName | gunzip > $nomf";
          }
          elsif ( $fileName =~  /.cpio$/ ) { $str.="cpio -idumvI $fileName" }
          elsif ( $fileName =~  /.tar$/ ) { $str.="tar xvf $fileName" }
          elsif ( $fileName =~  /.shx$/ ) { $str.="sh $fileName" }
          else  { $str = "cp $fileName . ";}
          if ( ! &run_cmd($str))  { &html_errshow("cmd")};
          #print "$str\n";
       }
    }
    print "</pre></td></tr></table></table>\n";
}


sub lock_insupd_stta
{
   my $task_steps = $task."_steps";
   open(FILEINSTALL, ">>$config_directory/$module_name/.install");
        print FILEINSTALL "# ----$_[0]---$task---------------------------\n";
        if ( $task )  { print FILEINSTALL "task=$task\n"; }
        print FILEINSTALL "now=".&set_now($now)."\n";
        if ( $Step ) {print FILEINSTALL "step=$Step\n"; }
        if ( $nextStep ) {print FILEINSTALL "next=$nextStep\n"; }
        if ( $rules{$task} ) {print FILEINSTALL "rule=$rules{$task}\n"; }
        if ( $tdone ) {print FILEINSTALL "tdone=$tdone\n"; }
        if ( $rules{$task_steps} ) {print FILEINSTALL "steps=$rules{$task_steps}\n";}
        if ( $rulesHash{'proc'} ) {print FILEINSTALL "proc=$rulesHash{'proc'}\n"; }
        if ( $rulesHash{'brand'} ) {print FILEINSTALL "brand=$rulesHash{'brand'}\n"; }
   close (FILEINSTALL);
}

# Unlock STTA and move all to $task logfile 
sub unlock_insupd_stta
{
   my $Line=""; 
   unless ( open(FILELOG,"$config_directory/$module_name/.install") ) { return -1}
   &lock_insupd_stta($_[0]);
   @ary = <FILELOG>;
   close (FILELOG);
   my $ahoraf=&out_now_filename($now);
   my $logfile=$DirInstall."/".$ahoraf."_log";
   open(FILEINSTALL, ">>$logfile");
   foreach $line (@ary) { print FILEINSTALL "$line"; }
   close (FILEINSTALL);
   system (" rm -f $config_directory/$module_name/.install");
}

sub doStep
{
    if ( $Step eq "" ) { return };
    my $foo="";
    my @statStep = split(/:/,$rulesHash{$Step},-1);

    print "<table border align=center>\n";
    print "<tr $tb><td><b><big></big>$text{$Step}</b>\n";
    print "<tr $cb> <td><table align=center>\n";
    print "<tr $cb><td align=center valign=top><pre>\n";

    if ( $statStep[0] eq 1 &&  $statStep[1] gt 1  ) { 
         print &text('error_runonce_step', $text{$Step});
    } else { 
         &lock_insupd_stta($Step);
         if ( $Step eq "lockstta" )  { &lock_insupd_stta("lock"); }
         if ( $Step eq "dataconf" )  {
              $JScodeInit="showWin('config','')";
#         } elsif ( $Step eq "defslists" )  {
#         } elsif ( $Step eq "ldapbasedns" )  {
#         } elsif ( $Step eq "ttaensbase" )  {
         } elsif ( $Step eq "checklist" )  {
              $foo="";  # Do nothing so all test will are at end of page
              $JScodeInit="showWin(config.cgi,'')";
#         } elsif ( $Step eq "makelogsdir" )  {
#         } elsif ( $Step eq "maketools" )  {
#         } elsif ( $Step eq "expectchanges" )  {
         } elsif ( $Step eq "unlockstta" )  {
                   &unlock_insupd_stta;
#         } elsif ( $Step eq "fixstampdata" )  {
         } elsif ( $Step eq "doupdate" )  {
                   &run_instupd_procedure();
         } elsif ( $Step eq "doupgrade" ) {
                   &run_instupd_procedure();
         } elsif ( $Step eq "doupatch" ) {
                   &run_instupd_procedure();
# commenting following lines nothing is done if the "Step" has no code to handle
# interesting to manage "info" files 
#         } else  ( ! $Step =~ /readme/ )  {
#                   print &text('error_unknown_step', $Step, $text{$Step});
         }
    }
    print "</pre></td></tr></table></table>\n";
}
1;  # Return true
