#!/usr/bin/perl -w
#------------------------------------------------------------------
# MAKE_HTML.PL
#
# VERSION 2.1
#
# THIS IS USED TO CONSTRUCT ALL OF THE *.HTML AND *.OUT FILES FOR 
# THE MIDWEST RADAR PAGE (EXCEPT THE RADAR.HTML AND LEFTFRAME.HTML
# PAGES).  THIS CREATES THE FILES IN A FOLDER CALLED "NEW_HTML" AND 
# THE USER IS REQUIRED TO COPY THE NEW HTML TO CORRECT LOCATION, AFTER
# BACKING UP THE CURRENT HTML
# 
#
#------------------------------------------------------------------

use strict;
use Time::Local;
use HTML::Template;

# GLOBAL VARIABLES
###my $dir = '/var/apache/www/htdocs/localweather/radar/new_html';
my $dir = '/var/apache/www/htdocs/localweather/radar';
my $file;
my ($i, $j);

# FOR LATEST IMAGE (ORDER CONTROLS WHAT IS ON TOP - LAST IS ON TOP)
##my @overlaystr = ('_county_basemap_5.gif', '_city_5.gif', '_obs_5.gif', '_wwdisp_5.gif', '_radar_lowdbz_5.gif');
my @overlaystr = ('_radar_lowdbz_5.gif', '_county_basemap_5.gif', '_city_5.gif', '_obs_5.gif', '_wwdisp_5.gif');

# FOR LOOP
##my @overlaystr2 = ('_county_basemap_', '_city_', '_obs_', '_wwdisp_', '_radar_lowdbz_');
my @overlaystr2 = ('_radar_lowdbz_', '_county_basemap_', '_city_', '_obs_', '_wwdisp_');


# VARIALBLES TO LOOP OVER
my @regions = ('midwest','mkx','grb','arx','dlh');
my @animate = ('latest','loop');
my @products = ('radar','radial','storm');
my @alternate = ('','_cc');
my @overlays = ('','_no');                  # TEMPLATE VARIABLE - no stands for NoOverlay, not No

# TEMPLATES
use constant TMPL1 => "/var/apache/www/htdocs/localweather/radar/templates/region_product.html";
use constant TMPL2 => "/var/apache/www/htdocs/localweather/radar/templates/region_product.out";
#use constant TMPL3 => "/var/apache/www/htdocs/localweather/radar/templates/region_product_loop.out";
use constant TMPL4 => "/var/apache/www/htdocs/localweather/radar/templates/region_extrapbref1.html";

# TEMPLATE VARIABLES
my $img;
my %appletwidth = ('midwest' => 705,
                   'mkx'     => 635,
                   'grb'     => 635,
                   'arx'     => 635,
                   'dlh'     => 635);
my %appletheight = ('midwest' => 640,
                    'mkx'     => 600,
                    'grb'     => 600,
                    'arx'     => 600,
                    'dlh'     => 600);
my %controls = ('latest' => 'show,zoom,overlay,refresh',
                'loop'     => 'overlay,startstop,show,looprock,speed,step,zoom,refresh');
my $outfile;
my %overlaylabels = ('radar' => '&lt;20 dBZ, County Map, Cities, Weather, Watches/Warnings',
                     'radial' => 'County Map, Cities, Weather, Watches/Warnings',
                     'storm' => 'County Map, Cities, Weather, Watches/Warnings');
my $scaleimgname;
my $overlaylist;

