mirror of
https://github.com/notwa/rc
synced 2024-11-05 06:49:03 -08:00
127 lines
2.3 KiB
Bash
Executable file
127 lines
2.3 KiB
Bash
Executable file
#!/usr/bin/env sh
|
|
# compat: -ash +bash +dash +hush +ksh +mksh +oksh +osh +posh +yash +zsh
|
|
# FIXME: `find -delete` is not POSIX (fails on busybox).
|
|
|
|
__grab() { ### @grab - download a file from my site and verify its integrity by its [minisign](https://github.com/jedisct1/minisign/) signature.
|
|
[ -z "${ZSH_VERSION}" ] || emulate sh
|
|
#!/usr/bin/env sh
|
|
umask 002
|
|
pub=RWQdYTxDsppw5ZFpYZFoF6IoPS0okMKlOiz9MwTl0NUoP41r57sf9dI1
|
|
remote=eaguru.guru/t
|
|
unset t
|
|
|
|
_(){
|
|
task="${1:-do something}"
|
|
}
|
|
|
|
will(){
|
|
_ "$*"
|
|
}
|
|
|
|
die(){
|
|
x=${?#0}
|
|
printf "failed to %s\n" "$task" >&2
|
|
exit ${x:-1}
|
|
}
|
|
|
|
esc(){
|
|
while [ -n "$1" ]; do
|
|
case "$1" in
|
|
([\!\"$\&-\>@-~]*) printf %.1s "$1";;
|
|
(*) printf %%%02X "'$1";;
|
|
esac
|
|
set -- "${1#?}"
|
|
done
|
|
} 2>&-
|
|
|
|
get(){
|
|
if [ -f /usr/bin/curl ]
|
|
then curl -sSfLgm5 -o "$@"
|
|
else wget -qt1 -T5 -O "$@"
|
|
fi
|
|
}
|
|
|
|
clean(){
|
|
will clean up temporary files
|
|
! [ -d "$t" ] || rm -r "$t" || die
|
|
exit 1
|
|
}
|
|
|
|
hex(){
|
|
if [ -f /usr/bin/xxd ]
|
|
then exec xxd -p -l4 -- "$@"
|
|
else exec hexdump -ve '1 1 "%02x"' -n4 -- "$@"
|
|
fi
|
|
}
|
|
|
|
owd="${PWD:?unsupported}"
|
|
|
|
will create temporary directory
|
|
trap clean INT EXIT
|
|
t="$(exec mktemp -d)" && cd "$t" || die
|
|
|
|
for f
|
|
do
|
|
fn="${f##*/}"
|
|
fn="${fn%%\?*}" # for shame, wget
|
|
fn="${fn%%#*}"
|
|
|
|
will ensure filename is valid
|
|
case "$fn" in
|
|
(.) false;;
|
|
(..) false;;
|
|
([!\ -~]) false;;
|
|
([\":\<\>?\\\|]) false;;
|
|
(?*) true;;
|
|
(*) false;;
|
|
esac || die
|
|
touch -- "$fn" || die
|
|
|
|
will ensure supported protocols # busybox restricts this
|
|
fx="$(esc "$fn")"
|
|
case "$f" in
|
|
(http://*);;
|
|
(https://*);;
|
|
(ftp://*);;
|
|
(ftps://*);; # buggy
|
|
(*:*)
|
|
printf 'unsupported protocol: %s\n' "${f%%:*}" >&2
|
|
die;;
|
|
(*)
|
|
f="https://$remote/$fx";;
|
|
esac
|
|
g="https://$remote/$fx.minisig"
|
|
|
|
will download "$fn" from "$f"
|
|
get "$fn" -- "$f" || die
|
|
|
|
will download "$fn.minisig" from "$g"
|
|
get "$fn.minisig" -- "$g" || die
|
|
|
|
will verify file integrity of "$fn"
|
|
minisign -QVm "$fn" -P "$pub" || die
|
|
|
|
will determine basic file type
|
|
x4="$(hex "$fn")" || die
|
|
x2="${x4%????}"
|
|
if [ "$x4" = 7f454c46 ] || [ "$x2" = 2321 ] || [ "$x2" = 4d5a ]
|
|
then
|
|
will set executable permission
|
|
chmod +x -- "$fn" || die
|
|
fi
|
|
|
|
done
|
|
|
|
will remove temporary files
|
|
find -name '*.minisig' -delete || die
|
|
|
|
will return files to original directory
|
|
cd "$owd" && find "$t" -type f -exec mv -- {} . \; || die
|
|
|
|
will remove temporary directory # assuming it's now emptied
|
|
rmdir "$t" || die
|
|
trap - INT EXIT
|
|
}
|
|
|
|
grab()(__grab "$@")
|
|
[ -n "${preload+-}" ] || __grab "$@"
|