chiark / gitweb /
Parallelise
[post-pizza-notify.git] / notify
1 #!/bin/bash
2 set -e
3 usage () { cat <<END
4
5 usage: ./notify senderfile recipientsfile 'message'
6
7 recipientsfile: see test-recipients for an example
8 see comments in via-* scripts for individual line formats
9
10 senderinfo: file containing
11    email YOUREMAILADDRESS@EXAMPLE.COM
12    name YOUR NAME
13
14 END
15 }
16
17 case "$#.$1" in
18 3.[^-]*)        ;;
19 *)              usage >&2; exit 1 ;;
20 esac
21
22 senderinfo="$1"
23 rcpts="$2"
24 msg="$3"
25
26 exec <"$senderinfo"
27 while read settingname value; do
28         case "$settingname" in
29         #*|'')  continue;;
30         *)      vn=PIZZANOTIFY_$settingname
31                 eval "$vn=\"\$value\""
32                 export $vn
33                 ;;
34         esac
35 done
36
37 perl <"$rcpts" -wne '
38     use strict;
39     our @children;
40     our @passon;
41     BEGIN { @passon = @ARGV; @ARGV = (); }
42     s/^\s+//;
43     s/\s+$//;
44     next if m/^\#/;
45     next unless m/\S/;
46     die unless m/^([a-z]\w+)\s/;
47     my $method = $1;
48     my $child = fork;
49     defined $child or die $!;
50     if (!$child) {
51         exec "./onemethod", $method, @passon;
52         die $!;
53     }
54     push @children, $child;
55     END {
56         foreach my $child (@children) {
57             $!=$?=0;
58             my $got = waitpid $child, 0;
59             die $! unless $got==$child;
60             warn "$method [$child] $?" if $?;
61         }
62     }
63 ' "$rcpts" "$msg"
64
65 echo