@ -1,5 +1,16 @@
#!/bin/sh
# We're going to need to remember failures from the *left* side of a pipeline...
# shellcheck disable=SC2039
if set -o pipefail 2>/dev/null; then
WITH_PIPEFAIL="true"
else
# But because we can't have nice things, some devices (hi, Kindle) may use an extremely old busybox build,
# one in which pipefail was not yet implemented,
# (it appeared in busybox 1.16.0, and is contingent on bash compatibility being enabled in ash).
WITH_PIPEFAIL="true"
fi
# Figure out whether that's a delta or a full download given the number of arguments passed...
if [ $# -lt 7 ]; then
ZSYNC_MESSAGE="Downloading update data"
@ -7,43 +18,34 @@ else
ZSYNC_MESSAGE="Computing zsync delta"
fi
# Small zsync wrapper so we can get a pretty spinner while it works...
./fbink -q -y -7 -pmh "${ZSYNC_MESSAGE} ! "
# Small zsync wrapper so we can print its output while it works...
./fbink -q -y -7 -pmh "${ZSYNC_MESSAGE} . . . "
# Clear any potential leftover from the local OTA tarball creation.
./fbink -q -y -6 -pm ' '
# Spin in the background while we work ;).
(
# See https://stackoverflow.com/questions/2685435 for inspiration
# as well as https://www.npmjs.com/package/cli-spinners
# & https://github.com/swelljoe/spinner
# http://www.fileformat.info/info/unicode/block/block_elements/list.htm
##
# Simple bars, they look better when a bit smoother, with a snappier interval (~250ms)
#SPINNER="<22> ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ▇ ▆ ▅ ▄ ▃ ▂ ▁"
#SPINNER="<22> ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ ▉ ▊ ▋ ▌ ▍ ▎ ▏"
# Spinning blocks
SPINNER="▖ ▘ ▝ ▗"
#SPINNER="▜ ▟ ▙ ▛"
# Snaking blocks
#SPINNER="▌ ▀ ▐ ▄"
#SPINNER="▌ ▛ ▀ ▜ ▐ ▟ ▄ ▙"
while :; do
for spin in ${SPINNER}; do
usleep 500000 2>/dev/null || sleep 0.5
# NOTE: Throw stderr to the void because I'm cheating w/ U+FFFD for a blank character,
# which FBInk replaces by a blank, but not before shouting at us on stderr ;).
./fbink -q -y -7 -pmh "${ZSYNC_MESSAGE} ${spin}" 2>/dev/null
done
done
) &
# Launch zsync2, and remember its exit code...
./zsync2 "$@"
rc=$?
# We'll be piping its output to FBInk in order to print it live, in a smaller font than usual...
# NOTE: This depends on an undocumented FBInk hack (-z) in order to deal with the progress bars (and their CR) sanely.
rc=0
# Kill the spinner subshell now that we're done
kill -15 $!
if [ "${WITH_PIPEFAIL}" = "true" ]; then
# We can use pipefail, let the shell do the heavy lifting for us...
./zsync2 "$@" 2>&1 | ./fbink -q -z -pm -F scientifica
rc=$?
else
# We cannot use pipefail, extreme shell wizardry is required...
# Pure magic courtesy of https://unix.stackexchange.com/a/70675
# Much more compact than the stock answer from the Usenet shell FAQ: http://cfajohnson.com/shell/cus-faq-2.html#Q11
{ { { {
./zsync2 "$@" 2>&1 3>&- 4>&-
echo $? >&3
} | ./fbink -q -z -pm -F scientifica >&4; } 3>&1; } | {
read -r xs
exit "${xs}"
}; } 4>&1
rc=$?
fi
# And return with zsync's exit code, not kill's ;).
# And return with zsync's exit code
exit ${rc}