| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Getopt::Long; | ||
| 6 | use JSON::XS qw/decode_json/; | ||
| 7 | use Pod::Usage; | ||
| 8 | use Schedule::Easing; | ||
| 9 | use Schedule::Easing::Stream; | ||
| 10 | |||
| 11 | # todo: replay mode, historical checking, pulling the timestamp from the line itself? | ||
| 12 | |||
| 13 | sub load { | ||
| 14 | my ($fn)=@_; | ||
| 15 | my $t; | ||
| 16 | if(!-e $fn) { die "No such file: $fn" } | ||
| 17 | open(my $fh,'<',$fn) or die "Unable to read configuration from $fn: $@"; | ||
| 18 | {local($/); $t=<$fh>}; | ||
| 19 | close($fh); | ||
| 20 | return $t; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub loadeval { | ||
| 24 | my ($fn)=@_; | ||
| 25 | my $t=load($fn); | ||
| 26 | $t=~s/^\s*[^@$][A-Za-z_]\w+\s*=\s*//s; | ||
| 27 | my @res; | ||
| 28 | if($t=~/^[(]/) { eval "\@res=$t;"; if($@) { die "$@" } } # ) | ||
| 29 | elsif($t=~/^[\[]/) { eval "\@res=\@{ $t };"; if($@) { die "$@" } } # ) | ||
| 30 | else { die "$fn does not contain a valid configuration" } | ||
| 31 | return @res; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub loadjson { | ||
| 35 | my ($fn)=@_; | ||
| 36 | my $t=load($fn); | ||
| 37 | my @res=@{ decode_json($t) }; | ||
| 38 | foreach my $E (@res) { $$E{match}//=''; $$E{match}=qr/$$E{match}/ } | ||
| 39 | return @res; | ||
| 40 | } | ||
| 41 | |||
| 42 | my %opt=( | ||
| 43 | schedule =>undef, | ||
| 44 | json =>undef, | ||
| 45 | timestamps=>0, | ||
| 46 | expiration=>0, | ||
| 47 | check =>0, | ||
| 48 | stream =>'lines=>1', | ||
| 49 | help =>0, | ||
| 50 | time =>undef, | ||
| 51 | ); | ||
| 52 | |||
| 53 | GetOptions( | ||
| 54 | 'schedule=s'=>\$opt{schedule}, | ||
| 55 | 'json=s' =>\$opt{json}, | ||
| 56 | 'timestamps'=>\$opt{timestamps}, | ||
| 57 | 'expiration'=>\$opt{expiration}, | ||
| 58 | 'check' =>\$opt{check}, | ||
| 59 | 'stream=s' =>\$opt{stream}, | ||
| 60 | 'help' =>\$opt{help}, | ||
| 61 | 'time=i' =>\$opt{time}, # undocumented | ||
| 62 | ); | ||
| 63 | if($opt{help}) { pod2usage(-verbose=>2,-exitval=>2) } | ||
| 64 | |||
| 65 | my @schedule= | ||
| 66 | $opt{schedule} ? loadeval($opt{schedule}) : | ||
| 67 | $opt{json} ? loadjson($opt{json}) : | ||
| 68 | die 'Configuration is required'; | ||
| 69 | |||
| 70 | my $easing=Schedule::Easing->new( | ||
| 71 | schedule=>\@schedule, | ||
| 72 | warnExpired=>$opt{expiration}||$opt{check}, | ||
| 73 | ); | ||
| 74 | if(!$easing) { exit(1) } | ||
| 75 | if($opt{check}) { exit(0+!!$$easing{_err}) } | ||
| 76 | |||
| 77 | my ($inputcb,$updatecb,%streamopt); | ||
| 78 | eval "\%streamopt=($opt{stream});"; | ||
| 79 | if($@) { die "Stream options error: $@" } | ||
| 80 | |||
| 81 | if($opt{timestamps}) { | ||
| 82 | $inputcb=sub { | ||
| 83 | foreach my $sched ($easing->schedule(events=>[@_])) { | ||
| 84 | $$sched[0]//=9999999999; print join(' ',@$sched); } }; | ||
| 85 | $updatecb=undef; | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | my $currentts; | ||
| 89 | $inputcb=sub { print $easing->matches(ts=>$currentts,events=>[@_]) }; | ||
| 90 | if($opt{time}) { $currentts=$opt{time}; $updatecb=undef } | ||
| 91 | else { $updatecb=sub { $currentts=time() } } | ||
| 92 | } | ||
| 93 | |||
| 94 | my $stream=Schedule::Easing::Stream->new(fh=>\*STDIN,input=>$inputcb,update=>$updatecb,%streamopt); | ||
| 95 | $stream->read(); | ||
| 96 | |||
| 97 | |||
| 98 | __END__ |