chiark / gitweb /
numbered-alias-sheet: wip, run generator
[d.git] / numbered-alias-sheet
1 #!/usr/bin/perl -w
2 use strict;
3 our $us = $0; $us =~ s#.*/##;
4
5 our $papersize =  'creditcard';
6 our $count = 10;
7 our $fontname = 'Courier';
8
9 our $usage = <<END;
10 usage: $us [<options>] <foo-mail-pregen> <foo-mail-pregen-opts>...
11 options:
12   -p<papersize>     for libpaper, or "creditcard"  default is $papersize
13   -F<font>          font name                      default is $fontname
14 END
15
16 our @paperpts;
17
18 sub badusage () { die "bad usage\n\n$usage"; }
19
20 for (;;) {
21     badusage unless @ARGV;
22     last unless $ARGV[0] =~ m/^-/;
23     $_ = shift @ARGV;
24     last if m/^--?$/;
25     while (m/^-./) {
26         if (s/^-p(\w+)$//) {
27             $papersize = $1;
28         } elsif (s/^-n(\d{1,2})$//) {
29             $count = 1;
30         } elsif (s/^-F([^()\\]+)$//) {
31             $fontname = $1;
32         } else {
33             badusage;
34         }
35     }
36 }
37
38 if ($papersize eq 'creditcard') {
39     # ISO/IEC 7810 ID-1, from en.wikipedia.org/wiki/Payment_card
40     @paperpts = map { $_ * 72.0 / 25.4 } qw(85.60 53.98);
41 } else {
42     $!=0; $?=0; my $r = `paperconf -sp $1`;
43     defined $r or die "paperconf failed: $? $!\n";
44     $r =~ m/^([0-9.]+) ([0-9.]+)$/ or die "$_ ?";
45     @paperpts = ($1,$2);
46 }
47
48 @ARGV >= 2 or badusage;
49
50 our @strings;
51
52 our $nlen = 0;
53
54 open P, "-|", @ARGV or die $!;
55 while (<P>) {
56     chomp or die;
57     m/^(\d+) (\S+)$/ or die "$_ ?";
58     $nlen = length($1) if length($1) > $nlen;
59     push @strings, [ $1, $2 ];
60 }
61 $!=0; $?=0; close P or die "$us: generator failed: $! $?\n";
62
63 $_ = sprintf "%${nlen}s %s", @$_ foreach @strings;
64
65 print $_,"\n" or die $! foreach @strings;