#!/bin/sh

INSTDIR="/var/lib/wine-browser-installer";
PKG_DIR="/usr/share/wine-browser-installer";
OTHER_SUMS="${PKG_DIR}/$1.sha256sums";
GLOBAL_SUMS="${PKG_DIR}/wine-browser-installer.sha256sums";
SERVICE="$2";

# Enable translation capabilities
. gettext.sh
export TEXTDOMAIN="netflix-desktop";
export TEXTDOMAINDIR="/usr/share/locale";

SUMS="/tmp/wine-browser.$$.sha256sums";
cat "${GLOBAL_SUMS}" "${OTHER_SUMS}" > "${SUMS}";
while read desired_checksum filename permissions url; do
	if [ ! -f "${INSTDIR}/${filename}" ]; then
		gettext_downloading_file=$(eval_gettext "Downloading '\${filename}'...");
		file="${INSTDIR}/${filename}";
		tmplog="/tmp/netflix-desktop.$$.tmp";
		touch "${tmplog}";
		wget -o"${tmplog}" -O"${file}" "${url}" &
		WGETPID=$!;
		(
			while kill -0 ${WGETPID} 2>/dev/null; do
				tail -n 1 "${tmplog}" | grep '% ' | cut -b 63-65 | sed 's/ //';
			done
		) | zenity --progress --auto-kill --auto-close --title="${SERVICE}" --text="${gettext_downloading_file}";
		kill ${WGETPID}; # if cancelled
		rm "${tmplog}";
	fi
done < "${SUMS}";
rm "${SUMS}";
