#!/usr/bin/perl
# STTA Rev. 1.3 Copyright (c) 2001-2004 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# help.cgi
# Displays help HTML for some module, with substitutions

require '../web-lib.pl';
do 'web-lib.pl';
do 'html-lib.pl';
do 'global-lib.pl';
do 'help-lib.pl';
do 'lists-lib.pl';

&init_config();
&load_dataconf();
&error_setup($text{'help_err'});
@lin=split(/\?/,$ENV{REQUEST_URI},-1);
$querySTR=$lin[1];
$ENV{'PATH_INFO'} !~ /[\\\&\;\`\'\"\|\*\?\~\<\>\^\(\)\[\]\{\}\$\n\r]/ ||
	&error($text{'help_epath'});
$ENV{'PATH_INFO'} =~ /^\/(\S+)\/(\S+)$/ || &error($text{'help_epath'});
$module = $1; $file = $2; $sec = "";

# if it ends with .gif assume it is a direct URL
if ($file =~ /\.gif$/) {
	&redirect("$module/$file");
	exit;
}

if ( $module eq "stta/config" ) {   # this is made to manage STTA config Items 
    $ENV{'PATH_INFO'} =~ /^\/(\S+)\/(\S+)\/(\S+)$/ || &error($text{'help_epath'});
    $module = $1; $file = $2; $sec = $3 ;
    $path = "$root_directory/$module/help/$current_lang/$file/$sec.html";
} elsif ( $module =~ /\/info$/ ) {   # this is made to manage STTA install info Items 
    $ENV{'PATH_INFO'} =~ /^\/(\S+)\/(\S+)\/(\S+)\/(\S+)$/ || &error($text{'help_epath'});
    $module = $1; $file = $4;
    $path = "$root_directory/$module/install/info/$file";
} else {
    # read the help file
    if ($file =~ /\.html$/) { # it is a full path 
       $path = "$root_directory/$module/$file";
    } else {
       $path = &help_file($module, $file);
    }
    # this is to develop help code
    open(HELP, $path) || &write_helpfile($module,$file);
}
# this is for deploy 
if ( ! -r $path) { 
      # Help window is open so ...  close !!
      &header($file);
      print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
      print "self.close();\n";
      print "</SCRIPT>\n";
}
@st = stat($path);
open(HELP, $path) || &helperror(&text('help_efile', $path));
read(HELP, $help, $st[7]);
close(HELP);

if ( $sec ne "" ) { # this is made to manage STTA config Items
                    #  loading head and foot, make section link
   $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;
}
# code has been change for STTA in len help-lib.pl
&make_help_html();

# output the HTML
print $help;
print &out_css();
&footer();

exit;

sub standard_help_code
{
    # find and replace the <header> section
    if ($help =~ s/<header>([^<]+)<\/header>/<center><h1>$1<\/h1><\/center><hr>/) {
            $item=$1;
            &header($1);
            if ( $1 =~ /^f\*/ ) {
                 $item=~ s/^f/f_/g;
                 $item_text=$text{$item}; 
                 if  ( $item_text eq "" ) { $item = $1 }
            }
            if ( $item eq $1 ) {
                 $item_text=$text{$item} || $1;
            }  
    #	&header($item_text);
    } else {
	    &helperror($text{'help_eheader'});
	    }

    # find and replace <include> directives
    $help =~ s/<lihelpinc\s+(\S+)\s+(\S+)>/lihelpinc($1,$2)/ge;
    $help =~ s/<include\s+(\S+)>/inchelp($noHeaderTable,$1)/ge;
    
    # find and replace <if><else> directives
    $help =~ s/<if\s+([^>]*)>([\000-\177]*?)<else>([\000-\177]*?)<\/if>/ifhelp($1, $2, $3)/ge;

    # find and replace <if> directives
    $help =~ s/<if\s+([^>]*)>([\000-\177]*?)<\/if>/ifhelp($1, $2)/ge;
    
    # find and replace <exec> directives
    $help =~ s/<exec\s+([^>]*)>/exechelp($1)/ge;
}

