/
pp
executable file
·61 lines (47 loc) · 1.56 KB
/
pp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/bash
#
# A light wrapper for pacman, implementing parallel downloading
# Requires: aria2
#
shopt -s extglob
PACARGS=("$@")
PACCACHE="/var/cache/pacman/pkg"
SYNC=0 BORING_OPT=0 DL_ONLY=0
# this is the easiest way to catch interesting operations we only want to catch
# things like -S or -Syu. Everything else will be passed straight to pacman
while getopts 'DQRSTUVb:cdefghiklmnopqr:stuvwy' opt; do
case $opt in
S) SYNC=1 ;;
@(c|g|i|q|s)) BORING_OPT=1 ;;
w) DL_ONLY=1 ;;
esac
done
(( !SYNC || BORING_OPT )) && exec pacman $@
(( UID != 0 )) && { printf "Must be root!\n"; exit 1; } >&2
# find alternate pacman cache location
paccache=$(awk -F'[[:space:]]*=[[:space:]]*' '/^[[:space:]]*CacheDir/ { print $2; exit; }' /etc/pacman.conf)
[[ $paccache ]] && PACCACHE="$paccache"
unset paccache
# read in urls to packages needed for transaction
IFS=$'\n' read -r -d'\0' -a pkgs < <(pacman -p "${PACARGS[@]}" | grep -E '^(ht|f)tp')
# exit on null array
[[ -z "${pkgs[@]}" ]] && { printf "Nothing to do!\n"; exit 0; }
# create a dl manifest, so we don't pass superfluous URLs to aria
manifest=()
for pkg in "${pkgs[@]}"; do
[[ -f $PACCACHE/${pkg##*/} ]] || manifest+=("$pkg")
done
if [[ ${manifest[@]} ]]; then
printf ":: Packages to be downloaded:\n"
for pkg in "${manifest[@]}"; do
printf " ==> %s\n" "${pkg##*/}"
done
fi
# filthy. strip out any -y option
ARGS=()
for arg; do
[[ $arg = -*y* ]] && arg=${arg//y/}
ARGS+=("$arg")
done
aria2c -j 10 --dir "$PACCACHE" -i - < <(printf "%s\n" "${manifest[@]}")
(( DL_ONLY )) || exec pacman "${ARGS[@]}"