#!/bin/sh

INSTDIR="/usr/share/netflix-desktop";
NETFLIX_DESKTOP="${HOME}/.netflix-desktop";
NETFLIX_TMP="${HOME}/.netflix-tmp";
LOCAL_SUMS="${NETFLIX_DESKTOP}/netflix-desktop.sha256sums";
GLOBAL_SUMS="${INSTDIR}/sha256sums";
WINE="/usr/bin/wine";
SHOWDEBUG="1";

# Process any command-line arguments
for arg in "$@"; do
	case "$arg" in
		--showdebug)
			SHOWDEBUG="1";
			;;
		*)
			echo "Unrecognized command-line argument '$arg', usage:";
			echo "netflix-desktop [options]

Options:
\t--showdebug\tShow Wine debug output.";
			exit;
			;;
	esac;
done

# Make sure that all the nencessary files are installed.
files_missing=1;
while [ "$files_missing" -eq "1" ]; do
	files_missing=0;
	while read desired_checksum filename permissions url; do
		if [ ! -f "${NETFLIX_TMP}/${filename}" ]; then
			files_missing=1;
		fi
	done < ${INSTDIR}/sha256sums;
	if [ "$files_missing" -eq "1" ]; then
		zenity --question --title="Netflix Desktop" --text="Not all of the components required by Netflix Desktop were downloaded, would you like to download them now? (requires an Internet connection and sudo permissions)" --ok-label="Yes" --cancel-label="No";
		RET=$?;
		if [ "$RET" -eq "1" ]; then
			exit;
		fi
		if [ ! -d "${NETFLIX_TMP}" ]; then
			mkdir ${NETFLIX_TMP}
		fi
		(
			echo "1";
			wget -O ${NETFLIX_TMP}/FirefoxSetup.exe "http://download.mozilla.org/?product=firefox-17.0&os=win&lang=en-US" 2>&1 >/dev/null;
			echo "50";
			wget -O ${NETFLIX_TMP}/SilverlightSetup.exe "http://silverlight.dlservice.microsoft.com/download/6/A/1/6A13C54D-3F35-4082-977A-27F30ECE0F34/10329.00/runtime/Silverlight.exe" 2>&1 >/dev/null;
                	echo "100";
        	) | zenity --progress --pulsate --no-cancel --auto-close --text="Downloading required components...";

	fi
done

# Make sure that the MS core true type fonts are installed
if [ ! -e "$HOME/.fonts/tahoma.ttf" ]; then
	zenity --question --title="Netflix Desktop" --text="Netflix requires Microsofts core font family to be installed, would you like to download and install them now? (requires an Internet connection)" --ok-label="Yes" --cancel-label="No";
	RET=$?;
	if [ "$RET" -eq "1" ]; then
		exit;
	fi
	(
	echo "1";
	${INSTDIR}/install-fonts;
	echo "100";
	) | zenity --progress --pulsate --no-cancel --auto-close --text="Downloading and configuring fonts...";
fi

# Copy over the specially configured user profile if installation has never been performed previously
if [ ! -f "${LOCAL_SUMS}" ]; then
	mkdir -p "${NETFLIX_DESKTOP}/drive_c";
	cp -a "${INSTDIR}/netflix-profile" "${NETFLIX_DESKTOP}/drive_c/netflix-profile";
fi
# If no previous installation has been performed (or new installation packages are available) then install the software we need to the local prefix
if [ "$(cmp "${LOCAL_SUMS}" "${GLOBAL_SUMS}" > /dev/null; echo $?)" -ne "0" ]; then
	TASK="local installation";
	if [ -f "${LOCAL_SUMS}" ]; then
		TASK="profile upgrade";
	fi
	(
		echo "1";
		WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "${NETFLIX_TMP}/FirefoxSetup.exe" /S > /dev/null;
		WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "${NETFLIX_TMP}/SilverlightSetup.exe" /q > /dev/null;
		rm -f "${HOME}/Desktop/Mozilla Firefox.desktop" 2>&1 >/dev/null;
		echo "100";
	) | zenity --progress --pulsate --no-cancel --auto-close --text="Performing ${TASK}...";
	cp "${GLOBAL_SUMS}" "${LOCAL_SUMS}";
fi

if [ "$(uname -m)" = "x86_64" ]; then
  # Launch Firefox with our specially prepared profile
  if [ "${SHOWDEBUG}" -eq "1" ]; then
        WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" -profile "C:\\netflix-profile" "http://www.netflix.com/";
  else
        WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "C:\\Program Files (x86)\\Mozilla Firefox \\(x86\)\\firefox.exe" -profile "C:\\netflix-profile" "http://www.netflix.com/" 2>/dev/null;
  fi
else
  # Launch Firefox with our specially prepared profile
  if [ "${SHOWDEBUG}" -eq "1" ]; then
  	WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "C:\\Program Files\\Mozilla Firefox\\firefox.exe" -profile "C:\\netflix-profile" "http://www.netflix.com/";
  else
	WINEPREFIX="${NETFLIX_DESKTOP}" ${WINE} "C:\\Program Files\\Mozilla Firefox\\firefox.exe" -profile "C:\\netflix-profile" "http://www.netflix.com/" 2>/dev/null;
  fi
fi
