useful-scripts/bin/.inc/common
Jörn-Michael Miehe bc40c2e4d5 🔧 various fixes
- add `download_gui` to the `common` script
- unify indentation
- prefer `notify-send` over `zenity` for better notifications
2025-11-12 00:50:05 +01:00

70 lines
1.7 KiB
Bash

#!/bin/sh
# check if a command is available
has_command() { # $command
command -v "${1}" 1>/dev/null 2>/dev/null
}
needs_commands() { # $status $cmd1 $cmd2 ...
_nc_status="${1}"
shift 1
for _nc_cmd in "${@}"; do
if ! has_command "${_nc_cmd}"; then
echo "Command '${_nc_cmd}' not available!" >&2
exit "${_nc_status}"
fi
done
}
download_gui() { # $uri $file $sha256
needs_commands "curl" "zenity" "sha256sum" || return 1
echo "Downloading '${1}' to '${2}'"
curl --show-error --fail --location \
--output "${2}" \
"${1}" &
_dg_curlpid=$!
yes | zenity \
--progress --pulsate --auto-close \
--title="Downloading" \
--text="Downloading '${2}' ..." &
_dg_zenpid=$!
while kill -0 "${_dg_curlpid}" 2>/dev/null; do
if ! kill -0 "${_dg_zenpid}" 2>/dev/null; then
# progress bar died; ask for abort
if zenity \
--question \
--text="Abort Download?"; then
kill "${_dg_curlpid}" 2>/dev/null || true
echo "Download cancelled!" >&2
break
fi
# restart progress bar
yes | zenity \
--progress --pulsate --auto-close \
--title="Downloading" \
--text="Downloading '${2}' ..." &
_dg_zenpid=$!
fi
sleep 0.2
done
kill "${_dg_zenpid}" 2>/dev/null || true
wait "${_dg_curlpid}"
if [ -z "${3}" ]; then
echo "No SHA256 (OK)!" >&2
return 0
elif [ "$( sha256sum "${2}" | cut -d' ' -f1 )" = "${3}" ]; then
echo "SHA256 ${3} OK!" >&2
return 0
else
echo "SHA256 ${3} mismatch!" >&2
return 1
fi
}