#!/usr/bin/perl

# update_javascript.pl
# This script modifies template-head files in 
# /inetpub/www/reference///http directories,
# with  and  used dynamically.

# The script inserts replaces JavaScript in the header
# with improved JS ( no error occurs if a page has no form).

# Lines with "# DEBUG" report progress and can be deleted without
# affecting functionality.

$basepath = "/inetpub/www/reference";   # Set base path to reference dir
print "1.0 base path: $basepath\n";   # DEBUG

# For each platform dir in the base dir
foreach $platform ( @ref_dir = glob( "$basepath/*" ))
{
   print "2.1 platform: $platform\n";   # DEBUG
   # if entry is a dir, process it except for those known not hold template-head files
   if ( -d "$platform"                        && 
        substr( $platform, -4, 4 ) ne ".kde"  &&
        substr( $platform, -7, 7 ) ne "Desktop" )
   {
      print "2.2 dir & if ne tests passed: $platform\n";   # DEBUG
      # For each project dir in the current platform dir
      foreach $project ( @plat_dir = glob( "$platform/*" ))
      {
         print "3.1 project: $project\n";   # DEBUG
         # if entry is a dir, process it
         if ( -d "$project" )
         {
            print "3.2 dir test passed: $project\n";   # DEBUG
            # For each entry in the current project dir
            foreach $cur_path ( @proj_dir = glob( "$project/*" ))
            {
               print "4.1 entry: $cur_path\n";   # DEBUG
               if ( -d "$cur_path" && substr( $cur_path, -4, 4 ) eq "http" )
               {
                  print "4.2 dir & if eq tests passed: $cur_path\n";   # DEBUG
                  UpdateTemplateHead();   # Update the template-head file
               }
            }
         }
      }
   }
}


sub UpdateTemplateHead
{
   rename( "$cur_path/template-head", "$cur_path/template-head-old" ) 
      || die "Cannot rename $cur_path/template-head: $!\n";
   print "5.1 renamed template-head as template-head-old in $cur_path\n";   # DEBUG
   open( TPL_IN, "$cur_path/template-head-old" )
      || die "Cannot open $cur_path/template-head-old: $!\n";
   print "5.2 opened for reading template-head-old in $cur_path\n";   # DEBUG
   open( TPL_OUT, ">$cur_path/template-head" )
      || die "Cannot open $cur_path/template-head: $!\n";
   print "5.3 opened for writing new template-head in $cur_path\n";   # DEBUG

   $skip = 0;   # flags to skip writing input when true
   while (  )
   {
      if ( substr( $_, 0, 7 ) eq " )
         {
            print TPL_OUT $_;
         }
         close( JS_IN )
            || die "cannot close /inetpub/www/temp/jsinsert2: $!\n";
      }
      print TPL_OUT $_ unless $skip;
      if ( substr( $_, 0, 9 ) eq "" )
      {
         $skip = 0;
      }
   }
   close( TPL_IN )
      || die "Cannot close $cur_path/template-head-old: $!\n";
   print "5.5 closed template-head-old in $cur_path\n";   # DEBUG
   close( TPL_OUT )
      || die "Cannot close $cur_path/template-head: $!\n";
   print "5.6 closed template-head in $cur_path\n";   # DEBUG
   unlink( "$cur_path/template-head-old" )
      || warn "Cannot delete $cur_path/template-head-old: $!\n";
   print "5.7 deleted template-head-old in $cur_path\n";   # DEBUG
}