#!/bin/bash

# aqt install-qt linux desktop 6.5.0 wasm_multithread --autodesktop

argc=$# # number of arguments passed (not counting the command itself)
argv=("$@") # list of arguments passed (not including the command itself)

function cleanAbsPath()
{
    local CLEAN_ABS_PATH="$( #spawns a new bash interpreter
        cd "$1" >/dev/null 2>&1 #change directory to that folder
        pwd -P
    )"
    echo "$CLEAN_ABS_PATH"
}

ECHO_FLAG=""
EMSDK_VERSION="4.0.7"
SAVE_LOG="true"
SHOW_HELP="false"

SCRIPT_PATH="$(cleanAbsPath "$(dirname "$0")")"
PROJECT_BASE_PATH="$(cleanAbsPath "$SCRIPT_PATH/..")"

NUM_PARALLEL_PROC=1


if [ "$(uname)" == "Darwin" ]; then
    NUM_PARALLEL_PROC=$(sysctl -n hw.logicalcpu)
else
    NUM_PARALLEL_PROC=$(expr $(nproc --all))
    ECHO_FLAG="-e"
fi

## Stop at first error
set -e

for (( j=0; j<argc; j++)); do
    if [ "${argv[j]}" == "--emsdk" ] || [ "${argv[j]}" == "-e" ]; then
        EMSDK_VERSION="${argv[j+1]}" 
		j=$j+1
	elif [ "${argv[j]}" == "--no-log" ]; then
		SAVE_LOG="false"
	elif [ "${argv[j]}" == "--qt" ] || [ "${argv[j]}" == "-q" ]; then
	        export Qt6_DIR="${argv[j+1]}"
	        j=$j+1
	elif [ "${argv[j]}" == "--qt-host" ]; then
	        export QT_HOST_PATH="${argv[j+1]}"
	        j=$j+1
	elif [ "${argv[j]}" == "--help" ] || [ "${argv[j]}" == "-h" ]; then
		SHOW_HELP="true"
	fi
done

if [ ${SHOW_HELP} == "true" ]; then
	echo "MNE-CPP wasm build script"
	echo ""
	echo "  This script is meant to be used with Qt version 6.5 and up."
	echo ""
	echo "Usage:"
	echo "    wasm --emsdk <VERSION>"
	echo ""
	echo "Depends on the enviroment variable Qt6_DIR being set and having wasm qt installed."
	echo "For more info, go to https://doc.qt.io/qt-6/wasm.html"
	echo ""
	echo "Options:"
	echo "    --emsdk, -e  Set what version of emscripten to use."
	echo "                 Qt recommends the following versions depending on what version"
	echo "                 of Qt you use:"
	echo "                    Qt 6.2 -> emsdk 2.0.14"
	echo "                    Qt 6.3 -> emsdk 3.0.0"
	echo "                    Qt 6.4 -> emsdk 3.1.14"
	echo "                    Qt 6.5 -> emsdk 3.1.25"
	echo ""
	echo "    --qt, -q     Set path to qt installation manually."
	echo "                 This path should be the directory that contains the wasm and"
        echo "                 system qt folders (for example, in linux, the dir should"
        echo "                 contain gcc_64 and wasm_multithread). Must be an absolute path."
	echo ""
  	echo "    --no-log     Omits build log."
	echo ""
	echo "    --help, -h   Prints this help text."

	exit 0
fi

# Make sure we run everything relative to root folder as pwd
cd ${PROJECT_BASE_PATH}

if [ -z "$Qt6_DIR" ]; then
        echo ${ECHO_FLAG} "\033[31m[ERROR]\033[0m Qt6_DIR not set."
        echo "Please set Qt6_DIR as an enviroment variable or pass it as an argument when calling with script using '--qt <DIR_PATH>"
        exit 1
fi

## Emscripten
echo "Setting up emsdk ${EMSDK_VERSION}..."
if [ -d "emsdk" ]; then
	echo "Found emsdk folder."
else
	echo "emsdk folder not found. Fetching repo..."
	git clone https://github.com/emscripten-core/emsdk.git
fi

cd emsdk
git pull
./emsdk install ${EMSDK_VERSION}
./emsdk activate ${EMSDK_VERSION}
source ./emsdk_env.sh
cd ..
echo "emsdk set up."

## Build project
SOURCE_DIRECTORY=${PROJECT_BASE_PATH}/src
BUILD_DIRECTORY=${PROJECT_BASE_PATH}/build/wasm
OUTPUT_DIRECTORY=${PROJECT_BASE_PATH}/out/wasm

# Determine host Qt path for cross-compilation
if [ -n "${QT_HOST_PATH}" ] && [ -d "${QT_HOST_PATH}" ]; then
    echo "Using QT_HOST_PATH from environment: ${QT_HOST_PATH}"
elif [ -d "${Qt6_DIR}/gcc_64" ]; then
    QT_HOST_PATH="${Qt6_DIR}/gcc_64"
elif [ -d "${Qt6_DIR}/macos" ]; then
    QT_HOST_PATH="${Qt6_DIR}/macos"
else
    echo "ERROR: No host Qt found. Set QT_HOST_PATH or install a desktop Qt alongside the WASM Qt."
    exit 1
fi

${Qt6_DIR}/wasm_multithread/bin/qt-cmake \
        -DQT_HOST_PATH=${QT_HOST_PATH} \
	-DCMAKE_BUILD_TYPE=Release \
	-DBINARY_OUTPUT_DIRECTORY=${OUTPUT_DIRECTORY} \
	-S ${SOURCE_DIRECTORY} \
	-B ${BUILD_DIRECTORY} \
	-DWASM=ON

cmake \
	--build ${BUILD_DIRECTORY} \
	--parallel ${NUM_PARALLEL_PROC}

## Post-build: inject COOP/COEP service worker for cross-origin isolation
# Required for SharedArrayBuffer / multi-threaded Wasm in Chrome & Safari
POSTBUILD_SCRIPT="${SCRIPT_PATH}/wasm/wasm_postbuild.sh"
if [ -x "$POSTBUILD_SCRIPT" ]; then
	if [ -d "${OUTPUT_DIRECTORY}/apps" ]; then
		"$POSTBUILD_SCRIPT" "${OUTPUT_DIRECTORY}/apps"
	fi
	if [ -d "${OUTPUT_DIRECTORY}/examples" ]; then
		"$POSTBUILD_SCRIPT" "${OUTPUT_DIRECTORY}/examples"
	fi
fi

## Log
if [ ${SAVE_LOG} == "true" ]; then
	echo "Saving build and system parameters..."
	touch build_info.txt
	echo "System Info: $(uname -rvop)" >> build_info.txt
	echo "emsdk version: ${EMSDK_VERSION}" >> build_info.txt
	echo "Qt version: ${QT_VERSION}" >> build_info.txt
	echo "Build time: $(date)" >> build_info.txt
	echo "Build git hash: ${GIT_HASH}" >> build_info.txt
	echo "Saved."
fi

exit 0
