# installupd-lib.pl
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# Common functions for Install / update

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 "var msg8=\"".$text{'msg_8'}."\";\n";
    print "var msg9=\"".$text{'msg_9'}."\";\n";
    print "var msg10=\"".$text{'msg_10'}."\";\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 "</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";
     if ( $task eq "" ) {
         print "<font color='#000099'>$text{installupd_title}</font></h1></td>\n";
     } else {
         print "<font color='#000099'>$text{$task}</font></h1></td>\n";
     }
     print "<td valign=top >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>";
     if ( $STTA_LOCK eq 1 ) {
         print "(".$text{mode_run}.")";
     } else {
         print "(".$text{mode_browse}.")";
     }
     print "</b></td>\n";
}

sub head_lock_button
{
     if ( $button_lock eq 1 ) { return }
     $opLock="unlockstta";
     if ( $STTA_LOCK eq 0  ) { $opLock="lockstta"; }
     $opLock_msg=$opLock,"_msg";
     print "<td align=rigth valign=top><form METHOD=POST name=\"f_lockbutton\" ACTION=\"stta_installupd.cgi?lck=$opLock&task=$task\" onSubmit=\"";
     if ( $opLock eq "lockstta" ){ 
         if ( $STTA_LOCK eq 0 ) { print " alert('".eval("\$text{$opLock.'_msg'}")."');"; }
     } else {
         if ( $STTA_LOCK eq 1 ) { print " alert('".eval("\$text{$opLock.'_msg'}")."');"; }
#          if ( $task ne "" ) { print ";return confirm(msg10);"; }
     }
     print "\">\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";
     $button_lock=1;
}

#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];
        if ( $r eq "lockstta" && $nextStep eq $r && $STTA_LOCK eq 1 && $StepCheck eq "" ) { 
             my @lineStep = split(/:/,$rulesHash{$r},-1);
             $nextStep=$ruleline[$i+1];
             $rulesHash{$r}=$lineStep[0].":".$lineStep[1]++;
        };
        $i++;
     }
     $rulesHash{'ntask'}=$i;
     if ( $nextStep eq "" ) { 
          $nextStep=$ruleline[0]; 
          if ( $ruleline[0] eq "lockstta" && $STTA_LOCK eq 1 ) { $nextStep=$ruleline[1] };
     }
     if ( $Step eq "" ) { 
         $Step=$ruleline[0]; 
         if ( $ruleline[0] eq "lockstta" && $lockDo eq "lockstta" ) {
              my @lineStep = split(/:/,$rulesHash{'lockstta'},-1);
              $rulesHash{'lockstta'}=$lineStep[0].":".$lineStep[1]++;
              $Step=$ruleline[1];
         };
     }
}

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

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

sub make_steps_tables
{
     my  @statStep =();
     my @ruleline = split(/,/,$rules{$task},-1);
     my $newLine=0;
     my $jsString="";
     my $langf="";
     local $help="";

     print "</td></tr></table><tr $cb> <td><table $cb width=100%>\n";
     print "<tr><td valign=top><table width=100% border align=center>\n";
     print "<tr $tb> <td valign=top><font color='$revtext'>$text{menu}&nbsp;&nbsp;&nbsp;<b>$text{$task}</b></font></td></tr>\n";
     print "<tr $cb> <td valign=top><table width=100%>\n";
     print "<tr $cb><td><a class='menu1' href=\"javascript:alert('$text{seehelpInfo}')\"><img SRC='/stta/images/info.gif' width=20 heigth=20 align=top border=0></a>\n";
     print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>$text{work_secuence}:</b></td></tr>\n";
     print "<tr><td valign=top><hr></td></tr>";

     print "<tr><td valign=top>\n";
     $i=0;
     foreach $r (@ruleline) {
        $newLine=0;
        @statStep = split(/:/,$rulesHash{$r},-1);
        print "<tr $cb><td valign=top>";
        if ( $statStep[0] eq 1 ) { $jsString.="    if ( step == \"$r\" ) { alreadyrun= 1;}\n ";}
        if ( $nextStep eq $r ) { 
             print "<img SRC='images/next.gif'width=18 heigth=18 align=top border=0>"; 
        } else {
             print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        if ( $statStep[0] eq 1  && $statStep[1] gt 0  ) {
             print "<img SRC='images/ok.gif' align=top width=18 heigth=18 border=0>"; 
             if ( $Step eq $r ) { print "<font color='#9830d2'><b>";} 
        } else {
             print "<input type=\"radio\" name=\"step\" value=$r ";
             if ( $Step eq $r ) {
                 print "checked ><font color='#9830d2'><b>"; 
             } else { print ">"; }
        }
        if ( $statStep[0] eq 1  ) { print "<b>";}
        if ( $statStep[0] eq 1  && $statStep[1] gt 0 ) { print "<b>";  $newLine=1 }
        if ( $statStep[1] gt 0 ) { $newLine=1 }
        print "&nbsp;&nbsp;&nbsp;".$text{$r}."</b></font>\n";
        if ( $newLine eq 1 ) { 
              print "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
              print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
        }
        if ( $nextStep eq $r ) { print "($text{nextstep})"; }
        if ( $newLine eq 1 ) { print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"; }
        if ( $statStep[0] eq 1  && $statStep[1] gt 0 ) { print "($text{'done'})"; }
        if ( $statStep[1] gt 0 ) { print "($text{'visited'} $statStep[1])"; }
        print "</td></tr>\n"; 
        $i++;
     }
     print "<tr><td valign=top><hr></td></tr>";
     print "<tr><td valign=top>$text{opc_msg}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
     print "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
     if ( $STTA_LOCK eq 1 ) {
         print "<input TYPE=\"submit\" NAME=\"",$text{'execute'},"\" VALUE=\"",$text{'execute'},"\">";
     } else {
         print "<input TYPE=\"submit\" NAME=\"",$text{'browse'},"\" VALUE=\"",$text{'browse'},"\">";
     }
     print "</td></tr>\n";
     print "</td></tr></table></table></td>\n";
     &close_form_hidden();
     print "<td><table width=100% border>\n";
     print "<tr $tb> <td valign=top><font color='$revtext'>$text{info}&nbsp;&nbsp;&nbsp;<b>$text{$Step}</b></font></td></tr>\n";
     print "<tr $cb > <td valign=top><table width=100%>\n";
     print "<tr $bgimage ><td valign=top>\n";

     # First try info files for particular $task/tmp/info .. if got it.. last
     # Second try info files order for global $task/info.. if got it  ... last
     $infoTmpDirInstall= "$DirInstall/tmp/info";
     $infoDirInstall= "$DirInstall/info";
     $langf="$infoTmpDirInstall/$Step.html";
     if ( ! -r $langf ) { 
          $langf="$infoDirInstall/$Step.html";
          if ( ! -r $langf ) { $langf=""; }
     }
#    foreach $l (@lang_order_list) {
#           $langf="$infoTmpDirInstall/$l/$Step.html";
#           if ( -r $langf ) { last; };
#           $langf="$infoDirInstall/$l/$Step.html";
#           if ( -r $langf ) { last; };
#           $langf="";
#     }
     if ( $langf )  { 
            unless ( open(INDEXPAGE,$langf) ) { &write_help_info_file($langf,$Step) }
            my @ary = <INDEXPAGE>;
            close INDEXPAGE;
            foreach $line (@ary) { $help.=$line; }
     }
     if ( $help )  { &make_help_html(); }
     if ( $help )  {
                   $help =~s/<BODY>//g; $help =~s/<body>//g;
                   $help =~s/<\/BODY>//g;    $help =~s/<\/body>//g;
                   $help =~s/<HTML>//g; $help =~s/<html>//g;
                   $help =~s/<\/HTML>//g;    $help =~s/<\/html>//g;
                   # split html to find href and changes to window.open
                   # This is hard work ... probably there is an other easier way to do it
                   @thisline=split(/href=/, $help, -1);
                   my $outline="$thisline[0]";
                   for ( $i = 1 ; $i <= $#thisline ; $i++ ) { 
                       @wordline=split(/"/, $thisline[$i], -1);
                       $outline.= " onclick=\" window.open('$wordline[1]','help',".
                                   "'toolbar=no,menubar=no,scrollbars=yes,".
                                   "width=$config{help_width},height=$config{help_heigth},resizable=yes'); return false\" ".
                                   " class='menu1' href=\"$wordline[1]\" ";
                       $restLine=$thisline[$i];
                       $restLine=~s/"$wordline[1]"//;
                       $outline.=$restLine;
                  }
                  print "$outline";
     } 
     print "</td></tr>\n";
     print "</td></tr></table></table></td></tr>\n";
     print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
     print "/******************************************************************\n";
     print " checkStep(obj) {\n";
     print "*******************************************************************/\n";
     print "function checkStep(obj) {\n";
     print "   var alreadyrun=0;  \n";
     print "   var valret = true;  \n";
     print "   step=get_value(\"radio\",obj.step);\n";
     if ( $STTA_LOCK eq 1 ) {
          print "   $jsString \n";
          print "   if ( alreadyrun == 1  ) {\n ";
          print "        valret=confirm(msg10)\n";
          print "   }\n";
     }
#     print "   if ( step ==  'unlockstta' && valret == true ) {\n ";
#     print "           obj.action=obj.action+'?lck='+step ; \n";
#     print "           obj.td.value=''; \n";
#     print "           obj.next.value=''; \n";
#     print "   }\n";
     print "   return valret;\n";
     print "}\n";
     print "</SCRIPT>\n";

     print "</table><tr><td valign=top><table width=100% border align=center>\n";
     print "<tr $tb> <td valign=top><font color='$revtext'>$text{help}:&nbsp;&nbsp;&nbsp;<b>$text{'index_installupd'}</b></font></td> </tr>\n";
     print "<tr $cb> <td valign=top><table>\n";
     print "<tr $cb><td><b>$text{menu}</b></td>"; 
     print "<td>".&text('menu_panel',$text{$task})."</td></tr>\n";
     print "<tr $cb><td><b>$text{info}</b></td>"; 
     print "<td>".&text('info_panel',$text{$Step})."</td></tr>\n";
     print "<tr $cb><td><b>$text{run}</b></td>"; 
     print "<td>".&text('run_panel',$text{$Step})."</td></tr>\n";
     print "<tr $cb><td><b>$text{help}</b></td>"; 
     print "<td>".&text('help_panel',$text{'index_installupd'})."</td></tr>\n";
     print "<tr $cb><td><b>$text{black_line}</td>"; 
     print "<td>$text{step_runonce}</b></td></tr>\n";
     print "<tr $cb><td><b><font color='#9830d2'>$text{this_colorline}"; 
     print "<td>$text{step_current}</td></font></b></td></tr>\n";
     print "<tr $cb><td>($text{'visited'} N)</td>"; 
     print "<td>$text{'nro_visited'}</td></tr> "; 
     print "<tr $cb><td><img SRC='images/next.gif'width=18 heigth=18 align=top border=0></td>"; 
     print "<td>$text{step_next} -- ($text{nextstep})</td></tr>\n";
     print "<tr $cb><td><img SRC='images/ok.gif' align=top width=18 heigth=18 border=0></td>"; 
     print "<td>$text{steps_done} -- ($text{done})</td></tr>\n";
     print "<tr $cb><td>"; 
     print "<b>$text{note}</b></td><td><i>$text{help_msg1}</i></td></tr>\n";
     print "</td></tr></table></table></td></tr>\n";
     print "<a name=\"help\">\n";
     print "</pre></td></tr>\n";
     print "</td></tr></table></table></td></tr>\n";
}

sub hrefwin
{
    return "<a class='menu1' href='javascript:window.open(\"".$_[0]."\", \"help\", \"toolbar=no,menubar=no,scrollbars=yes,width=$config{help_width},height=$config{help_heigth},resizable=yes\"); return false' >";
}

sub close_form_hidden
{
     print "  <input TYPE=\"hidden\" NAME=\"task\" VALUE=\"",$task,"\">\n";
     print "  <input TYPE=\"hidden\" NAME=\"next\" VALUE=\"",$nextStep,"\">\n";
     print "  <input TYPE=\"hidden\" NAME=\"td\" VALUE=\"",$tdone,"\">\n";
     print "  <input TYPE=\"hidden\" NAME=\"sttasrv\" VALUE=\"",$sttasrv,"\">\n";
}

sub write_help_info_file
{
  open (FILE, ">$_[0]");
  print FILE "<include head>\n";
  print FILE "<header>$_[1]</header>\n";
  print FILE "<include foot>\n";
  close (FILE);
}

sub out_sel_task 
{
     print "</td></tr></table><tr $cb> <td><table border align=center>\n";
     print "<tr $tb> <td><font color='$revtext'><b>$text{f_taskinstupd}  </b>\n";
     print "<form METHOD=POST name=\"f_seltask\" ACTION=\"stta_installupd.cgi\" onSubmit=\"";
     if ( $STTA_LOCK eq 0 ) { print "alert('$text{warn_lockstta}');"; }
     print "\"></font></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>\n";
     print "<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 out_sel_sttasrv 
{
     print "</td></tr></table><tr $cb> <td><table border align=center>\n";
     print "<tr $tb> <td><font color='$revtext'><b>$text{f_sttasrvs}  </b>\n";
     print "<form METHOD=POST name=\"f_seltask\" ACTION=\"stta_installupd.cgi\" onSubmit=\"";
     if ( $STTA_LOCK eq 0 ) { print "alert('$text{warn_lockstta}');"; }
     print "\"></font></td><td></td></tr>\n";
     print "<tr><td valign=top><table>\n";
     print "   <tr $cb><td valign=top>",&hlink("<b>$text{'f_sttasrvs'}</b>","fsttasrvs"),"&nbsp;</td>\n";
     local %servers = &list_servers();
     print "<td valign=top ><select NAME=\"sttasrv\">\n";
         print "    <option VALUE=\"none\" >-----------  ";
     foreach $h (keys (%servers)) {
         next if ( $item =~ /_/ );
         print "    <option VALUE=\"".$servers{$h}{'host'}."\" >".$servers{$h}{'host'}."  ";
     } 
     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 "  <input TYPE=\"hidden\" NAME=\"task\" VALUE=\"".$task."\">\n";
     print "</form>\n";
     print "</td></tr></table></table></td></tr>\n";
     print "<tr $cb><td valign=top><b>$text{'msg_sttasrvs'}</b>&nbsp;</td>\n";
     print "\n";
}

sub get_file_updpatch
{
if ($file) { return };
    print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
    print "/******************************************************************\n";
    print "   Check_fields(obj)\n";
    print "*******************************************************************/\n";
    print "function Check_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";
    print "<table border align=center>\n";
    print "<tr $tb> <td><font color='$revtext'><b><big></big>".&text('tit_fileupdpatch',$text{$task})."  </b></font></td></tr>\n";
    print "<tr $cb> <td><table align=center>\n";
    print "<tr></tr>\n";
    print "<form METHOD=POST name=\"f_updpatch\" ACTION=\"stta_installupd.cgi\" onSubmit=\"return Check_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,0,"/")," (/)</td></tr>\n";
    print "        <tr><td valign=top><input TYPE=\"submit\" NAME=\"",$text{'f_sendreq'},"\" VALUE=\"",$text{'f_sendreq'},"\" onclick=\"alert('$text{warn_lockstta}');\">\n";
    print "        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n";
    &close_form_hidden();
    print "      </form>\n";
 print "<tr></tr></table></table>\n";
 print "<tr $tb> <td><font color='$revtext'><b>$text{'warn_updpatch'}</b></font></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 "" ) { 
        print "($Step) $text{msg_processing} ".eval("\$text{$rulesHash{proc}}")."($rulesHash{proc})<br>\n"; 
        if ( !defined(eval("&$rulesHash{proc}"))) {
             print &text('error_unknown_step', $rulesHash{'proc'}, $text{$Step});
        }
   }
}

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 width=100%>\n";
    print "<tr $tb> <td><font color='$revtext'><b><big></big>".&text('tit_runfilename',$fileName)."  </b></font></td></tr>\n";
    print "<tr $cb> <td><table>\n";
    print "<tr $cb><td 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 ( ! $config{unziptool} || $config{unziptool} eq "" ) { $config{unziptool}="gunzip"; }
          if ( $fileName =~  /.tar.gz$/ || $fileName =~  /.wbm.gz$/ ) {
                 $str.="cat $fileName | $config{unziptool} | tar xvf -" }
          elsif ( $fileName =~  /.cpio.gz$/ ) { $str.="cat $fileName | $config{unziptool} | cpio -idumv " }
          elsif ( $fileName =~  /.gz$/ ) {
                 @line=split(/\//, $fileName, -1);
                 $nomf=$line[$#line];
                 $nomf=~s/.gz$//g;
                 $str.="cat $fileName | $config{unziptool} > $nomf";
          }
          elsif ( $fileName =~  /.cpio$/ ) { $str.="cpio -idumvI $fileName" }
          elsif ( $fileName =~  /.tar$/ || $fileName =~  /.wbm$/ ) {
                  $str.="tar xvf $fileName" }
          elsif ( $fileName =~  /.shx$/ ) { $str.="sh $fileName" }
          else  { $str = "cp $fileName . ";}
          if ( ! &run_cmd($str))  { &html_errshow("cmd")};
          #print "$str\n";
          open(FILEINSTALL, ">>$logInsUpd");
             print FILEINSTALL "$task: ".&text('tit_runfilename',$fileName)." -".&set_now($now)."\n";
          close (FILEINSTALL);
       }
    }
    print "</pre></td></tr></table></table>\n";
}


sub lock_insupd_stta
{
   my $task_steps = $task."_steps";
   open(FILEINSTALL, ">>$config_directory/$module_name/.insupd.lock");
        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);
   $STTA_LOCK=1;
}

sub task_log
{
     open(TASKLOG,">>$config_directory/$module_name/.insupd.lock");
     print TASKLOG $now." ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]." ".$_[4]."\n";
     close (TASKLOG);
     &webmin_log($_[0], $_[1], $_[2], $_[3], $_[4]);
}
# Unlock STTA and move all to $task logfile 
sub unlock_insupd_stta
{
   my $Line=""; 
   unless ( open(FILELOG,"$config_directory/$module_name/.insupd.lock") ) { return -1}
   &lock_insupd_stta($_[0]);
   @ary = <FILELOG>;
   close (FILELOG);
   my $ahoraf=&out_now_filename($now);
   my $logfile="$config_directory/$module_name/$task";
   if ( ! -d $logfile ) { system (" mkdir $logfile "); }
   $logfile.="/".$ahoraf."_log";
   open(FILEINSTALL, ">>$logfile");
   foreach $line (@ary) { print FILEINSTALL "$line"; }
   close (FILEINSTALL);
   open(FILEINSTALL, ">>$logInsUpd");
   open(FILEINSTALL, ">>$logfile");
      print FILEINSTALL "$task: done $ahoraf  $rulesHash{'brand'}"; 
   close (FILEINSTALL);
   system (" rm -f $config_directory/$module_name/.insupd.lock");
   my $wkdir="$DirInstall/tmp";
   if ( $wkdir ne "/tmp") { system("rm -rf $wkdir 2>/dev/null"); }
   $STTA_LOCK=0;
}

sub run_fix_brand_data
{
#  my $task_brand = $task."_brand";
   
#  local %minfo=&load_minfo();
#  $minfo{$task_brand}=$rulesHash{'brand'};
  &dump_brandinfo("","",1);
}

sub stepcheck_button
{
    $StepCheck=$_[0];
    print "  <input TYPE=\"hidden\" NAME=\"stepcheck\" VALUE=\"".$_[0]."\">\n";
    print "     <tr><td valign=top><input TYPE=\"submit\" NAME=\"$text{make}\" VALUE=\"$text{make}\" onClick=\"\" >\n";
    print "     &nbsp;&nbsp;&nbsp;$text{make_msg1}</td></tr>\n";
}

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

    if ( $Step eq "lockstta" )  { &lock_insupd_stta("lock"); }
    if ( $Step eq "unlockstta" )  { &unlock_insupd_stta; }
    if ( $Step =~ /lockstta/ || $Step =~ /readme/ )  { return }
    print "<table border align=center width=100%>\n";
    print "<tr $tb> <td><font color='$revtext'>$text{run}&nbsp;&nbsp;&nbsp;<b>$text{$Step}</b></font></td></tr>\n";
    print "<tr $cb> <td><table>\n";
    print "<tr $cb><td 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 "dataconf" )  {
              print "$text{done}\n";
              $JScodeInit="showWin('config','')";
         } elsif ( $Step eq "checklist" )  {
              $foo="";  # Do nothing so all test will be at the end of page
              print "$text{done}\n";
         } elsif ( $Step eq "fixbrand" )  {
                   &run_fix_brand_data();
         } elsif ( $Step eq "doupdate" )  {
                   &run_instupd_procedure();
         } elsif ( $Step eq "doupgrade" ) {
                   &run_instupd_procedure();
         } elsif ( $Step eq "dopatch" ) {
                   &run_instupd_procedure();
         } elsif ( $Step eq "doloadfiles" ) {
                   &run_instupd_procedure();
         } else  {    # Wanderland new function can be added and loaded on the fly 
                 if ( !defined(eval("\&step_$Step"))) {
                     print &text('error_unknown_step', $Step, $text{$Step});
                 }
         }
    }
    print "</pre></td></tr></table></table>\n";
}
1;  # Return true
