#!/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 }