#!/bin/sh
set -e

exit_with_error() {
	echo
	echo $1
	echo "fluxgui is NOT installed."
	exit 1
}

# There is similar code for downloading 'xflux' in
# ../download-xflux.py. If you are changing this you probably want to
# change that too.

case $(dpkg --print-architecture) in
	i386|i586|i686)
		URL=https://justgetflux.com/linux/xflux-pre.tgz
		FILENAME=xflux-pre.tgz
	;;
	amd64)
		URL=https://justgetflux.com/linux/xflux64.tgz
		FILENAME=xflux64.tgz
	;;
	*)
		exit_with_error "Unsupported platform: '$(dpkg --print-architecture)'"
esac

APT_PROXIES=$(apt-config shell http_proxy Acquire::http::proxy https_proxy Acquire::https::Proxy ftp_proxy Acquire::ftp::Proxy dl_direct Acquire::http::Proxy::justgetflux.com)
if [ -n "$APT_PROXIES" ]; then
	eval export $APT_PROXIES
	if [ "$ftp_proxy" = "false" ]; then
		# FTP Proxy is set to false, remove it.
		unset ftp_proxy
	fi
	if [ "$http_proxy" = "false" ]; then
		# HTTP Proxy is set to false, remove it.
		unset http_proxy
	fi
	if [ "$https_proxy" = "false" ]; then
		# HTTPS Proxy is set to false, remove it.
		unset https_proxy
	fi
fi

if [ "$dl_direct" = "DIRECT" ]; then
	unset http_proxy
	unset https_proxy
	unset ftp_proxy
fi

mkdir -p /var/cache/fluxgui
cd /var/cache/fluxgui

echo
echo "Downloading xflux..."

wget -v -N $URL || exit_with_error "Download failed"
echo "Download done."
echo

tar xzf $FILENAME || exit_with_error "Cannot unpack $FILENAME"

if [ "$1" != "upgrade" ] || dpkg --compare-versions "$2" lt 1.1.10; then
	dpkg-divert --package fluxgui --rename --add /usr/bin/xflux
fi

mv xflux /usr/bin/xflux


#DEBHELPER#
