#!/usr/bin/perl

# Config
my $statusfile = '/var/www/html/weather/log/weathermachine.status';


my %info = ();

&init;

if (@ARGV == 0) {
	push @ARGV, "--help";
}

foreach $arg (@ARGV) {

	$arg = lc($arg);
	$arg =~ s/^[\/\\\-]*//;

	my $mod = 0;
	if ($arg =~ /^\w+([-+])(\d+)$/) {
		$mod += $2 if ($1 eq '+');
		$mod -= $2 if ($1 eq '-');
		$arg =~ s/[-+]\d+$//;
	}
	my $val = 0;
	my $found = 1;

	if ($arg =~ /^\d+$/) {
		$val = $arg;
	} elsif ($arg eq "itf") {
		$val = sprintf("%.2f",($info{indoortemp}*1.8)+32);
	} elsif ($arg eq "itc") {
		$val = $info{indoortemp};
	} elsif ($arg eq "otf") {
		$val = sprintf("%.2f",($info{outdoortemp}*1.8)+32);
	} elsif ($arg eq "otc") {
		$val = $info{outdoortemp};
	} elsif ($arg eq "idf") {
		$val = sprintf("%.2f",($info{indoordewpoint}*1.8)+32);
	} elsif ($arg eq "idc") {
		$val = $info{indoordewpoint};
	} elsif ($arg eq "odf") {
		$val = sprintf("%.2f",($info{outdoordewpoint}*1.8)+32);
	} elsif ($arg eq "odc") {
		$val = $info{outdoordewpoint};
	} elsif ($arg eq "irh") {
		$val = $info{indoorhumidity};
	} elsif ($arg eq "orh") {
		$val = $info{outdoorhumidity};
	} elsif ($arg eq "bmb") {
		$val = $info{baro};
#	} elsif ($arg eq "bin") {
#		$val = $info{baro};
	} elsif ($arg eq "smb") {
		$val = $info{barosea};
#	} elsif ($arg eq "sin") {
#		$val = $info{barosea};
	} elsif ($arg eq "wgd") {
		$val = $info{windgustdir};
	} elsif ($arg eq "wgs") {
		$val = $info{windgustspeed};
	} elsif ($arg eq "was") {
		$val = $info{windavgspeed};
	} elsif ($arg eq "wgm") {
		$val = sprintf("%.2f",$info{windgustspeed}*3.6*1.60933);
	} elsif ($arg eq "wam") {
		$val = sprintf("%.2f",$info{windavgspeed}*3.6*1.60933);
	} elsif ($arg eq "wgk") {
		$val = sprintf("%.2f",$info{windgustspeed}*3.6);
	} elsif ($arg eq "wak") {
		$val = sprintf("%.2f",$info{windavgspeed}*3.6);
	} elsif ($arg eq "wch") {
		$val = $info{windchill};
	 } elsif ($arg eq "wcf") {
                $val = sprintf("%.2f",($info{windchill}*1.8)+32);
	} elsif ($arg eq "rrm") {
		$val = $info{rainrate};
	} elsif ($arg eq "rri") {
		$val = sprintf("%.2f",$info{rainrate}/25.4)+0;
	} elsif ($arg eq "rtm") {
		$val = $info{raintotal};
	} elsif ($arg eq "rti") {
		$val = sprintf("%.2f",$info{raintotal}/25.4)+0;
	} elsif ($arg eq "rym") {
		$val = $info{rainyesterday};
	} elsif ($arg eq "ryi") {
		$val = sprintf("%.2f",$info{rainyesterday}/25.4)+0;
	} else {
		print "\nI don't understand '$arg'! ";
		print "Please use one of these:\n\n";
		print "\t\t itf = Indoor  Temperature Farenheit\n";
		print "\t\t itc = Indoor  Temperature Centigrade\n";
		print "\t\t otf = Outdoor Temperature Farenheit\n";
		print "\t\t otc = Outdoor Temperature Centigrade\n\n";
		print "\t\t idf = Indoor  DewPoint Farenheit\n";
		print "\t\t idc = Indoor  DewPoint Centigrade\n";
		print "\t\t odf = Outdoor DewPoint Farenheit\n";
		print "\t\t odc = Outdoor DewPoint Centigrade\n\n";
		print "\t\t irh = Indoor  Relative Humidity\n";
		print "\t\t orh = Outdoor Relative Humidity\n\n";
		print "\t\t bmb = Barometric pressure, Millibars\n";
		print "\t\t bin = Barometric pressure, Inches\n";
		print "\t\t smb = Sealevel Pressure,   Millibars\n";
		print "\t\t sin = Sealevel Pressure,   Inches\n\n";
		print "\t\t wgd = Wind Gust Direction\n";
		print "\t\t wgs = Wind Gust Speed    (m/s)\n";
		print "\t\t was = Wind Average Speed (m/s)\n";
		print "\t\t wgm = Wind Gust Speed    (mph)\n";
		print "\t\t wam = Wind Average Speed (mph)\n";
		print "\t\t wgk = Wind Gust Speed    (kph)\n";
		print "\t\t wak = Wind Average Speed (kph)\n";
		print "\t\t wch = Wind Chill\n\n";
		print "\t\t rrm = Rain Rate      (mm/hr)\n";
		print "\t\t rri = Rain Rate      (inches/hr)\n";
		print "\t\t rtm = Rain Total     (mm)\n";
		print "\t\t rti = Rain Total     (inches)\n";
		print "\t\t rym = Rain Yesterday (mm)\n";
		print "\t\t ryi = Rain Yesterday (inches)\n";
		$found = 0;
	}
	if ($found) {
		$val += $mod;
		print "$val\n";
	}

}



sub init {
        %info = (windgustdir => 0, windgustspeed => 0, windavgspeed => 0,
                windchill => 0, rainrate => 0, raintotal => 0, rainyesterday => 0,
                indoortemp => 0, indoorhumidity => 0, indoordewpoint => 0,
                outdoortemp => 0, outdoorhumidity => 0, outdoordewpoint => 0,
                baro => 0, barosea => 0 );
        open INFILE, "<$statusfile" or die "Could not read $statusfile!\n";
        while (<INFILE>) {
           chomp;
           if ($_) {
               @_ = split / /;
               my $timestamp = shift;
               $info{windgustdir} = shift;
               $info{windgustspeed} = shift;
               $info{windavgspeed} = shift;
               $info{windchill} = shift;
               $info{rainrate} = shift;
               $info{raintotal} = shift;
               $info{rainyesterday} = shift;
               $info{indoortemp} = shift;
               $info{indoorhumidity} = shift;
               $info{indoordewpoint} = shift;
               $info{outdoortemp} = shift;
               $info{outdoorhumidity} = shift;
               $info{outdoordewpoint} = shift;
               $info{baro} = shift;
               $info{barosea} = shift;
           }
        }
        close INFILE;
}

#print round(0.7).' '.round(0.2).' '.round(-0.2).' '.round(-0.7)."\n";

sub round {
	my $num = shift;
	if (abs($num - int($num)) >= 0.5) {
		$num = int($num)+1 if ($num > 0);
		$num = int($num)-1 if ($num < 0);
	} else {
		$num = int($num);
	}
	return $num;
}

