#!/bin/sh

set -e

. /usr/share/debconf/confmodule

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

INSTDIR="/var/lib/wine-browser-installer";
PKG_DIR="/usr/share/wine-browser-installer";
SUMS="${PKG_DIR}/${PACKAGE}.sha256sums";

EXITCODE=0;

exit_with_error() {
	echo "$1" >&2;
	eval_gettext "\$SERVICE is NOT fully installed." >&2; echo >&2;
	eval_gettext "Please run 'dpkg-reconfigure \$PACKAGE' to perform the installation again" >&2; echo >&2;
	exit 1;
}

# Go through the list of expected files and copy each one to our installation folder
while read desired_checksum filename permissions url; do
	found_file=0;
	for tempfile in "$@"; do
		actual_checksum=`sha256sum $tempfile | { read checksum file permissions url; echo $checksum; }`;
		if [ "$actual_checksum" = "$desired_checksum" ]; then
			found_file=1;
			destfile="${INSTDIR}/$filename";
			error=0;
			cp "$tempfile" "$destfile" 1>&2 || error=1;
			chmod $permissions "$destfile" 1>&2 || error=1;
			if [ "$error" -eq "1" ]; then
				eval_gettext "Failed to install '\$filename'." 1>&2; echo 1>&2;
				EXITCODE=1;
			fi
		fi
	done
	if [ "$found_file" -eq "0" ]; then
		eval_gettext "Failed to download '\$filename'." 1>&2; echo 1>&2;
		EXITCODE=1;
	fi
done < "${SUMS}";

# Remove all the temporary files
for tempfile in "$@"; do
	rm $tempfile;
done

# Let the user know everything went ok (or not)
if [ -z "$QUIETMODE" ] ; then
	if [ $EXITCODE = 0 ] ; then
		gettext "Everything downloaded and installed."; echo;
	else
		exit_with_error `gettext "One or more components could not be installed."`;
	fi
fi

exit $EXITCODE;
