#! /usr/bin/perl -i

use strict;
use warnings;
use Geo::METAR;
use constant METARDATA => "/var/lib/wview/img/metar.txt";

my $metar;
open METAR, "<", METARDATA or die "cannot open " . METARDATA . "\n";
{
	local $/;
	$metar = <METAR>;
}
close METAR;

my $m = new Geo::METAR;
$m->metar($metar);

sub get_metar {
	my $var = shift;
	my $val = $m->$var;
	return "" unless $val;
	$val = join("<br>", @$val) if ref($val) eq "ARRAY";
	$val =~ s{(\d+)ft}{sprintf "%d m", int(($1 * 0.3048 + 50) / 100) * 100}eg;
	if ($var eq "VISIBILITY") {
		$val =~ s{meters}{m};
		$val =~ s{9999 m}{&ge; 10 km};
	}
	return $val;
}

while (<>) {
	s{<!--METAR:(\w+)-->}{get_metar($1)}eg;
} continue {
	print;
}

# vi:ts=4:
