#!/usr/bin/env bash

BUILD="x86"
BUILD_TYPE="release"
BUILD_WINDOWS="no"
OPENGL_DEFINE=""
OPENGL_LIBS="-lGL"
ANDROID_INSTALL="no"
CPP_LTO="-flto"

OS=$(uname)

function compile_x86
{
	CPU_FLAGS='-mno-sse2 -mno-sse -mno-mmx'
	COMMON_FLAGS='-Wall -m32 -std=gnu89'

	echo -n "Building"
	if [[ $BUILD_TYPE == "debug" ]]; then
		echo -n " debug"
		WIN_SUBSYSTEM='console'
		DEBUG_ASM='-g dwarf2'
		C_FLAGS="$COMMON_FLAGS -g $CPU_FLAGS"
	else
		echo -n " release"
		WIN_SUBSYSTEM='windows'
		C_FLAGS="$COMMON_FLAGS -O2 $CPU_FLAGS"
		STRIP='-s'
	fi

	rm -f *.o

	echo -n " for"
	if [[ $BUILD_WINDOWS == "yes" ]]; then
		echo -n " Windows... "
		i686-w64-mingw32-windres Windows/nfs2se.rc icon.o &&
		i686-w64-mingw32-gcc $C_FLAGS $OPENGL_DEFINE -c *.c &&
		yasm -f win32 Asm/NFS2SE.asm -o NFS2SE.Win32.o $DEBUG_ASM --prefix=_ &&
		i686-w64-mingw32-ld --enable-stdcall-fixup -o "../Need For Speed II SE/nfs2se.exe" *.o --stack=0x7D00,0x7D00 --heap=0x2000,0x1000 -lws2_32 -lwinmm -lmingwex -lmsvcrt -lkernel32 -lopengl32 -lSDL2 -subsystem=$WIN_SUBSYSTEM $STRIP -e _start &&
		rm -f *.o &&
		echo "OK!"
	else
		if [ $OS == "Darwin" ]; then
			echo -n " macOS... "
			clang $C_FLAGS $OPENGL_DEFINE -DSTACK_REALIGN -DSWAP_WINDOW_AND_GL_THREAD -c *.c -mmacosx-version-min=10.6 &&
			yasm -f macho Asm/NFS2SE.asm -o NFS2SE.OSX.o --prefix=_ -DSWAP_WINDOW_AND_GL_THREAD &&
			ld -o "../Need For Speed II SE/nfs2se" *.o -lc -lSDL2 -framework OpenGL -macosx_version_min 10.6 -e _start &&
			rm -f *.o &&
			echo "OK!"
		else
			if [ -z $CC ]; then
				CC=gcc
			fi
			echo -n " $OS ($CC)... "
			yasm -f elf32 Asm/NFS2SE.asm -o NFS2SE.Elf32.o $DEBUG_ASM &&
			$CC -no-pie -nostartfiles $C_FLAGS -DSTACK_REALIGN $OPENGL_DEFINE -o "../Need For Speed II SE/nfs2se" NFS2SE.Elf32.o *.c -lSDL2 $OPENGL_LIBS $STRIP -Wl,-rpath=\$ORIGIN -Wl,-estart &&
			rm -f NFS2SE.Elf32.o &&
			echo "OK!"
		fi
	fi
}

function compile_cpp
{
	if [ $(uname -m) == "x86_64" ]; then
		CPU_FLAGS="-m32"
	fi

	if [[ $BUILD_TYPE == "debug" ]]; then
		FLAGS="-O0 -g"
		CPP_LTO=""
		echo "Debug"
	else
		FLAGS="-O3 -DNDEBUG"
		echo "Release"
	fi

	echo -n "Compiling C++ code... "
	clang -c -std=c++14 $CPU_FLAGS $CPP_LTO $FLAGS -Wno-c++11-narrowing -Wextra Cpp/NFS2SE.cpp -fno-rtti -fno-exceptions || exit
	echo "OK!"

	echo -n "Compiling C code... "
	clang -c -std=gnu89 -Wno-incompatible-function-pointer-types -Wno-int-conversion -Wall $CPU_FLAGS $FLAGS $OPENGL_DEFINE -DNFS_CPP *.c || exit
	echo "OK!"

	echo -n "Linking... "
	clang -fuse-ld=lld -o "../Need For Speed II SE/nfs2se" $CPU_FLAGS $CPP_LTO *.o -lSDL2 $OPENGL_LIBS -lm || exit
	echo "OK!"

	if [[ $BUILD_TYPE != "debug" ]]; then
		echo -n "Stripping... "
		strip "../Need For Speed II SE/nfs2se" || exit
		echo "OK!"
	fi

	rm -f *.o
}

while (( $# > 0 ))
do
	key=$1
	case $key in
		cpp|android)
			BUILD=$key
		;;
		debug)
			BUILD_TYPE="debug"
		;;
		win32)
			BUILD_WINDOWS="yes"
		;;
		gl1)
			OPENGL_DEFINE="-DOPENGL1X"
		;;
		gles2)
			OPENGL_DEFINE="-DGLES2"
			OPENGL_LIBS="-lGLESv2"
		;;
		install)
			ANDROID_INSTALL="yes"
		;;
	esac
	shift
done

cd src || exit

if [[ $BUILD != "android" ]]; then
	if [[ $OPENGL_DEFINE == "-DOPENGL1X" ]]; then
		echo "Building OpenGL 1"
	elif [[ $OPENGL_DEFINE == "-DGLES2" ]]; then
		if [[ $OS == "Darwin" ]]; then
			echo "OpenGL|ES 2 is not supported on macOS"
			exit 1
		fi
		if [[ $BUILD_WINDOWS == "yes" ]]; then
			echo "OpenGL|ES 2 is not supported on Windows"
			exit 1
		fi
		echo "Building OpenGL|ES 2"
	else
		echo "Building OpenGL 2"
	fi
fi

if [[ $BUILD == "cpp" ]]; then
	if [[ $OS == "Darwin" ]]; then
		echo "C++ translation is not supported on macOS"
		exit 1
	fi
	if [[ $BUILD_WINDOWS == "yes" ]]; then
		echo "C++ translation is not supported on Windows"
		exit 1
	fi
	compile_cpp $@
elif [[ $BUILD == "android" ]]; then
	VERSION=$(grep WRAPPER_VERSION "Version" | cut -c26-30 | grep "\.") || exit
	cd Android || exit
	echo "Building OpenGL|ES 2 for Android, wrapper version: $VERSION"
	sed "s/@VERSION@/$VERSION/" app/src/main/AndroidManifest.xml.in > app/src/main/AndroidManifest.xml || exit
	rm -f app/build/outputs/apk/debug/app-debug.apk || exit
	./gradlew packageDebug || exit # packageRelease if you want to sign it
	if [[ $ANDROID_INSTALL == "yes" ]]; then
		adb install -r app/build/outputs/apk/debug/app-debug.apk || exit
		sleep 0.25
		adb shell am start -n org.zaps166.NFSIISE/org.zaps166.NFSIISE.NFSIISE
	fi
else
	compile_x86 $@
fi
