#!/usr/bin/env bash

cd "$(dirname "$0")"

if [ ! -z $1 ] && [ $1 == "clean" ]; then
	echo -n "Do you want to remove 'Makefile', '*.so*' files, 'app' and 'build' directories form '$PWD'? [N/y]: "
	read choice
	if [ ! -z $choice ] && ( [ $choice == 'Y' ] || [ $choice == 'y' ] ); then
		rm -rf $(find . | grep Makefile)
		rm -rf $(find . | grep .Release)
		rm -rf $(find . | grep .Debug)
		rm -rf $(find . | grep build)
		rm -rf $(find . | grep qdbusxml2cpp_wrapper.sh)
		rm -rf $(find . | grep uic_wrapper.sh)
		rm -rf app x86_64-w64-mingw32 i686-w64-mingw32
		rm -f .qmake.stash
		echo "Cleaned!"
		exit
	fi
	exit 1
fi

if [ ! -z $1 ]; then
	Threads=$1
else
	if [ `uname` == "Linux" ]; then
		Threads=$(grep -c processor /proc/cpuinfo 2> /dev/null)
	fi
	if [ -z $Threads ] || [ $Threads == 0 ]; then
		Threads=4
	fi
fi

if [ `uname` == "Darwin" ]; then
	rm -r app/*

	lrelease QMPlay2.pro
	qmake && time make -j$Threads
	BUILD_ERROR=$?

	if [ $BUILD_ERROR != 0 ]; then
		echo Build failed!
	else
		QMPlay2Dir=app/QMPlay2.app/Contents/MacOS
		mv app/bin/QMPlay2.app app || exit 1
		rmdir app/bin
		mv app/lib $QMPlay2Dir || exit 1
		mkdir -p $QMPlay2Dir/share/qmplay2/lang
		mv lang/*.qm $QMPlay2Dir/share/qmplay2/lang
		install_name_tool -change libqmplay2.dylib @executable_path/lib/libqmplay2.dylib $QMPlay2Dir/QMPlay2
		for dylib in $QMPlay2Dir/lib/qmplay2/modules/*.dylib; do
			install_name_tool -change libqmplay2.dylib @executable_path/lib/libqmplay2.dylib $dylib
		done
		cp ChangeLog $QMPlay2Dir/share/qmplay2
		echo Build complete!
	fi

	exit $BUILD_ERROR
fi

if [ -z ${QT_SUFFIX+x} ]; then
	if hash pkg-config 2> /dev/null; then
		pkg-config Qt5Core --atleast-version 5.6.1
		if [ $? == 0 ]; then
			if hash qmake-qt5 2> /dev/null; then
				QT_SUFFIX=-qt5
			elif hash qmake 2> /dev/null; then
				QT_SUFFIX=
			fi
		fi
	fi
	if [ -z ${QT_SUFFIX+x} ]; then
		if hash qmake-qt4 2> /dev/null; then
			QT_SUFFIX=-qt4
		elif hash qmake 2> /dev/null; then
			QT_SUFFIX=
		elif hash qmake-qt5 2> /dev/null; then
			QT_SUFFIX=-qt5
		fi
	fi
fi

# lupdate-qt4 -no-obsolete -locations none QMPlay2.pro

lrelease$QT_SUFFIX QMPlay2.pro

mkdir -p app/share/qmplay2/lang
mv lang/*.qm app/share/qmplay2/lang

if [ $Threads == 0 ]; then
	qmake$QT_SUFFIX && time make
else
	qmake$QT_SUFFIX && time make -j$Threads
fi

BUILD_ERROR=$?

if [ $BUILD_ERROR != 0 ]; then
	echo Build failed!
else
	if [ -d /usr/share/solid/actions ]; then
		SOLID_ACTIONS=app/share/solid/actions
	elif [ -d /usr/share/kde4/apps/solid/actions ]; then
		SOLID_ACTIONS=app/share/kde4/apps/solid/actions
	elif [ -d /usr/share/apps/solid/actions ]; then
		SOLID_ACTIONS=app/share/apps/solid/actions
	fi

	mkdir -p app/share/applications
	cp src/gui/Unix/QMPlay2*.desktop app/share/applications

	if [ `uname` == "Linux" ]; then
		if hash gzip 2> /dev/null; then
			VERSION=$(./version)
			if [ $? == 0 ]; then
				mkdir -p app/share/man/man1
				cp src/gui/Unix/QMPlay2.1.in app/share/man/man1/QMPlay2.1
				sed -i "s/@QMPLAY2_VERSION@/$VERSION/" app/share/man/man1/QMPlay2.1
				gzip -f app/share/man/man1/QMPlay2.1
			fi
		fi
	fi

	if [ ! -z $SOLID_ACTIONS ]; then
		mkdir -p $SOLID_ACTIONS
		cp src/gui/Unix/solid-actions/QMPlay2-opencda.desktop $SOLID_ACTIONS
	fi

	mkdir -p app/share/icons
	cp -r src/gui/Icons/hicolor app/share/icons
	cp {ChangeLog,LICENSE,README.md,TODO,AUTHORS} app/share/qmplay2
	mkdir -p app/include/QMPlay2
	cp src/qmplay2/headers/* app/include/QMPlay2

	echo Build complete!
fi

exit $BUILD_ERROR
