scripts/distrobox/toolbox/gammastep.pl

54 lines
1.2 KiB
Perl
Raw Normal View History

2020-09-11 15:00:39 +00:00
#!/usr/bin/perl
# TODO: If gammastep is already running, allow location to update
use strict;
use warnings;
use LWP::UserAgent;
use JSON::XS;
my $location = "https://papillon.john.me.tz/data/location.json";
my $lat_lon = fetch_lat_lon($location);
2020-09-11 15:00:39 +00:00
my $pid = fork;
unless ($pid) {
2023-01-06 19:41:31 +00:00
open(my $fh, ">", $ENV{HOME}."/.spool/gammastep.pid");
print $fh $$;
close($fh);
open($fh, ">", $ENV{HOME}."/.spool/gammastep.status");
print $fh 1;
close($fh);
open STDIN, '/dev/null';
open STDOUT, '>>/dev/null';
open STDERR, '>>/dev/null';
if (! -e "/run/.containerenv" && ! -e "/.dockerenv" ) {
exec "exec /bin/distrobox-enter -n debian12 -- gammastep -l $lat_lon -b 0.7:0.3";
} else {
exec "exec gammastep -l $lat_lon -b 0.7:0.3";
}
2020-09-11 15:00:39 +00:00
}
2020-11-23 09:35:26 +00:00
sub fetch_lat_lon
{
2023-01-06 19:41:31 +00:00
my ($location) = shift;
2023-01-06 19:41:31 +00:00
my $ua = LWP::UserAgent->new();
my $json = JSON::XS->new();
2020-09-11 15:00:39 +00:00
2023-01-06 19:41:31 +00:00
my $raw = $ua->get($location)->content();
2023-01-06 19:41:31 +00:00
if (defined $raw) {
my $decoded = $json->decode($raw);
if (defined $decoded->{lat} && defined $decoded->{lon}) {
return "$decoded->{lat}:$decoded->{lon}";
2020-11-23 09:35:26 +00:00
}
2023-01-06 19:41:31 +00:00
}
sleep 5;
return fetch_lat_lon($ua, $json, $location);
2020-09-11 15:00:39 +00:00
}
exit;