#!/usr/bin/perl
# stta_webhelp.cgi
# Make a Web help contents
# 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 "help-lib.pl"
 &foreign_require("proc", "proc-lib.pl");
 &ReadParse();

 &headerstta($text{index_webhelp}, "", "webhelp",undef,1,0,"$text{'headpage_right'}"."$text{'head_logout'}",undef,undef,"$text{head_below}" );

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

 $outdir=$in{outdir}|| "";
 $OutDir=$outdir;
 $langSel=$in{langsel}|| "";
 $URLBase=$in{urlbase}|| "";
 $urlbase = $URLBase;
 if ( $URLBase =~ /NO/ ) { 
      $URLBase="NONE";
 } else {
    if ( $langSel ne "" ) { 
        if ( $OutDir =~ /\/$langSel$/ ) {
             $outdir=~ s/\/$langSel$//g;
        } else {
             $OutDir="$outdir/$langSel";
        }
        if ( $URLBase =~ /\/$langSel$/ ) {
          $urlbase =~ s/\/$langSel$//g;
        } else {
             $URLBase="$URLBase/$langSel"; 
        }
    }
 }
 if ( $IMGBase =~ /NO/ ) { $IMGBase="NONE"; }
 $IMGBase=$in{imgbasedir}|| "";
 $packCmd=$in{packcmd}|| "";
 $cp_images=$in{cpimages} || "";
 if ( $OutDir ne ""  ) {
    $helpPath="$root_directory/$module_name/help";
    if ( ! -d $OutDir ) { system("mkdir -p $OutDir")  }
    if ( ! -d  $OutDir) {
               print &text('dir_erraccess',$OutDir)."\n";
               &footHere();
               exit;
    } 
    # set default form "webhelp" dataconf file ...
    if ( $config{wbgimage} ) { $config{bgimage} = $config{wbgimage} } ;
    if ( $config{wcs_page} ) { $config{cs_page} = $config{wcs_page} } ;
    if ( $config{wcs_table} ) { $config{cs_table} = $config{wcs_table} } ;
    if ( $config{wcs_header} ) { $config{cs_header} = $config{wcs_header} } ;
    if ( $config{wcs_link} ) { $config{cs_link} = $config{wcs_link} } ;
    if ( $config{wcs_text} ) { $config{cs_text} = $config{wcs_text} } ;
    if ( $config{wcs_revtext} ) { $config{cs_revtext} = $config{wcs_revtext} } ;

    # reset to form input values ...
    if ( $in{bgimage} ) { $config{bgimage} = $in{bgimage} } ;
    if ( $in{cs_page} ) { $config{cs_page} = $in{cs_page} } ;
    if ( $in{cs_table} ) { $config{cs_table} = $in{cs_table} } ;
    if ( $in{cs_header} ) { $config{cs_header} = $in{cs_header} } ;
    if ( $in{cs_link} ) { $config{cs_link} = $in{cs_link} } ;
    if ( $in{cs_text} ) { $config{cs_text} = $in{cs_text} } ;
    if ( $in{cs_revtext} ) { $config{cs_revtext} = $in{cs_revtext} } ;

    &set_interface_colors();   #  Set as normal environment values !! (works as reset from here)
    $module=$module_name;
    $i=0;
    opendir(DIR, "$helpPath");
          foreach $f (readdir(DIR)) { 
                # read the help file
                next if ($f eq "." || $f eq ".." );
                next if ( -d "$helpPath/$f" );
                if ( $f =~ /\.$langSel\./ ) {
                     $namef=$f;
                     $namef=~s/\.$langSel//;
                } elsif ( $f =~ /\.html$/ ) {
                     $namef=$f;
                } else { next };
                $html="";
                @st = stat("$helpPath/$f");
                open(HELP, "$helpPath/$f") || &helperror(&text('help_efile', $f));
                read(HELP, $help, $st[7]);
                close(HELP);
                $WebHelpOut="$OutDir/$namef";
                open(WEBFILEOUT, ">$OutDir/$namef");
                if ( $WebHelpOut ) { select(WEBFILEOUT)}
                if ( $f  =~  /\.html$/ ) { &make_help_html(); }
                $help =~ s/href=\"\/stta\/gpl\//href=\"$URLBase\/gpl\//g;
                $help =~ s/\/stta\/help\/images/$IMGBase\/images/g;
                if ( $URLBase =~ /NONE/ ) {  
                     $help =~ s/$URLBase\///g;
                     $help =~ s/$URLBase//g;
                     $help =~ s/href="\//href="/g;
                     $help =~ s/HREF="\//HREF="/g;
                     $help =~ s/http:\/\/www.eagora.info\/stta\/help\/$langSel\///g;
                }
                if ( $IMGBase =~ /NONE/ ) {  
                     $help =~ s/$IMGBase\///g;
                     $help =~ s/$IMGBase//g;
                     $help =~ s/src="\//src="/g;
                     $help =~ s/SRC="\//SRC="/g;
                }
                print $help;
                $outCSS=&out_css();
                $outCSS =~ s/href='\/stta/href='../g;
                print $outCSS;
                if ( $WebHelpOut ) { $| =1; select(STDOUT);}
                close(WEBFILEOUT);
                print "<tr><td> $text{user_made} ... <b>$namef</b></td><td> $item_text</td></tr>\n";
#                $strCmd="cat $OutDir/$namef | sed 's,href=\"\/stta\/help.cgi\/stta,href=\"\/stta,g' | ".
#                  " sed 's,href=\"\/stta,href=\"$URLBase,g' | ".
#                  " sed 's,href=\"javascript:self.close(),href=\"$URLBase,g' | ".
#                  " sed 's,src=\"\/stta\/images,src=\"$IMGBase\/images,g' >$OutDir/$namef-new ";
#                system ($strCmd );
#                system ("mv $OutDir/$namef-new $OutDir/$namef" );
                $i++;
          }
    closedir(DIR);
    system ("mkdir $OutDir/config" );
    opendir(DIR, "$helpPath/$langSel/config");
          foreach $f (readdir(DIR)) { 
                # read the help file
                next if ($f eq "." || $f eq ".." );
                next if ( -d "$helpPath/$langSel/config/$f" );
                if ( $f =~ /\.$langSel\./ ) {
                     $namef=$f;
                     $namef=~s/\.$langSel//;
                } elsif ( $f =~ /\.html$/ ) {
                     $namef=$f;
                } else { next };
                $html="";
                @st = stat("$helpPath/$langSel/config/$f");
                open(HELP,"$helpPath/$langSel/config/$f") || &helperror(&text('help_efile', $f));
                read(HELP,$help, $st[7]);
                close(HELP);
                $sec=$f;
                $sec=~ s/\.html$//g;
                $sectionCfg="";
                if ($help =~ /<sectionCfg\s+([^>]*)>/) { $sectionCfg=$1 }
                $help =~ s/<sectionCfg\s+(\S+)>//g;
                $secLink="<a href=\"/stta/help.cgi/stta/config_".$sectionCfg."\"> <exec \$text{".$sectionCfg."_title}></a>";
                $head_path= "$root_directory/$module/help/cfg_head.html";
                @st = stat($head_path);
                open(HELP, $head_path) || &helperror(&text('help_efile', $head_path));
                read(HELP, $head_help, $st[7]);
                close(HELP);
                $foot_path= "$root_directory/$module/help/cfg_foot.html";
                @st = stat($foot_path);
                open(HELP, $foot_path) || &helperror(&text('help_efile', $foot_path));
                read(HELP, $foot_help, $st[7]);
                close(HELP);
                $help=$head_help.$help.$foot_help;
                $help =~ s/%CFGSEC_LINK%/$secLink/g;
                $help=~ s/%ITEM_CFG%/$sec/g;
                $WebHelpOut="$OutDir/config/$namef";
                open(WEBFILEOUT, ">$OutDir/config/$namef");
                if ( $WebHelpOut ) { select(WEBFILEOUT)}
                if ( $f  =~  /\.html$/ ) { &make_help_html(); }
                $help =~ s/href=\"\/stta\/gpl\//href=\"$URLBase\/gpl\//g;
                $help =~ s/\/stta\/help\/images/$IMGBase\/images/g;
                if ( $URLBase =~ /NONE/ ) {  
                     $help =~ s/$URLBase\///g;
                     $help =~ s/$URLBase//g;
                     $help =~ s/href="\//href="/g;
                     $help =~ s/HREF="\//HREF="/g;
                     $help =~ s/http:\/\/www.eagora.info\/stta\/help\/$langSel\///g;
                }
                if ( $IMGBase =~ /NONE/ ) {  
                     $help =~ s/$IMGBase\///g;
                     $help =~ s/$IMGBase//g;
                     $help =~ s/src="\//src="/g;
                     $help =~ s/SRC="\//SRC="/g;
                }
                print $help;
                print &out_css();
                if ( $WebHelpOut ) { $| =1; select(STDOUT);}
                close(WEBFILEOUT);
                print "<tr><td> $text{user_made} ... <b>config/$namef</b></td><td> $item_text</td></tr>\n";
                $i++;
          }
    closedir(DIR);
    print "<tr><td><hr><b> ".&text('l_total',$i)."</b></td><td><hr><b>$OutDir</b></td></tr>\n";
          # changes with "sed" as footer can have links 
          # copy images ...
   if ( $cp_images eq "true" ) {
          print "<tr><td valign=top>$text{'img_dircopy'}&nbsp;</td></tr>\n";
          system ( "mkdir $outdir/images ");
#          print "<tr><td> $urlbase - $URLBase - $outdir - $OutDir - $langSel </td></tr>";
#          print "<tr><td>cd $root_directory/$module_name ; find images  -print -depth | cpio -pdumv $OutDir </td></tr>";
          system ( "find images  -print -depth | cpio -pdumv $outdir ");
   }
          system ( "cp $langSel/*.pdf $OutDir ");
          system ( "mkdir $OutDir/gpl ");
#          system ( "cd gpl | find gpl -print -depth | cpio -pdumv $OutDir ");
          system ( "cp gpl/license*html $OutDir/gpl ");
          system ( "cp gpl/lic*txt $OutDir/gpl ");
          if ( $packCmd ne ""  ) { 
               $str=$packCmd;
               &run_cmd();
          }
          # saving values for next time
          %webhtml=();
          $webhtml{outdir}=$OutDir;
          $webhtml{urlbase}=$URLBase;
          $webhtml{imgbasedir}=$IMGBase;
          $webhtml{packcmd}=$packCmd;
          $webhtml{wbgimage}=$config{bgimage};
          $webhtml{wcs_page}=$config{cs_page};
          $webhtml{wcs_table}=$config{cs_table};
          $webhtml{wcs_header}=$config{cs_header};
          $webhtml{wcs_text}=$config{cs_text};
          $webhtml{wcs_revtext}=$config{cs_revtext};
          $webhtml{wcs_link}=$config{cs_link};
          &write_file("$config_directory/$module_name/dataconf/webhtml", \%webhtml);
          &webmin_log("Making web Help html ($i Files)","stta_webhtml","$OutDir ($i Files) ",\%webhtml);  
 } else  {
    &read_file("$config_directory/$module_name/dataconf/webhtml", \%config);
 print "<SCRIPT SRC=\"global.js\">\n"; 
 print "</SCRIPT>\n"; 
 print "<SCRIPT LANGUAGE=\"JavaScript\">\n"; 
 print "<!--\n"; 
 print "/*  Strings for locales  */\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 "/******************************************************************\n"; 
 print "   Check_fields(obj)\n"; 
 print "*******************************************************************/\n"; 
 print "function Check_fields(obj) {\n"; 
 print "     if ( emptyField(obj.outdir) == true ) {\n"; 
 print "          alert(obj.outdir.name+\":\"+msg1+\"->\"+obj.outdir.value+\"<-\");\n"; 
 print "		    obj.outdir.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.urlbase) == true ) {\n"; 
 print "          alert(obj.urlbase.name+\":\"+msg1+\"->\"+obj.urlbase.value+\"<-\");\n"; 
 print "		    obj.urlbase.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "     if ( emptyField(obj.imgbasedir) == true ) {\n"; 
 print "          alert(obj.imgbasedir.name+\":\"+msg1+\"->\"+obj.imgbasedir.value+\"<-\");\n"; 
 print "		    obj.imgbasedir.focus();\n"; 
 print "          return false;\n"; 
 print "     }         \n"; 
 print "\n"; 
 print "     return true;\n"; 
 print "}\n"; 
 print "//-->\n"; 
 print "</SCRIPT>\n"; 
 print "\n"; 
  print "<form METHOD=POST name=newgrp ACTION=\"stta_webhelp.cgi\" onSubmit=\"return Check_fields(this)\">\n"; 
 print "<tr><td valign=top>$text{'sel_lang'}&nbsp;</td>\n";
 print "   <td valign=top><select NAME=\"langsel\">\n";
 $nItems=0;
 foreach $l (@lang_order_list) {
        print "  <option VALUE=\"$l\" ";
        if ( $l eq $current_lang ) {
           print "selected";
        }
        print ">",$l,"\n";
        $nItems++;
 }
 print "        </select></td></tr>\n";
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_outdir'}</b>","foutdir"),"&nbsp;</td>\n";
print "		<td valign=top><input TYPE=\"text\" NAME=\"outdir\" VALUE=\"$config{outdir}\" cols=\"60\" size=\"60\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_urlbase'}</b>","furlbase"),"&nbsp;</td>\n";
print "		<td valign=top><input TYPE=\"text\" NAME=\"urlbase\" VALUE=\"$config{urlbase}\" cols=\"60\" size=\"60\" ALIGN=left></td></tr>\n"; 
 print "	<tr><td valign=top>",&hlink("<b>$text{'f_imgbasedir'}</b>","fimgbasedir"),"&nbsp;</td>\n";
print "         <td valign=top> <input TYPE=\"text\" NAME=\"imgbasedir\" VALUE=\"$config{imgbasedir}\" cols=\"60\" size=\"60\" ALIGN=left ></td></tr>\n"; 
print "<tr><td valign=top>$text{'img_dircopy'}&nbsp;</td>\n";
print "<td><input type=checkbox name=\"cpimages\" value=\"true\" ></td></tr>\n";

 print "        <tr><td valign=top>",&hlink("<b>$text{'f_packcmd'}</b>","fpackcmd"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"packcmd\" VALUE=\"$config{packcmd}\" cols=\"60\" size=\"60\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_bgimage'}</b>","fbgimage"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"bgimage\" VALUE=\"$config{wbgimage}\" cols=\"60\" size=\"60\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_page'}</b>","fcs_page"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_page\" VALUE=\"$config{wcs_page}\" cols=\"8\" size=\"8\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_table'}</b>","fcs_table"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_table\" VALUE=\"$config{wcs_table}\" cols=\"8\" size=\"8\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_header'}</b>","fcs_header"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_header\" VALUE=\"$config{wcs_header}\" cols=\"8\" size=\"8\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_revtext'}</b>","fcs_revtext"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_revtext\" VALUE=\"$config{wcs_revtext}\" cols=\"8\" size=\"8\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_link'}</b>","fcs_link"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_link\" VALUE=\"$config{wcs_link}\" cols=\"8\" size=\"8\" ALIGN=left >\n";
 print "        <tr><td valign=top>",&hlink("<b>$text{'f_cs_text'}</b>","fcs_text"),"&nbsp;</td>\n";
 print "        <td valign=top><input TYPE=\"text\" NAME=\"cs_text\" VALUE=\"$config{wcs_text}\" cols=\"8\" size=\"8\" ALIGN=left >\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;</td>\n";
 print "        <td valign=top><input TYPE=\"reset\" NAME=\"",$text{'f_cancelreq'},"\" VALUE=\"",$text{'f_cancelreq'},"\"></td></tr>\n";
 print "	</form>\n"; 
 }
 print "</table></table><p>\n";
 &footer($config{'back_return'},$text{'index'});
 do "footer.pl";
 exit;
