Initial import
[debian/deb-download-from-pool.git] / deb-download-from-pool.sh
1 #!/bin/sh
2 #
3 # deb-download-from-pool.sh - download specific packages from the pool
4 #
5 # Copyright (C) 2018  Antonio Ospite <ao2@ao2.it>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 set -e
21
22 MIRROR="${MIRROR:-https://deb.debian.org/debian}"
23 DEBUG_MIRROR="${DEBUG_MIRROR:-http://debug.mirrors.debian.org/debian-debug}"
24
25 usage() {
26   PROG_NAME=$(basename "$0")
27   cat <<EOF
28 usage: $PROG_NAME [--all|-h|--help] <package> [<version>]
29
30 Helper script to download specific packages from the pool directory of
31 a Debian mirror.
32
33
34 Options:
35   --all               download all related packages even if currently
36                       uninstalled
37   -h, --help          display this usage message and exit
38
39
40 Notes:
41
42 Some settings can be changed via environment variables:
43   SOURCE_PACKAGE
44   POOL_DIRECTORY
45   MIRROR
46   DEBUG_MIRROR
47 EOF
48 }
49
50 download() {
51   dpkg --validate-pkgname -- "$1" || { usage 1>&2; exit 1; }
52
53   PACKAGE="$1"
54
55   if [ "x$SOURCE_PACKAGE" = "x" ];
56   then
57     SOURCE_PACKAGE=$(apt-cache show "$PACKAGE" | grep Source | head -1 | cut -d ' ' -f 2)
58     if [ "x$SOURCE_PACKAGE" = "x" ];
59     then
60       SOURCE_PACKAGE="$PACKAGE"
61     fi
62   fi
63
64   if [ "x$POOL_DIRECTORY" = "x" ];
65   then
66     POOL_DIRECTORY=$(apt-cache showsrc "$SOURCE_PACKAGE" | grep Directory | head -1 | cut -d ' ' -f 2)
67   fi
68
69   POOL_URL="${MIRROR}/${POOL_DIRECTORY}"
70
71   if [ "x$2" = "x" ];
72   then
73     echo "Available versions, pass one of these as the second argument:"
74     wget -q "$POOL_URL" -O - | grep "${SOURCE_PACKAGE}_.*\\.dsc" | sed -e 's/^.*<a .*>.*_\(.*\)\.dsc<\/a>.*$/\1/g'
75     exit $?
76   fi
77
78   dpkg --validate-version -- "$2" || { usage 1>&2; exit 1; }
79
80   VERSION="$2"
81
82   ARCH=$(dpkg-architecture -qDEB_BUILD_ARCH)
83   PACKAGES=$(wget -q "$POOL_URL" -O - | \
84     grep -e "${VERSION}[^_]*_${ARCH}" \
85     -e "${VERSION}[^_]*_all" | \
86     sed -e 's/^.* href="\([^"]*\)".*$/\1/g')
87
88   for p in $PACKAGES;
89   do
90     PNAME=$(echo "$p" | cut -d '_' -f 1);
91     STATUS=$(grep-status -X \( -F Architecture "$ARCH" --or -F Architecture all \) --and -X -F Package "$PNAME" -n -s Status || true)
92     if [ "$STATUS" = "install ok installed" ] || [ "$DOWNLOAD_ALL" = "true" ];
93     then
94       wget -nv "${POOL_URL}/${p}"
95     fi
96   done
97 }
98
99
100 [ $# -eq 0 ] && { usage 1>&2 && exit 1; }
101
102 while [ $# -gt 0 ];
103 do
104   case "$1" in
105     --)
106       shift
107       break
108       ;;
109     -h|--help)
110       usage
111       exit 0
112       ;;
113     --all)
114       DOWNLOAD_ALL="true"
115       shift
116       ;;
117     -*)
118       echo "Error: Unknown option '${1}'" 1>&2
119       shift
120       ;;
121     *)
122       break
123       ;;
124   esac
125 done
126
127 download "$@"
128 MIRROR=$DEBUG_MIRROR download "$@"