#!/bin/bash

check_system() {
	# Check that the unzip command is available
	if ! type unzip >/dev/null 2>&1 ; then
		echo "Please install unzip, e.g.:  apt-get install unzip / pacman -S unzip" >&2
		return 1
	fi

	if ! type wget &> /dev/null ; then
		echo "Please install wget, e.g.:  apt-get install wget / pacman -S wget" >&2
		return 1
	fi

	if [ "$EUID" != 0 ] ; then
		echo "Please run this script as root, e.g.:  sudo bash inst_ft.sh" >&2
		return 1
	fi

	if [ -z "$TDIR" ] ; then
		echo "mktemp did not provide a temporary directory!" >&2
		return 1
	fi

	# Success
	return 0
}


want_install_java() {
	# Returns whether Java 8 needs to be downloaded & installed

	# Check whether jre8 is already installed
	grep "^JAVA_VERSION=" "$FT_JAVA_HOME/release" | cut -d\" -f2 | grep -q "_${JUVER}$" && return 1

	# Always install, for now, to ensure that users have an up-to-date JRE.
	# To be revisited when e.g. openjdk8 is stable.
	return 0

	# Ask Java to show its version
	if java -version 2>&1 | head -n 1 | grep -q '"1\.8' ; then
		FT_JAVA_HOME=""
		return 1
	fi

	[ -e "$FT_JAVA_HOME/bin/java" ] && return 1

	# Needs to be installed by this script
	return 0
}


install_java() {
	# 32 or 64-bit
	A="x64" &&
	if [ "$(uname -m)" != "x86_64" ] ; then
		A="i586"
	fi &&
	J_FNAME="jre-8-linux-x64.tar.gz" &&

	# Download java automatically
	wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" "https://filetopia.org/linux/jre-8-linux-x64.tar.gz" -O "$J_FNAME" &&

	# Install java
	mkdir -p "$FT_HOME" "$FT_JAVA_HOME" &&
	tar xf "$J_FNAME" --no-same-owner &&
	pushd "jre1.8.0_${JUVER}" > /dev/null &&
	cp -a * "$FT_JAVA_HOME" &&
	echo "Java Runtime 8 is in $FT_JAVA_HOME" &&
	popd > /dev/null
}


update_icon_cache() {
	if type gtk-update-icon-cache >/dev/null 2>&1 ; then
		gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor
	fi

	# Ignore any error from gtk-update-icon-cache - not our problem
	return 0
}


show_info() {
	echo "FiletopiaFX is in $FT_HOME"
	echo "Run filetopiafx (as normal user) to start."
	echo "It will also be in the desktop menu, under Network or Internet."
	return 0
}


cleanup() {
	if [ -n "$TDIR" ] ; then
		cd /
		# Remove the temporary download dir
		rm -rf "$TDIR"
	fi
}


umask 0022 &&
# Temp directory, to download into
TDIR="$(mktemp -d --suffix=.ftfx)" &&

if ! check_system ; then
	echo "Checks failed - aborting." >&2
	exit 1
fi &&

cd "$TDIR" &&

FT_HOME="/opt/filetopiafx" &&
FT_JAVA_HOME="$FT_HOME/jre8" &&
FT_FNAME="filetopiaFX.zip" &&
EXEC_JAVA_CMD="exec java -jar -Dfile.encoding=UTF8 FiletopiaFX.jar" &&
JUVER=192 &&
JBVER=11 &&

install_java  &&

# Download the app, showing progress
wget "http://www.filetopia.org/$FT_FNAME" -O "$FT_FNAME" &&

# Install the app
unzip -q "$FT_FNAME" &&
cd filetopiafx &&
mkdir -p "$FT_HOME" &&
cp -r *.jar home lib "$FT_HOME" &&

# Desktop entry
cat > f.d << EOF &&
[Desktop Entry]
Type=Application
Name=Filetopia FX
Comment=Secure chat and file sharing
Exec=filetopiafx
Icon=filetopiafx
Terminal=false
Categories=Network;
StartupNotify=true
EOF

install -Dm 644 f.d /usr/share/applications/filetopiafx.desktop || return 1

# Startup executable
mkdir -p /usr/bin &&
if [ -z "$FT_JAVA_HOME" ] ; then
	# No need to setup java
	cat > /usr/bin/filetopiafx << EOF
#!/bin/sh

cd "$FT_HOME" &&
$EXEC_JAVA_CMD
EOF
else
	cat > /usr/bin/filetopiafx << EOF
#!/bin/sh

cd "$FT_HOME" &&

export JAVA_HOME="$FT_JAVA_HOME" &&
export PATH="\$JAVA_HOME/bin:\$PATH" &&
$EXEC_JAVA_CMD
EOF
fi &&

chmod 755 /usr/bin/filetopiafx &&

# Icons
f="filetopiafx.png" &&
for n in 16 24 32 48 64 128 256 ; do
	d="/usr/share/icons/hicolor/${n}x${n}/apps" &&
	if [ ! -e "$d/$f" ] ; then
		# Download & install icon
		wget -q "http://www.filetopia.org/img/icons/ftfx${n}.png" -O "$n" &&
		install -Dm 644 "$n" "$d/$f"
	fi
done &&

show_info &&
update_icon_cache

cleanup