#-------------------------------------------------------------------------------------
# CREATE HTML FOR ALL PRODUCTS EXCEPT EXTRAPBREF1
#-------------------------------------------------------------------------------------
# LOOP OVER VARIABLES AND CREATE HTML
my $tmpl = new HTML::Template( filename => TMPL1 );
my $tmpl2 = new HTML::Template( filename => TMPL2 );
#my $tmpl3 = new HTML::Template( filename => TMPL3 );
my ($reg,$prod,$ani,$alt,$ov);
my $r;
my @tmparr;
my @tmparr2;
foreach $reg (@regions) { 
 foreach $prod (@products) { 

  # skip unwanted permutations
  if ( $reg eq 'midwest' && ($prod eq 'radial' || $prod eq 'storm') ) { next; }

  foreach $alt (@alternate) { 

   # skip unwanted permutations
   if ( $prod eq 'storm' && $alt eq '_cc' ) { next; }

   foreach $ani (@animate) { 
    foreach $ov (@overlays) {

      # skip unwanted permutations
      if ( $ani eq 'loop' && $ov eq '_no' ) { next; }

      $file = $reg . '_' . $prod . '_' . $ani . $alt . $ov;
      $outfile = $file.'.out';

      open(FILE,">$dir/$file.html") or die "can not create $dir/$file.html \n";

      $scaleimgname = '';
      $r = $reg;
      if ($reg ne 'midwest') { $r = uc($reg); }
      
      if ($prod eq 'radial') { 
        $scaleimgname = 'scale_rv_vert'; 
##        $img = $r . '_rv' . $alt . '_5.gif';
        $img = $r . '_rv' . $alt;
      } elsif ($prod eq 'storm') {
        $scaleimgname = 'scale_pcp_vert'; 
##        $img = $r . '_pcptot' . $alt . '_5.gif';
        $img = $r . '_pcptot' . $alt;
      } else {  
        $scaleimgname = 'scale_radar_vert'; 
##        $img = $r . '_radar' . $alt . '_5.gif';
        $img = $r . '_radar' . $alt;
      }
      $scaleimgname = $scaleimgname . $alt . '.gif';

      #------------------------------------
      # EVALUATE TEMPLATE VARIABLES FOR TMPL1
      # logic is setup/controlled in template
      #------------------------------------
      # --- used in logical test for if statement in template ---
      $tmpl->param( overlays => $ov );

      # ----- if overlays is a non-null value, i.e. if overlays eq "_no" or No Overlays  ------
##      $tmpl->param( img => $img );
      $tmpl->param( img => $img . '_5.gif' );
  
      # ----- else if overlays is a null value, i.e. there are overlays -----
      $tmpl->param( appletwidth => $appletwidth{$reg} );
      $tmpl->param( appletheight => $appletheight{$reg} );
      $tmpl->param( controls => $controls{$ani} );
      $tmpl->param( outfile => $outfile );
      $tmpl->param( overlaylabels => $overlaylabels{$prod} );
      $tmpl->param( scaleimgname => $scaleimgname );
###      if ($ov eq '_no') { $tmpl->param( img => $img ); }  # IF statement might be redundant

      print FILE $tmpl->output;

      close(FILE);

      #--------------------------------------
      # EVALUATE TEMPLATE VARIABLES FOR TMPL2
      #--------------------------------------
      if (!($ov)) {
        if ($ani eq 'latest') {
          # create overlay string for *.out files
          @tmparr2 = @overlaystr; # for some reason, map is processing input array, so I do this 
          @tmparr = map { 
            $_ = $r . $_; 
          } @tmparr2;
          if ($prod eq 'radar') {
            $overlaylist = join(',',@tmparr);
          } else {
##            $overlaylist = join(',',@tmparr[0..3]);
            $overlaylist = join(',',@tmparr[1..4]);
          }
 
          open(OUTFILE,">$dir/$outfile") or die "can not create $outfile \n";
##          $tmpl2->param( img => $img );
          $tmpl2->param( img => $img . '_5.gif' );
          $tmpl2->param( overlaylist => $overlaylist );
          print OUTFILE $tmpl2->output;
          close(OUTFILE);
        } else {
          # START LOOP - CREATE FILES
          for ($i = 0; $i <= 0; $i++) {
            $j = $i + 1;

            open(OUTFILE,">$dir/$outfile") or die "can not create $outfile \n";
            # create overlay string for *.out files
            @tmparr2 = @overlaystr2; # for some reason, map is processing input array, so I do this 
            @tmparr = map { 
              $_ = $r . $_; 
            } @tmparr2;
            $tmparr[1] = $tmparr[1] . '1.gif';
            $tmparr[2] = $tmparr[2] . '1.gif';
            $tmparr[3] = $tmparr[3] . $j . '.gif';
            $tmparr[4] = $tmparr[4] . $j . '.gif';
            $tmparr[0] = $tmparr[0] . $j . '.gif';
            if ($prod eq 'radar') {
              $overlaylist = join(',',@tmparr);
            } else {
#              $overlaylist = join(',',@tmparr[0..3]);
              $overlaylist = join(',',@tmparr[1..4]);
            }

            $tmpl2->param( img => $img . '_' . $j . '.gif' );
            $tmpl2->param( overlaylist => $overlaylist );
            print OUTFILE $tmpl2->output;
            close(OUTFILE);
          } # for ($i = 0; $i <= 0; $i++)

          # CONTINUE LOOP - APPEND OUTPUT TO FILE
          for ($i = 1; $i <= 4; $i++) {
            $j = $i + 1;
            open(OUTFILE,">>$dir/$outfile") or die "can not create $outfile \n";
            # create overlay string for *.out files
            @tmparr2 = @overlaystr2; # for some reason, map is processing input array, so I do this 
            @tmparr = map { 
              $_ = $r . $_; 
            } @tmparr2;
            $tmparr[1] = $tmparr[1] . '1.gif';
            $tmparr[2] = $tmparr[2] . '1.gif';
            $tmparr[3] = $tmparr[3] . $j . '.gif';
            $tmparr[4] = $tmparr[4] . $j . '.gif';
            $tmparr[0] = $tmparr[0] . $j . '.gif';
            if ($prod eq 'radar') {
              $overlaylist = join(',',@tmparr);
            } else {
#              $overlaylist = join(',',@tmparr[0..3]);
              $overlaylist = join(',',@tmparr[1..4]);
            }

            $tmpl2->param( img => $img . '_' . $j . '.gif' );
            $tmpl2->param( overlaylist => $overlaylist );
            print OUTFILE $tmpl2->output;
            close(OUTFILE);
          } # for ($i = 0; $i <= 0; $i++)
        }
      }

    } #foreach overlays 
   } #foreach animate
  } #foreach alternate
 } # foreach products
} #foreach regions

#-------------------------------------------------------------------------------------
# CREATE HTML FOR EXTRAPBREF1 PRODUCT
#-------------------------------------------------------------------------------------
%appletwidth = ('midwest' => 785,
                   'mkx'     => 585,
                   'grb'     => 585,
                   'arx'     => 585,
                   'dlh'     => 585);
%appletheight = ('midwest' => 640,
                    'mkx'     => 600,
                    'grb'     => 600,
                    'arx'     => 600,
                    'dlh'     => 600);
my $extrapimg;
my $extrapevent;
my $tmpl4 = new HTML::Template( filename => TMPL4 );

foreach $reg (@regions) { 
 foreach $alt (@alternate) { 

   $file = $reg . '_extrapbref1' . $alt;
   $extrapimg = '';
   $extrapevent = '';

   if ($reg eq 'midwest') {
     $extrapimg = $reg . '_radar_extrap' . $alt . '_';
     $extrapevent = $reg . '_radar.event';
   } else {
     $extrapimg = uc($reg) . '_radar_extrap' . $alt . '_';
     $extrapevent = uc($reg) . '_radar.event';
   }

   open(FILE,">$dir/$file.html") or die "can not create $dir/$file.html \n";
   $tmpl4->param( extrapimg => $extrapimg );
   $tmpl4->param( extrapevent => $extrapevent );
   $tmpl4->param( appletwidth => $appletwidth{$reg} );
   $tmpl4->param( appletheight => $appletheight{$reg} );
   print FILE $tmpl4->output;
   close(FILE);

 } #foreach alternate
} #foreach regions
