#!/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 'configure'.
# If you want to modify configure.ac or Makefile.am, you will need the correct version of autotools, and you should
# run configure (or this script) with --enable-maintainer-mode.

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

echo "Detecting x86/x64 ..."
case "$( uname -m )" in
	"i386"|"i486"|"i586"|"i686"|"x86_64") ENABLE_X86_ASM="--enable-x86-asm" ;;
	*) ENABLE_X86_ASM="--disable-x86-asm --disable-glinjectlib" ;;
esac
echo "x86/x64 = $ENABLE_X86_ASM"

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

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 "Fatal: Detected ffmpeg AND libav, this should not happen!"
		exit 1
	else
		echo "Detected ffmpeg."
		ENABLE_FFMPEG_VERSIONS="--enable-ffmpeg-versions"
	fi
else
	if [ $HAS_LIBAV -gt 0 ]; then
		echo "Detected libav."
		ENABLE_FFMPEG_VERSIONS="--disable-ffmpeg-versions"
	else
		echo "Fatal: Detection failed."
		exit 1
	fi
fi

CONFIGURE_OPTIONS="--disable-assert $ENABLE_X86_ASM $ENABLE_FFMPEG_VERSIONS"

export CPPFLAGS=""
export CFLAGS="-Wall -O2 -pipe"
export CXXFLAGS="-Wall -O2 -pipe"
export LDFLAGS=""

# This shouldn't be needed anymore now that configure uses pkg-config for FFmpeg.
#export CPPFLAGS="$CPPFLAGS `pkg-config --cflags-only-I libavformat libavcodec libavutil libswscale`"
#export LDFLAGS="$LDFLAGS `pkg-config --libs-only-L libavformat libavcodec libavutil libswscale`"

mkdir -p build
cd build
echo "Configuring ..."
if [ x"$( uname -m )" = x"x86_64" ]; then
	
	if [ -d "/usr/lib/i386-linux-gnu" ]; then
		LIB32DIR="/usr/lib/i386-linux-gnu"
	elif [ -d "/usr/lib/i686-linux-gnu" ]; then
		LIB32DIR="/usr/lib/i686-linux-gnu"
	elif [ -d "/usr/lib32" ]; then
		LIB32DIR="/usr/lib32"
	else
		LIB32DIR="/usr/lib"
	fi
	
	if [ -d "/usr/lib/x86_64-linux-gnu" ]; then
		LIB64DIR="/usr/lib/x86_64-linux-gnu"
	elif [ -d "/usr/lib64" ]; then
		LIB64DIR="/usr/lib64"
	else
		LIB64DIR="/usr/lib"
	fi
	
	if [ x"$LIB32DIR" == x"$LIB64DIR" ]; then
		echo "Fatal: Same lib directories found for 32-bit and 64-bit. This should not happen!"
		exit 1
	fi
	PKG_CONFIG_PATH="$LIB64DIR/pkgconfig" \
		../configure --prefix=/usr --libdir=$LIB64DIR $CONFIGURE_OPTIONS "$@"
else
	../configure --prefix=/usr $CONFIGURE_OPTIONS "$@"
fi

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

if [ x"$( uname -m )" = x"x86_64" ]; then
	mkdir -p build32
	cd build32
	echo "Configuring 32-bit GLInject library ..."
	CC="gcc -m32" CXX="g++ -m32" PKG_CONFIG_PATH="$LIB32DIR/pkgconfig" \
		../configure --prefix=/usr --libdir=$LIB32DIR --disable-ssrprogram $CONFIGURE_OPTIONS "$@"
	echo "Compiling 32-bit GLInject library ..."
	make -j "$( nproc )"
	cd ..
fi

echo "Removing old SSR libraries ..."
sudo rm -f "/usr/lib/libssr-glinject."*
sudo rm -f "/usr/lib32/libssr-glinject."*
sudo rm -f "/usr/lib64/libssr-glinject."*
sudo rm -f "/usr/lib/i386-linux-gnu/libssr-glinject."*
sudo rm -f "/usr/lib/i686-linux-gnu/libssr-glinject."*
sudo rm -f "/usr/lib/x86_64-linux-gnu/libssr-glinject."*

cd build
echo "Installing ..."
sudo make install
cd ..

if [ x$( uname -m ) = x"x86_64" ]; then
	cd build32
	echo "Installing 32-bit GLInject library ..."
	sudo make install
	cd ..
fi

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

echo "Done."
