#!/usr/bin/perl -wt
# $Id: metar.cgi,v 3.5 2015/02/02 13:42:22 psb Exp psb $
#
# Display METAR & TAF for an ICAO location
#
# $Log: metar.cgi,v $
# Revision 3.5  2015/02/02 13:42:22  psb
# Allowing lower case FORM_icao also.
#
# Revision 3.4  2014/10/30 22:58:19  psb
# Historic values held in a cookie. Good version.
#
# Revision 3.3  2014/10/02 23:03:16  psb
# *** empty log message ***
#
# Revision 3.2  2014/10/02 02:05:58  psb
# *** empty log message ***
#
# Revision 3.1  2014/10/02 01:45:47  psb
# This is 1st perl version.
#
use CGI::Pretty qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

my $existsornew;
my $idcookie;
my $idvalue;

$idvalue = cookie('metarsession');
if( ! $idvalue) {
   $idvalue = int(rand(100000000));
}
$idcookie = cookie(-name=>'metarsession', -value=>$idvalue, -expires=>'+365d');

my $FORM_ICAO = escapeHTML(uc(param("ICAO")||param("icao")||""));
my $histcookie;
my @histvalues;

@histvalues = cookie(-name=>'metarhist');
if( ! @histvalues) {
	@histvalues=();
}
	
@histvalues = grep /\S/, @histvalues;
@histvalues = ($FORM_ICAO,grep { $FORM_ICAO ne $_ } @histvalues);
my $nval = scalar @histvalues - 1;
if( $nval > 6) {$nval = 6; }
@histvalues = @histvalues[0..$nval];
$histcookie = cookie(-name=>'metarhist', -value=>\@histvalues, -expires=>'+28d');

print header( -cookie=>[$idcookie,$histcookie]);

my %meta;
$meta{'RCSID'}='$Id: metar.cgi,v 3.5 2015/02/02 13:42:22 psb Exp psb $';
$meta{'GENERATOR'}='Linux /bin/vi';
$meta{'AUTHOR'}='Paul Beardsell';
    my $style=<<STYLE;
    body {
      font-family: "Courier New", monospace;
      text-align: center;
    }
    p {
      font-family: "Courier New", monospace;
    }
    form {
      text-align: center;
      font-size: 2em;
    }
    h1 {
      font-size: 2em;
    }
    input {
      font-family: "Courier New", monospace;
      font-size: 2em;
      text-align: center;
    }
    pre {
      text-align: left;
      color: black;
      font-size: 1.875em;
    }
    a {
        text-decoration: none;
    }
STYLE
print start_html( -title=>'Aviation weather: METAR & TAF, decoded', -meta=>{%meta}, -style=>{-code=>$style});

my $script_name = script_name();
my @histlinks = map qq|<a href="$script_name?ICAO=$_">$_</a>|, @histvalues;

print <<F1;
    <form action="$script_name" method="get">
    <p>@histlinks</p>
    <h1>Get METAR &amp; TAF</h1>
    <p>Enter airport name or 4-character ICAO code</p>
    <input type="text" name="ICAO" value="$FORM_ICAO" size="7" onchange='this.form.submit()'/>
    <noscript><br/><input type="submit" value="submit"/></noscript>
    </form>
F1

if( $FORM_ICAO) {
    print "<pre>\n";
    my $sedscript='s;^\[\([A-Z][A-Z][A-Z][A-Z]\)\];<a href="' . $script_name . '?ICAO=\1\">\1</a>;';
    my $command="~psb/bin/metar '$FORM_ICAO' 2>&1 | sed -e '$sedscript' 2>&1";
    system( $command);
    print "</pre>\n";
}

print <<MAIN;
    <p>
    <br/>
    Use this form to find aviation <b>METAR</b> and <b>TAF</b>
    weather reports and forecasts for an ICAO location.
    </p>
    <p>
    This utility is intended to help pilots, perhaps even while
    flying, to obtain weather info via the Internet from cellphones and 
    other mobile devices.
    </p>
    <p>
    Paul Beardsell<br/>
    paul-AT-beardsell-DOT-com<br/>
    <a href="http://beardsell.com/">http://beardsell.com/</a>
    </p>
MAIN

#print p("metarsession='$idvalue'");

print end_html;
exit 0
