#!/usr/bin/env perl

# Print the historic weather for a place, e.g. "weather 2022-06-03 Buxton, NC, USA"

use strict;
use warnings;
use autodie;
use Cwd qw(realpath);
use File::Basename;
use File::Path qw(make_path);
use File::Spec;
use Readonly;
use Weather::Meteo;

# Only this script needs these, so the Weather::Meteo library does not depend on
# them (the distribution lists them as "recommends").  Geo::Coder::Free in
# particular pulls in a large dependency tree, including C libraries.  Check for
# them here so that a missing one gives an instruction instead of a stack trace.
BEGIN {
	my @missing = grep {
		(my $file = "$_.pm") =~ s{::}{/}g;
		!eval { require $file; 1 };
	} qw(
		File::HomeDir
		Geo::Coder::Free
		Geo::Coder::Free::Local
		Geo::Coder::Free::OpenAddresses
		LWP::UserAgent::Cached
	);

	if(@missing) {
		die "$0 needs these modules, which Weather::Meteo itself does not: @missing\n",
			"Install them with: cpanm Geo::Coder::Free LWP::UserAgent::Cached File::HomeDir\n";
	}
}

# What to print, in order: [ label, field in the API's 'daily' section ]
Readonly my @REPORT => (
	[ 'Maximum temperature'     => 'temperature_2m_max' ],
	[ 'Maximum wind speed'      => 'windspeed_10m_max' ],
	[ 'Maximum wind gust speed' => 'windgusts_10m_max' ],
);

# Cache directory is created private to the user: cached responses may reveal
# where somebody has been looking up the weather.
Readonly my $CACHE_MODE    => oct('700');
Readonly my $HTTP_TIMEOUT  => 30;

binmode(STDOUT, ':encoding(UTF-8)');

if(scalar(@ARGV) <= 1) {
	die "Usage: $0 YYYY-MM-DD location\n";
}

my $date     = shift @ARGV;
my $location = join(' ', @ARGV);

my $coordinates = Geo::Coder::Free::Local->new()->geocode($location) //
	Geo::Coder::Free->new()->geocode($location);

if(!defined($coordinates) && $ENV{'OPENADDR_HOME'}) {
	$coordinates = Geo::Coder::Free::OpenAddresses->new(
		openaddr => safe_path('OPENADDR_HOME'),
	)->geocode(location => $location);
}

if(!defined($coordinates)) {
	die "$0: unknown location '$location'\n";
}

# $CACHE_DIR if set, else ~/.cache/lwp-cache.  make_path() tolerates a
# directory that already exists, including one created by a concurrent run,
# so there is no check-then-create race.
my $cache_dir = File::Spec->catdir(
	($ENV{'CACHE_DIR'} ? safe_path('CACHE_DIR') : File::Spec->catdir(File::HomeDir->my_home(), '.cache')),
	'lwp-cache',
);
make_path($cache_dir, { mode => $CACHE_MODE });

my $cached_browser = LWP::UserAgent::Cached->new(
	cache_dir  => $cache_dir,
	agent      => basename($0),
	keep_alive => 1,
	timeout    => $HTTP_TIMEOUT,
);
$cached_browser->env_proxy(1);
$cached_browser->conn_cache->total_capacity(undef);

my $weather = Weather::Meteo->new(ua => $cached_browser)->weather($coordinates, $date);

if(!defined($weather)) {
	die "$0: no weather data available for '$location' on $date\n";
}

foreach my $line (@REPORT) {
	my ($label, $field) = @{$line};
	print "$label: ",
		$weather->{'daily'}->{$field}[0],
		($weather->{'daily_units'}->{$field} // ''), "\n";
}

# ---------------------------------------------------------------------------
# safe_path -- read a directory name from the environment and make it safe
#
# Purpose:     Environment variables are attacker-controlled input.  Accept
#              only plain path characters (which also untaints the value) and
#              canonicalise with realpath() so ../ tricks cannot escape.
# Entry:       $name -- name of the environment variable
# Exit:        canonical absolute path
# Side effects: dies with a message if the value has odd characters or does
#              not resolve
# ---------------------------------------------------------------------------
sub safe_path {
	my $name = $_[0];

	my ($safe) = ($ENV{$name} =~ m{\A([\w/.\-]+)\z}) or die "$name contains invalid characters\n";
	my $real   = realpath($safe) or die "$name '$safe' cannot be resolved: $!\n";

	return $real;
}
