#!/usr/bin/perl
#
        my $Version = "1.0.1";


# You may need to set your $password below
#
# Use as is, user assumes all risk, no warranty is expressed or implied. This
# has been rigorously tested but hey, this is Open Source, use at your own risk.
#
# Copyright 2012 Curtis J Blank a.k.a. curtronics dot com
# Subject to GPL License
#

use strict;
use DBI;
use CGI qw(:standard);

my $user = "wwwrun";
my $DBhost = "localhost";
my $DBname = "Weather";
my $vpID = 0;   # Vantage Pro Console ID 0 (zero is default)

my $true = 1;
my $false = 0;

my $Interactive;

my $QUERY_STRING = $ENV{'QUERY_STRING'};
if ($QUERY_STRING ne "") {
    # run as CGI, so look at env variables
    my @PARAMS = split /\+/, $QUERY_STRING;
    $Interactive = $false;
} else {
    shift;
    $Interactive = $true;
}

if (! $Interactive) { print "Content-type: text/html\n\n<pre>\n\n"; }

sub max {
    my $max = shift(@_);
    my $tmpmax;
    foreach $tmpmax (@_) { $max = $tmpmax if $max < $tmpmax; }
    return $max;
}

sub min {
    my $min = shift(@_);
    my $tmpmin;
    foreach $tmpmin (@_) { $min = $tmpmin if $tmpmin < $min; }
    return $min;
}

#| StormStartedDate | date         | NO   | PRI | NULL    |       |
#| StormStartedTime | time         | YES  | MUL | NULL    |       |
#| StormEndedDate   | date         | YES  | MUL | NULL    |       |
#| StormEndedTime   | time         | YES  | MUL | NULL    |       |
#| StormAmount      | decimal(6,2) | YES  |     | NULL    |       |
#| HighestRate      | decimal(6,2) | YES  |     | NULL    |       |
#| HighestRateDT    | datetime     | YES  |     | NULL    |       |


# use this if you have your ~/my.cnf file set up with a [vproweather] client group
my $dbh = DBI->connect("DBI:mysql:$DBname;mysql_read_default_file=~$user/.my.cnf;mysql_read_default_group=getDBWind24Hours","$user");
# use this if you don't
#my $dbh=DBI->connect("DBI:mysql:host=$DBhost;database=$DBname","$user","$password");
if (!$dbh) { die("ERROR! Connecting to $DBhost $DBname.\n"); }

my $Query = "SELECT StormStartedDate, StormStartedTime, StormEndedDate, StormEndedTime, StormAmount, HighestRate, HighestRateDT FROM Storms WHERE vpID = \"$vpID\" && StormStartedDate != \"\" && StormStartedTime != \"\" && StormEndedDate != \"\" && StormEndedTime != \"\" ORDER BY StormStartedDate DESC limit 10";

my $sth = $dbh->prepare($Query);
#printf stderr "\nQuerying DB... ";
$sth = $dbh->prepare($Query);
$sth->execute();
#printf stderr "Done.\n";

my $rows = $sth->rows;

if ($rows > 0) {
    my $cnt = 1;
    my $ref;
    while($ref = $sth->fetchrow_hashref()) {
        my $StormStartDate = $ref->{StormStartedDate};
        my $StormStartTime = $ref->{StormStartedTime};
        my $StormEndDate = $ref->{StormEndedDate};
        my $StormEndTime = $ref->{StormEndedTime};
        my $StormAmount = $ref->{StormAmount};
        my $StormHighRate = $ref->{HighestRate};
        my $StormHighRateDT = $ref->{HighestRateDT};
        if ($StormStartTime =~ /\d\d:\d\d:\d\d/) { $StormStartTime =~ s/:(\d\d)$//; }
        if ($StormEndTime =~ /\d\d:\d\d:\d\d/) { $StormEndTime =~ s/:(\d\d)$//; }
        if ($StormHighRateDT =~ /\d\d:\d\d:\d\d/) { $StormHighRateDT =~ s/:(\d\d)$//; }
        print "StormStartDT$cnt = $StormStartDate $StormStartTime\n";
        print "StormEndDT$cnt = $StormEndDate $StormEndTime\n";
        print "StormAmount$cnt = $StormAmount\n";
        print "StormHighRate$cnt = $StormHighRate\n";
        print "StormHighRateDT$cnt = $StormHighRateDT\n";
        $cnt++;
    }
}

#printf stderr "\nAll Done.\n\n";

if ($sth) { $sth->finish(); }
if ($dbh) { $dbh->disconnect(); }

#
