#!/bin/bash
# Run this if you just want to build and install the program and you don't care about all the details.
# Any additional arguments will be passed on to 'cmake'.
# The following environment variables can be set to TRUE or FALSE to override the auto-detected values:
#  - ENABLE_32BIT_GLINJECT
#  - ENABLE_X86_ASM
#  - ENABLE_FFMPEG_VERSIONS
#  - WITH_QT5
#  - WITH_GLINJECT

set -e
cd "$( dirname "${BASH_SOURCE[0]}" )"

if [ x"$( whoami )" = x"root" ]; then
	echo "Error: don't run this script as root, this will mess up file permissions"
	exit 1
fi

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/lib64/pkgconfig:/usr/local/lib/pkgconfig"

if [ -z "$ENABLE_32BIT_GLINJECT" ] || [ -z "$ENABLE_X86_ASM" ] || [ -z "$WITH_GLINJECT" ]; then
	echo "Detecting architecture ..."
	case "$( uname -m )" in
		"i386"|"i486"|"i586"|"i686")
			: ${ENABLE_32BIT_GLINJECT:="FALSE"}
			: ${ENABLE_X86_ASM:="TRUE"}
			: ${WITH_GLINJECT:="TRUE"}
			;;
		"x86_64")
			: ${ENABLE_32BIT_GLINJECT:="TRUE"}
			: ${ENABLE_X86_ASM:="TRUE"}
			: ${WITH_GLINJECT:="TRUE"}
			;;
		*)
			: ${ENABLE_32BIT_GLINJECT:="FALSE"}
			: ${ENABLE_X86_ASM:="FALSE"}
			: ${WITH_GLINJECT:="FALSE"}
			;;
	esac
fi

if [ -z "$ENABLE_FFMPEG_VERSIONS" ]; then
	echo "Detecting ffmpeg/libav ..."
	if ! pkg-config --exists libavcodec; then
		echo "  Error: libavcodec development package not found, make sure ffmpeg or libav development packages are installed."
		exit 1
	fi
	LIBAVCODEC_INCLUDE_DIR="$( pkg-config --variable=includedir libavcodec )"
	HAS_FFMPEG=$( grep -c "This file is part of FFmpeg." $LIBAVCODEC_INCLUDE_DIR/libavcodec/avcodec.h || true )
	HAS_LIBAV=$( grep -c "This file is part of Libav." $LIBAVCODEC_INCLUDE_DIR/libavcodec/avcodec.h || true )
	if [ $HAS_FFMPEG -gt 0 ]; then
		if [ $HAS_LIBAV -gt 0 ]; then
			echo "  Error: Detected ffmpeg AND libav, this should not happen!"
			exit 1
		else
			echo "  Detected ffmpeg."
			ENABLE_FFMPEG_VERSIONS="TRUE"
		fi
	else
		if [ $HAS_LIBAV -gt 0 ]; then
			echo "  Detected libav."
			ENABLE_FFMPEG_VERSIONS="FALSE"
		else
			echo "  Error: Detection failed."
			exit 1
		fi
	fi
fi

if [ -z "$WITH_QT5" ]; then
	echo "Detecting Qt version ..."
	if pkg-config --exists "Qt5Gui >= 5.7"; then
		echo "  Detected Qt5 (version $( pkg-config --modversion Qt5Gui ))."
		WITH_QT5="TRUE"
	elif pkg-config --exists "QtGui >= 4.8"; then
		echo "  Detected Qt4 (version $( pkg-config --modversion QtGui ))."
		WITH_QT5="FALSE"
	else
		echo "  Error: Qt development package not found, make sure that either Qt4 (4.8 or newer) or Qt5 (5.7 or newer) is installed."
		exit 1
	fi
fi

echo "Auto-detected options:"
echo "  ENABLE_32BIT_GLINJECT = $ENABLE_32BIT_GLINJECT"
echo "  ENABLE_X86_ASM = $ENABLE_X86_ASM"
echo "  ENABLE_FFMPEG_VERSIONS = $ENABLE_FFMPEG_VERSIONS"
echo "  WITH_QT5 = $WITH_QT5"
echo "  WITH_GLINJECT = $WITH_GLINJECT"

PREFIX="/usr"

OPTIONS=()
OPTIONS+=("-DENABLE_32BIT_GLINJECT=$ENABLE_32BIT_GLINJECT")
OPTIONS+=("-DENABLE_X86_ASM=$ENABLE_X86_ASM")
OPTIONS+=("-DENABLE_FFMPEG_VERSIONS=$ENABLE_FFMPEG_VERSIONS")
OPTIONS+=("-DWITH_QT5=$WITH_QT5")
OPTIONS+=("-DWITH_GLINJECT=$WITH_GLINJECT")

if [ x"$WITH_QT5" == x"TRUE" ]; then
	export QT_SELECT="qt5"
else
	export QT_SELECT="qt4"
	OPTIONS+=("-DCMAKE_AUTOMOC_MOC_OPTIONS=-D_SYS_SYSMACROS_H_OUTER")
fi

echo "Entering build-release directory ..."
rm -rf build-release
mkdir build-release
cd build-release

echo "Running cmake ..."
cmake -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_BUILD_TYPE=Release "${OPTIONS[@]}" "$@" ..

echo "Compiling ..."
make -j "$( nproc )"

if [ x"$WITH_GLINJECT" == x"TRUE" ]; then
	echo "Removing old GLInject libraries ..."
	sudo rm -f "/usr/lib/libssr-glinject."*
	sudo rm -f "/usr/lib64/libssr-glinject."*
	sudo rm -f "/usr/lib/x86_64-linux-gnu/libssr-glinject."*
	if [ x"$ENABLE_32BIT_GLINJECT" == x"TRUE" ]; then
		sudo rm -f "/usr/lib32/libssr-glinject."*
		sudo rm -f "/usr/lib/i386-linux-gnu/libssr-glinject."*
		sudo rm -f "/usr/lib/i686-linux-gnu/libssr-glinject."*
	fi
fi

echo "Installing ..."
sudo make install

echo "Leaving build-release directory ..."
cd ..

echo "Running post-install script ..."
sudo ./postinstall

echo "Done."
