apt-zephyr: Make send_zephyr a function.
[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 read -r line
53 case "$line" in
54     'VERSION 2')
55         while read -r line && [ -n "$line" ]; do :; done
56         while read -r package oldver cmp newver action; do
57             case "$action" in
58                 '**CONFIGURE**' | '**REMOVE**')
59                     package "$package" "$oldver" "$cmp" "$newver"
60                     ;;
61                 '**ERROR**')
62                     echo "ERROR on $package $newver"
63                     ;;
64             esac
65         done
66         ;;
67     'VERSION *')
68         echo "$0: unrecognized version: $line"
69         ;;
70     '')
71         ;;
72     *)
73         oldpkgs=$(mktemp -t "apt-zephyr-old.XXXXXX") || exit $?
74         newpkgs=$(mktemp -t "apt-zephyr-new.XXXXXX") || exit $?
75         (echo "$line"; cat) | xargs -r -d '\n' dpkg-deb -W | sort -o "$newpkgs"
76         cut -f 1 "$newpkgs" | xargs -r -d '\n' dpkg-query -W | \
77             sort -o "$oldpkgs"
78         join -t '       ' -j 1 -e '-' "$oldpkgs" "$newpkgs" | \
79             while IFS=' ' read -r package oldver newver; do
80             if dpkg --compare-versions "$oldver" lt "$newver"; then
81                 package "$package" "$oldver" '<' "$newver"
82             elif dpkg --compare-versions "$oldver" eq "$newver"; then
83                 package "$package" "$oldver" '=' "$newver"
84             else
85                 package "$package" "$oldver" '>' "$newver"
86             fi
87         done
88         rm -f "$oldpkgs" "$newpkgs"
89         ;;
90 esac | send_zephyr