summaryrefslogtreecommitdiff
path: root/bin/db-fakedisplay
blob: 5350c0569264ce9a1fec7c5b3e4dd6605f9e501a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use File::ShareDir qw(dist_file);
use Getopt::Long qw(:config no_ignore_case);
use HTML::Template;
use List::Util qw(first);
use Travel::Status::DE::DeutscheBahn;

our $VERSION = '0.00';

my @params;
my ( $station, @platforms );
my $template_file;
my $template;

GetOptions(

	'h|help'       => sub { show_help(0) },
	't|template=s' => \&handle_template,
	'V|version' => sub { say "db-fakedisplay version ${VERSION}"; exit 0 },

) or show_help(1);

( $station, @platforms ) = @ARGV;
$template_file //= dist_file( 'db-fakedisplay', 'single-lcd.html' );
$template = HTML::Template->new( filename => $template_file );

if ( not defined $station ) {
	show_help(1);
}

my $status = Travel::Status::DE::DeutscheBahn->new( station => $station );

sub handle_template {
	my ( undef, $template_name ) = @_;

	if ( -e $template_name ) {
		$template_file = $template_name;
	}
	else {
		$template_file = dist_file( 'db-fakedisplay', $template_name );
	}

	return;
}

sub show_help {
	my ($exit_status) = @_;

	say 'Usage: db-fakedisplay [-t template] <station> [platforms ...]';
	say 'See also: man db-fakedisplay';

	exit $exit_status;
}

if ( not @platforms ) {
	for my $result ( $status->results ) {
		if ( $result->platform ~~ \@platforms ) {
			next;
		}
		push( @platforms, $result->platform );
	}
	@platforms = sort { $a <=> $b } @platforms;
}

for my $platform (@platforms) {
	my $result = first { $_->platform =~ m{ ^ $platform (?: \s | $ )}x }
	$status->results;

	if ( not defined $result ) {
		push( @params, { platform => $platform } );
		next;
	}

	push(
		@params,
		{
			time  => $result->time,
			train => $result->train,
			via   => [ map { { stop => $_ } } $result->route_interesting(3) ],
			destination => $result->destination,
			platform    => ( split( / /, $result->platform ) )[0],
			info        => $result->info,
		}
	);
}

$template->param( platform => \@params, );

say $template->output;

__END__

=head1 NAME

db-fakedisplay - Show train departures, as seen on the displays on most main stations

=head1 SYNOPSIS

B<db-fakedisplay> [-t I<template>] I<station> [I<platforms ...>]

=head1 VERSION

version 0.00

=head1 DESCRIPTION

B<db-fakedisplay> outputs HTML showing the next departure for every
I<platform> on I<station> on stdout.  The HTML is styled to look like the LCDs
installed on most (major) stations.

If no I<platforms> were specified, B<db-fakedisplay> shows all platforms for
which departures are reported.

=head1 OPTIONS

=over

=item B<-t>, B<--template> B<single-lcd.html>|I<filename>

Select template.  Specify either a I<filename> or one of the templates shipped
with B<db-fakedisplay> (right now only B<single-lcd.html> is available, which
is also the default).

=item B<-V>, B<--version>

Show version information.

=back

=head1 EXIT STATUS

Zero.

=head1 CONFIGURATION

None.

=head1 DEPENDENCIES

=over

=item * File::ShareDir(3pm)

=item * HTML::Template(3pm)

=item * Travel::Status::DE::DeutscheBahn(3pm)

=back

=head1 BUGS AND LIMITATIONS

Unknown

=head1 SEE ALSO

There is some example output available at
L<http://finalrewind.org/projects/db-fakedisplay/>.

=head1 AUTHOR

Copyright (C) 2011 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.