-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
misc-autosync
executable file
·305 lines (267 loc) · 13.1 KB
/
misc-autosync
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/bin/sh
#description: sync misc static programs and save them to ./misc/$(uname -m)/pkg
bdir="$(cd "$(dirname "${0}")" && pwd)"
alert_mails="m#%#javier.io"
PS4="> "
_basename()
{
[ -z "${1}" ] && return 1 || _basename__name="${1}"
[ -z "${2}" ] || _basename__suffix="${2}"
case "${_basename__name}" in
/*|*/*) _basename__name="$(expr "${_basename__name}" : '.*/\([^/]*\)')" ;;
esac
if [ -n "${_basename__suffix}" ] && [ "${#_basename__name}" -gt "${#2}" ]; then
if [ X"$(printf "%s" "${_basename__name}" | cut -c"$((${#_basename__name} - ${#_basename__suffix} + 1))"-"${#_basename__name}")" \
= X"$(printf "%s" "${_basename__suffix}")" ]; then
_basename__name="$(printf "%s" "${_basename__name}" | cut -c1-"$((${#_basename__name} - ${#_basename__suffix}))")"
fi
fi
printf "%s\\n" "${_basename__name}"
}
_warning()
{
[ -z "${1}" ] && return 1
printf "%b\\n" "${*}" >&2
if command -v "mailgun" >/dev/null; then
for mail in ${alert_mails}; do
mail="$(printf "%s\\n" "${mail}" | sed "s:#%#:@:")"
mailgun --subject "${progname}: WARNING - ${1}" "${mail}" "$(
printf "+---Trace-----------------------------\\n"
printf "%b\\n" "${*}"
printf "+-------------------------------------\\n"
)" >/dev/null
done
fi
}
_die()
{
[ -z "${1}" ] && return 1
printf "%b\\n" "${*}" >&2
if command -v "mailgun" >/dev/null; then
for mail in ${alert_mails}; do
mail="$(printf "%s\\n" "${mail}" | sed "s:#%#:@:")"
mailgun --subject "${progname}: FAILED" "${mail}" "$(
printf "+---Trace-----------------------------\\n"
printf "%b\\n" "${*}"
printf "+-------------------------------------\\n"
)" >/dev/null
done
fi
exit 1
}
_remove_unique_root_dir()
{
[ -z "${1}" ] && return 1
_rurdir__oldpwd="${PWD}"
cd "${1}" || return 1
_rurdir__unique="$(find . -maxdepth 1 -type d ! -iname ".*")"
if [ -d "${_rurdir__unique}" ]; then
case "${_rurdir__unique##*/}" in
bin|boot|dev|etc|opt|usr|var|home|lib|media|mnt|proc|sbin|srv) : ;;
*) _rurdir__mktemp="$(mktemp -d)"
mv "${_rurdir__unique}"/* "${_rurdir__mktemp}" && \
rm -rf "${_rurdir__unique}" && mv "${_rurdir__mktemp}"/* . && \
rm -rf "${_rurdir__mktemp}" || {
_warning "failed to remove unique root dir from ${1}" "${_rurdir__unique} -> ${1}"
cd "${_rurdir__oldpwd}"
return 1
}
;;
esac
fi
cd "${_rurdir__oldpwd}"
}
_normalize()
{
[ -z "${1}" ] && return 1
[ -z "${2}" ] && _normalize__bname="$(_basename "${1}")" || _normalize__bname="$(_basename "${2}")"
_normalize__origin="${1##http://}"
_normalize__origin="${_normalize__origin##https://}"
_normalize__origin="${_normalize__origin##ftp://}"
_normalize__origin="${_normalize__origin##ftps://}"
_normalize__origin="${_normalize__origin%%/*}"
_normalize__output="$(tempfile)"
case "${_normalize__bname}" in
*.tar.gz) return 0 ;;
*.tar.*)
#requires a modern tar version
tar xf "${_normalize__bname}" > "${_normalize__output}" 2>&1 ||
{ _normalize__stdoutb="$(tail -100 "${_normalize__output}")"; rm -rf "${_normalize__output}";
_warning "failed to untar ${_normalize__bname}" "$(printf "%s\\n" "${_normalize__stdoutb}")";
return 1; }
_normalize__bname="$(tar tf "${_normalize__bname}"| head -1)"
;;
*.zip)
unzip -d "${_normalize__bname%%.zip}" "${_normalize__bname}" > "${_normalize__output}" 2>&1 ||
{ _normalize__stdoutb="$(tail -100 "${_normalize__output}")"; rm -rf "${_normalize__output}";
_warning "failed to unzip ${_normalize__bname}" "$(printf "%s\\n" "${_normalize__stdoutb}")";
return 1; }
_remove_unique_root_dir "${_normalize__bname%%.zip}"
_normalize__bname="${_normalize__bname%%.zip}"
;;
*.rar)
unrar "${_normalize__bname}" > "${_normalize__output}" 2>&1 ||
{ _normalize__stdoutb="$(tail -100 "${_normalize__output}")"; rm -rf "${_normalize__output}";
_warning "failed to unrar ${_normalize__bname}" "$(printf "%s\\n" "${_normalize__stdoutb}")";
return 1; }
_normalize__bname="${_normalize__bname%%.rar}"
;;
*)
[ -f "${_normalize__bname}" ] || { _warning "unsupported format ${1}"; return 1; }
;;
esac
_normalize__bname="${_normalize__bname%%/}"
_normalize__target="${_normalize__bname}-${_normalize__origin}"
tar -zcf "${_normalize__target}.tar.gz" "${_normalize__bname}" > "${_normalize__output}" 2>&1 ||
{ _normalize__stdoutb="$(tail -100 "${_normalize__output}")"; rm -rf "${_normalize__output}";
_warning "failed to tar ${_normalize__bname}" "$(printf "%s\\n" "${_normalize__stdoutb}")";
return 1; }
rm -rf "${_normalize__output}" "${_normalize__bname}"
printf "%s" "${_normalize__target}.tar.gz"
}
_repackage()
{
[ -z "${2}" ] && return 1
_repackage__bname="$(_basename "${1}" .tar.gz)"
_repackage__dname="/tmp/"${progname}".repackage"
_repackage_init()
{
mkdir -p "${_repackage__dname}"
[ -d "${_repackage__dname}/${_repackage__bname}" ] && return 0
(cd "${_repackage__dname}" && mkdir "${_repackage__bname}" && tar xf "${1}" -C "${_repackage__bname}")
}
case "${2}" in
bz2) [ -f "${_repackage__dname}/${_repackage__bname}.tar.bz2" ] && return 0
_repackage_init "${1}"
(cd "${_repackage__dname}" && tar -C "${_repackage__bname}" -jcf "${_repackage__bname}".tar.bz2 .)
cp "${_repackage__dname}/${_repackage__bname}.tar.bz2" .
;;
xz) [ -f "${_repackage__dname}/${_repackage__bname}.tar.xz" ] && return 0
_repackage_init "${1}"
(cd "${_repackage__dname}" && tar -C "${_repackage__bname}" -Jcf "${_repackage__bname}".tar.xz .)
cp "${_repackage__dname}/${_repackage__bname}.tar.xz" .
;;
*) rm -rf "${_repackage__dname}" ;;
esac
}
_cook_recipe()
{
[ -f "${1}" ] || return 1
_cook_recipe__output="$(tempfile)"
while read _cook_recipe__url; do
case "${_cook_recipe__url}" in
'#'*) : ;;
*)
_cook_recipe__bname="$(_basename "${_cook_recipe__url}")"
_cook_recipe__rename="${_cook_recipe__bname##*:}"
if [ X"${_cook_recipe__bname}" = X"${_cook_recipe__rename}" ]; then
_cook_recipe__bname_without_suffix="${_cook_recipe__bname%%.tar*}"
else
_cook_recipe__bname_without_suffix="${_cook_recipe__rename}"
_cook_recipe__bname="${_cook_recipe__rename}"
_cook_recipe__bname_without_suffix="${_cook_recipe__bname_without_suffix%%.tar*}"
_cook_recipe__wget_opts="-O ${_cook_recipe__rename}"
fi
_cook_recipe__bname_without_suffix="${_cook_recipe__bname_without_suffix%%.zip}"
_cook_recipe__bname_without_suffix="${_cook_recipe__bname_without_suffix%%.rar}"
_cook_recipe__origin="${_cook_recipe__url##http://}"
_cook_recipe__origin="${_cook_recipe__origin##https://}"
_cook_recipe__origin="${_cook_recipe__origin##ftp://}"
_cook_recipe__origin="${_cook_recipe__origin##ftps://}"
_cook_recipe__origin="${_cook_recipe__origin%%/*}"
(
mkdir -p "${_cook_recipe__origin}"-"${_cook_recipe__bname_without_suffix}"
cd "${_cook_recipe__origin}"-"${_cook_recipe__bname_without_suffix}"
[ -f "${_cook_recipe__bname}" ] || touch "${_cook_recipe__bname}"
_cook_recipe__hash_orig="$(shasum -a 512 "${_cook_recipe__bname}" | awk '{print $1}')"
printf "%s\\n" " fetching ${_cook_recipe__url} ..."
case "${_cook_recipe__url}" in
#http://lists.gnu.org/archive/html/bug-wget/2009-07/msg00080.html
*.zip) rm -rf *.zip ;;
esac
wget -c -N ${_cook_recipe__wget_opts} -d "${_cook_recipe__url%:$_cook_recipe__rename}" > "${_cook_recipe__output}" 2>&1 || \
wget -c --no-check-certificate ${_cook_recipe__wget_opts} -d "${_cook_recipe__url%:$_cook_recipe__rename}" >> "${_cook_recipe__output}" 2>&1 || \
{ _cook_recipe__stdoutb="$(tail -100 "${_cook_recipe__output}")"; rm -rf "${_cook_recipe__output}";
_warning "couldn't retrieve ${_cook_recipe__url}" "$(printf "%s\\n" "${_cook_recipe__stdoutb}")"; }
_cook_recipe__hash_new="$(shasum -a 512 "${_cook_recipe__bname}" | awk '{print $1}')"
if [ "${_cook_recipe__hash_orig}" != "${_cook_recipe__hash_new}" ]; then
_cook_recipe__normal="$(_normalize "${_cook_recipe__url}" "${_cook_recipe__rename}")"
[ -n "${_cook_recipe__normal}" ] || return 1
_repackage "${PWD}/${_cook_recipe__normal}" bz2
_repackage "${PWD}/${_cook_recipe__normal}" xz
_repackage "${PWD}/${_cook_recipe__normal}" cleanup
mv "${_cook_recipe__normal}" ..
mv "${_cook_recipe__normal%%.gz}".bz2 ..
mv "${_cook_recipe__normal%%.gz}".xz ..
fi
)
;;
esac
done < "${1}"
rm -rf "${_cook_recipe__output}"
}
progname="$(_basename ${0})"
nonroot_user="$(getent passwd | awk -F: '{if ($3 >= 1000 && $3 < 60000) print $1}'|head -1)"
if ! command -v "wget" >/dev/null; then
_die "you need to install 'wget' to run this program"
fi
exec 9>/tmp/"${progname}".lock #verify that only one instance is running
if ! flock -n 9; then #http://mywiki.wooledge.org/BashFAQ/045
_die "${progname}: another instance is running";
fi
mkdir -p "${bdir}"/misc-build/i686
mkdir -p "${bdir}"/misc-build/x86_64
mkdir -p "${bdir}"/misc/i686
mkdir -p "${bdir}"/misc/x86_64
#create packages
for recipe in "${bdir}"/misc-autosync-resources/*; do
[ ! -f "${recipe}" ] && continue
brecipe="$(_basename "${recipe}")"
printf "%s\\n" "cooking ${brecipe} recipe ..."
case "${recipe}" in
*.64) (cd "${bdir}"/misc-build/x86_64 && _cook_recipe "${recipe}") ;;
*) (cd "${bdir}"/misc-build/i686 && _cook_recipe "${recipe}") ;;
esac
done
[ ! -f "${bdir}"/misc/lock ] && touch "${bdir}"/misc/lock
#move packages
printf "%s\\n" "moving archives ...";
for archive in "${bdir}"/misc-build/i686/*.tar.gz "${bdir}"/misc-build/i686/*.tar.bz2 "${bdir}"/misc-build/i686/*.tar.xz; do
[ ! -f "${archive}" ] && continue
mv "${archive}" "${bdir}"/misc/i686/
done
for archive in "${bdir}"/misc-build/x86_64/*.tar.gz "${bdir}"/misc-build/x86_64/*.tar.bz2 "${bdir}"/misc-build/x86_64/*.tar.xz; do
[ ! -f "${archive}" ] && continue
mv "${archive}" "${bdir}"/misc/x86_64/
done
#create index
printf "%s\\n" "creating index ...";
(cd "${bdir}"/misc/i686/ && rm -rf sha512sum.* md5sum.*)
(cd "${bdir}"/misc/i686/ && shasum -a 512 *tar* > sha512sum.txt 2>/dev/null)
(cd "${bdir}"/misc/i686/ && md5sum *tar* > md5sum.txt 2>/dev/null)
(cd "${bdir}"/misc/x86_64/ && rm -rf sha512sum.* md5sum.*)
(cd "${bdir}"/misc/x86_64/ && shasum -a 512 *tar* > sha512sum.txt 2>/dev/null)
(cd "${bdir}"/misc/x86_64/ && md5sum *tar* > md5sum.txt 2>/dev/null)
[ X"$(ls "${bdir}"/misc/i686/*tar*|wc -l)" = X"$(wc -l "${bdir}"/misc/i686/sha512sum.txt|awk '{print $1}')" ] || \
_die "Package entries don't match total archives(i686)" \
"sha512 entries:$(wc -l "${bdir}"/misc/i686/sha512sum.txt)\\n" \
"archives :$(ls "${bdir}"/misc/i686/*tar*|wc -l)"
[ X"$(ls "${bdir}"/misc/x86_64/*tar*|wc -l)" = X"$(wc -l "${bdir}"/misc/x86_64/sha512sum.txt|awk '{print $1}')" ] || \
_die "Package entries don't match total archives(x86_64)" \
"sha512 entries:$(wc -l "${bdir}"/misc/x86_64/sha512sum.txt)\\n" \
"archives :$(ls "${bdir}"/misc/x86_64/*tar*|wc -l)"
#compress index
(cd "${bdir}"/misc/i686/ && tar -zcf sha512sum.tar.gz sha512sum.txt)
(cd "${bdir}"/misc/i686/ && tar -jcf sha512sum.tar.bz2 sha512sum.txt)
(cd "${bdir}"/misc/i686/ && tar -Jcf sha512sum.tar.xz sha512sum.txt)
(cd "${bdir}"/misc/i686/ && tar -zcf md5sum.tar.gz md5sum.txt)
(cd "${bdir}"/misc/i686/ && tar -jcf md5sum.tar.bz2 md5sum.txt)
(cd "${bdir}"/misc/i686/ && tar -Jcf md5sum.tar.xz md5sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -zcf sha512sum.tar.gz sha512sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -jcf sha512sum.tar.bz2 sha512sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -Jcf sha512sum.tar.xz sha512sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -zcf md5sum.tar.gz md5sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -jcf md5sum.tar.bz2 md5sum.txt)
(cd "${bdir}"/misc/x86_64/ && tar -Jcf md5sum.tar.xz md5sum.txt)
chown -R "${nonroot_user}:${nonroot_user}" "${bdir}"/misc/
rm "${bdir}"/misc/lock