-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-debs
executable file
·166 lines (142 loc) · 3.04 KB
/
update-debs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash -eu
DEBS=${DEBS:-/srv/local-apt-repository}
CONFS=${CONFS:-$(dirname $(realpath $0))}
DEBUG=${DEBUG:-0}
dumpvar() {
declare -p "$1" | sed -r 's/^[^=]*=\(?//; s/\)?$//'
}
pp() {
echo -n "$1: " >&2
dumpvar $1 >&2
}
extract_array() {
local -n ret=$1 s=$2
eval "ret=($s)"
}
github.expand_conf() {
local -n ret2=$1
local -nr rw=$2
local repos=${rw[1]} pkg=${rw[2]-}
github.expand_repos repos
github.expand_pkg pkg repos
ret2+=([repos]=$repos [pkg]=$pkg)
}
github.expand_repos() {
local -n ret=$1
if [[ $ret == */* ]]; then
return
fi
ret=$ret/$ret
}
github.expand_pkg() {
local -n ret=$1
local -nr repo=$2
if [[ $ret == *.deb ]]; then
return
fi
if [[ -z "$ret" ]]; then
ret=${repo#*/}
fi
ret="${ret}_%v_%a.deb"
}
normalize_proto() {
local -n ret2=$1 lin=$2
case $ret2 in
https|http)
ret2=direct
;;
github)
;; # let it be
*)
echo unknown protocol: "'$proto'" in "$lin" >&2
exit 2
;;
esac
}
direct.expand_conf() {
local -n ret2=$1
local -nr lin=$3
ret2+=([url]="$lin")
}
expand_conf() {
local -n ret=$1
local -r line="$(echo "$2" | sed 's/ *#.*//')"
local -ra row=(${line//:/ })
local proto=${row[0]}
normalize_proto proto line
ret=([proto]=$proto)
$proto.expand_conf ret row line
}
load_conf() {
local -r path=$CONFS/update-debs-source.conf
local -n ret=$1
local line
local -A entry
while read -r line; do
expand_conf entry "$line"
ret+=("$(dumpvar entry)")
done < $path
}
direct.fetch() {
local -n entr=$1
local -r url="${entr[url]}"
pushd "$DEBS" >/dev/null
if (( DEBUG )); then
echo "$url" >&2
popd >/dev/null
return
fi
$wget -q --content-disposition -N "$url"
popd >/dev/null
}
github.latest_version() {
local -n ret=$1
local -r base="$2"
ret=$($wget --spider -S "$base" 2>&1|sed -r 's| Location: .*/v?([^/]+)$|\1|p;d')
}
github.url_base() {
local -n ret=$1
local -r repos="$2"
ret="https://github.com/$repos/releases/latest/download"
}
github.format_pkg() {
local -n ret=$1
local -r pk=$2 v=$3
ret=$(echo $pk | sed "s/%a/$arch/; s/%v/$v/")
}
github.url() {
local -n ret=$1 e=$2
local -r repos="${e[repos]}"
local base ver pkg
github.url_base base $repos
github.latest_version ver "$base"
github.format_pkg pkg "${e[pkg]}" $ver
ret="$base/$pkg"
}
github.fetch() {
local -nr ent=$1
local url
github.url url ent
local -rA e=([url]="$url")
direct.fetch e
}
fetch() {
local -rn cnf=$1
local -A entry
local buf
for buf in "${cnf[@]}"; do
extract_array entry buf
${entry[proto]}.fetch entry &
done
wait
}
main() {
# globals
local -r arch=$(dpkg --print-architecture)
local -r wget='wget --no-verbose'
local -r progname=$(basename $0)
local -a conf
load_conf conf
fetch conf
}
main "$@"