Add apt-zephyr hook.
[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 package ()
17 {
18     package=$1
19     oldver=$2
20     cmp=$3
21     newver=$4
22
23     if [ "$newver" = '-' ] && [ "$oldver" = '-' ]; then
24         echo "Purging $package"
25     elif [ "$newver" = '-' ]; then
26         echo "Removing $package $oldver"
27     elif [ "$oldver" = '-' ]; then
28         echo "Installing $package $newver"
29     elif [ "$cmp" = '<' ]; then
30         echo "Upgrading $package $oldver to $newver"
31     elif [ "$cmp" = '=' ]; then
32         echo "Reinstalling $package $newver"
33     elif [ "$cmp" = '>' ]; then
34         echo "Downgrading $package $oldver to $newver"
35     else
36         echo "I'm confused: $*"
37     fi
38 }
39
40 read -r line
41 case "$line" in
42     'VERSION 2')
43         while read -r line && [ -n "$line" ]; do :; done
44         while read -r package oldver cmp newver action; do
45             case "$action" in
46                 '**CONFIGURE**' | '**REMOVE**')
47                     package "$package" "$oldver" "$cmp" "$newver"
48                     ;;
49                 '**ERROR**')
50                     echo "ERROR on $package $newver"
51                     ;;
52             esac
53         done
54         ;;
55     'VERSION *')
56         echo "$0: unrecognized version: $line"
57         ;;
58     '')
59         ;;
60     *)
61         oldpkgs=$(mktemp -t "apt-zephyr-old.XXXXXX") || exit $?
62         newpkgs=$(mktemp -t "apt-zephyr-new.XXXXXX") || exit $?
63         (echo "$line"; cat) | xargs -r -d '\n' dpkg-deb -W | sort -o "$newpkgs"
64         cut -f 1 "$newpkgs" | xargs -r -d '\n' dpkg-query -W | \
65             sort -o "$oldpkgs"
66         join -t '       ' -j 1 -e '-' "$oldpkgs" "$newpkgs" | \
67             while IFS=' ' read -r package oldver newver; do
68             if dpkg --compare-versions "$oldver" lt "$newver"; then
69                 package "$package" "$oldver" '<' "$newver"
70             elif dpkg --compare-versions "$oldver" eq "$newver"; then
71                 package "$package" "$oldver" '=' "$newver"
72             else
73                 package "$package" "$oldver" '>' "$newver"
74             fi
75         done
76         rm -f "$oldpkgs" "$newpkgs"
77         ;;
78 esac | \
79     zwrite ${CLASS:+-c "$CLASS"} ${INSTANCE:+-i "$INSTANCE"} ${REALM:+-r "$REALM"} ${SIG:+-s "$SIG"} ${OPCODE:+-O "$OPCODE"} ${ZAUTH:--d} ${RECIPIENTS:+$RECIPIENTS}