#!/bin/sh -e

outdir=
outfile=
icon=

_init_() {
	# gxmessage won't do progress or HTML at all
	type -t zenity >&/dev/null || exit 1
	outdir="$(mktemp -d)"
	[ -z "$url" ] || outfile="$outdir"/"$(basename "$url")"
	trap 'exit_handler $?' EXIT HUP PIPE
}

exit_handler() {
	local rc=$1
	rm -r -- "$outdir"
	exit $rc
}

Z() {
	zenity --icon-name="$icon" "$@"
}

# this has to be further wrapped with the proper message as an argument
_oktogo() {
	# NB: escape the HTML special chars
	Z --question --text="$@" --title="Помощник" || exit 0
}

# NB: those coughing at this particular killall are welcome with patches
download() {
	userinstall-wget "$outfile" "$url" | (
		trap 'killall userinstall-wget' HUP
		Z --progress --time-remaining --auto-close --auto-kill \
			--text="Подождите, пожалуйста..." \
			--title="Загрузка файла"
	)
}

# has to be wrapped either
_failed() {
	Z --info --text="$@" --title="Ошибка"
	exit 1
}
