#!/bin/bash -e

#
#  Singe 2
#  Copyright (C) 2006-2020 Scott Duensing <scott@kangaroopunch.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

source source.inc.sh

G_L="-------------------------------------------------------------------------------"

function doBuild() {

	local TARGET=$1
	local OSNAME=$2
	local OSARCH=$3
	local EXT=$4
	local SOURCE=
	local I=
	local OFILES=""
	local O=

	# Override this
	QMAKE_CFLAGS="-isystem ../../thirdparty-build/${OSNAME}/${OSARCH}/installed/include -isystem ../../singe/thirdparty/arg_parser -isystem ../../singe/thirdparty/manymouse"

	pushd "${SOURCE_DIR}/.."
	mkdir -p build/${OSNAME}/${OSARCH}
	cd build
	if [[ -d temp ]]; then
		rm -rf temp
	fi
	mkdir -p temp
	cd temp

	for I in ${SOURCES}; do
		if [[ ${I:0:1} == "/" ]]; then
			SOURCE="${I}"
		else
			SOURCE="${SOURCE_DIR}/${I}"
		fi
		O=`basename ${SOURCE}`
		O=${O%.*}.o
		OFILES="${OFILES} ${O}"
		echo "Compiling ${SOURCE}..."
		${CROSS}-gcc ${QMAKE_CFLAGS} ${EXTRA_CFLAGS} -c "${SOURCE}" -o ${O}
	done

	TARGET="${SOURCE_DIR}/../build/${OSNAME}/${OSARCH}/${TARGET}${EXT}"
	echo "Linking ${TARGET}..."
	${CROSS}-g++ -o "${TARGET}" ${OFILES} ${EXTRA_OFILES} "-L${SOURCE_DIR}/../thirdparty-build/${OSNAME}/${OSARCH}/installed/lib" ${EXTRA_LD_FLAGS}

	echo "Compressing ${TARGET}..."
	#${CROSS}-strip "${TARGET}"
	#upx -9 "${TARGET}"

	popd
}

# 64 Bit Linux
echo -e "${G_L}\nLinux x86_64\n${G_L}"
CROSS="x86_64-linux-gnu"
EXTRA_CFLAGS="-O2"
EXTRA_OFILES=""
EXTRA_LD_FLAGS="-l:everything.a -lpthread -lXv -lX11 -lXext -lm -ldl -lrt"
doBuild Singe-Linux-x86_64 linux 64

# 64 Bit Windows
echo -e "${G_L}\nWindows x86_64\n${G_L}"
CROSS="x86_64-w64-mingw32"
EXTRA_CFLAGS="-O2"
EXTRA_OFILES="/tmp/singe.res"
EXTRA_LD_FLAGS="-mwindows -static -lmingw32 -l:everything.a -lm -lbcrypt -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid -Dmain=SDL_main"
xcf2png icon.xcf -o /tmp/icon.png
icotool -c -o /tmp/icon.ico /tmp/icon.png
x86_64-w64-mingw32-windres singe.rc -O coff -o /tmp/singe.res
doBuild Singe-Windows-x86_64 mingw 64 .exe
rm /tmp/icon.ico
rm /tmp/icon.png
rm /tmp/singe.res
#cp ../build/mingw/64/Singe-Windows-x86_64.exe ../test/.

# 32 Bit Raspbian
echo -e "${G_L}\nLinux armv6\n${G_L}"
SYSROOT="/opt/cross/pi/buster"
CROSS="/opt/cross/pi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf"
EXTRA_CFLAGS="-O2 --sysroot ${SYSROOT}"
EXTRA_OFILES=""
EXTRA_LD_FLAGS="--sysroot ${SYSROOT} -l:everything.a -Wl,-rpath-link,${SYSROOT}/opt/vc/lib -L${SYSROOT}/opt/vc/lib -lbcm_host -lasound -lpthread -lm -ldl -lsndio -lmmal_core -lmmal_util -lmmal_vc_client -lvdpau -ldrm -lgbm -lX11 -lsamplerate"
doBuild Singe-Pi-armv6 pi 32
