apt-zephyr: Send a “Done.” zephyr when dpkg finishes running.
[snippets/.git] / apt-zephyr / apt-zephyr
1 #!/bin/sh
2 set -e
3
4 # Defaults
5 CLASS=
6 INSTANCE=
7 REALM=
8 RECIPIENTS=
9 SIG="$(hostname -f)"
10 OPCODE=auto
11 ZAUTH=
12
13 # Read configuration
14 . /etc/apt-zephyr.conf
15
16 send_zephyr ()
17 {
18     zwrite \
19         ${CLASS:+-c "$CLASS"} \
20         ${INSTANCE:+-i "$INSTANCE"} \
21         ${REALM:+-r "$REALM"} \
22         ${SIG:+-s "$SIG"} \
23         ${OPCODE:+-O "$OPCODE"} \
24         ${ZAUTH:--d} \
25         ${RECIPIENTS:+$RECIPIENTS}
26 }
27
28 package ()
29 {
30     package=$1
31     oldver=$2
32     cmp=$3
33     newver=$4
34
35     if [ "$newver" = '-' ] && [ "$oldver" = '-' ]; then
36         echo "Purging $package"
37     elif [ "$newver" = '-' ]; then
38         echo "Removing $package $oldver"
39     elif [ "$oldver" = '-' ]; then
40         echo "Installing $package $newver"
41     elif [ "$cmp" = '<' ]; then
42         echo "Upgrading $package $oldver to $newver"
43     elif [ "$cmp" = '=' ]; then
44         echo "Reinstalling $package $newver"
45     elif [ "$cmp" = '>' ]; then
46         echo "Downgrading $package $oldver to $newver"
47     else
48         echo "I'm confused: $*"
49     fi
50 }
51
52 parse_v1 ()
53 {
54     oldpkgs=$(mktemp -t "apt-zephyr-old.XXXXXX") || exit $?
55     newpkgs=$(mktemp -t "apt-zephyr-new.XXXXXX") || exit $?
56     xargs -r -d '\n' dpkg-deb -W | sort -o "$newpkgs"
57     cut -f 1 "$newpkgs" | xargs -r -d '\n' dpkg-query -W | \
58         sort -o "$oldpkgs"
59     join -t '   ' -j 1 -e '-' "$oldpkgs" "$newpkgs" | \
60         while IFS='     ' read -r package oldver newver; do
61         if dpkg --compare-versions "$oldver" lt "$newver"; then
62             package "$package" "$oldver" '<' "$newver"
63         elif dpkg --compare-versions "$oldver" eq "$newver"; then
64             package "$package" "$oldver" '=' "$newver"
65         else
66             package "$package" "$oldver" '>' "$newver"
67         fi
68     done
69     rm -f "$oldpkgs" "$newpkgs"
70 }
71
72 parse_v2 ()
73 {
74     while read -r line && [ -n "$line" ]; do :; done
75     while read -r package oldver cmp newver action; do
76         case "$action" in
77             '**CONFIGURE**' | '**REMOVE**')
78                 package "$package" "$oldver" "$cmp" "$newver"
79                 ;;
80             '**ERROR**')
81                 echo "ERROR on $package $newver"
82                 ;;
83         esac
84     done
85 }
86
87 pre_install_pkgs ()
88 {
89     read -r line
90     case "$line" in
91         'VERSION 2')
92             parse_v2 | send_zephyr
93             ;;
94         'VERSION *')
95             echo "$0: unrecognized version: $line" | send-zephyr
96             ;;
97         '')
98             ;;
99         *)
100             (echo "$line"; cat) | parse_v1 | send_zephyr
101             ;;
102     esac
103 }
104
105 post_invoke ()
106 {
107     echo 'Done.' | send_zephyr
108 }
109
110 if [ "$1" = "--pre-install-pkgs" ]; then
111     pre_install_pkgs
112 elif [ "$1" = "--post-invoke" ]; then
113     post_invoke
114 else
115     echo "usage: $0 {--pre-install-pkgs | --post-invoke}" >&2
116     echo "(Hint: you probably need to update /etc/apt/apt.conf.d/90zephyr.)" >&2
117 fi
118
119 exit 0