#!/bin/bash
shopt -s extglob

# Variables
SCRIPTNAME=$(basename $0)
CLEANUP=FALSE
BUILD_EXAMPLES=FALSE
BUILDDIR=${PWD}
SCRIPTDIR=$( dirname "${BASH_SOURCE[0]}" )
SRCDIR=$( dirname ${PWD} )

# Help
Help()
{
   # Display Help
   echo "Helper script to build Emu68 with docker"
   echo
   echo "Syntax: ${SCRIPTNAME} [-t|v|c|b|h]"
   echo "Options:"
   printf -- "-t\t\tSets which Emu68 target to build (raspi|raspi64|virt).\n"
   printf -- "-v\t\tSets which Emu68 variant to build (none|pistorm).\n"
   printf -- "-c\t\tCleans build directory before building.\n"
   printf -- "-b\t\tBuilds example programs to run in Emu68.\n"
   printf -- "-h\t\tPrint this Help.\n"
 
   echo
}

# Get the options
while getopts ":t:v:cbh?" option; do
    case $option in
        t) # target
            TARGET=$OPTARG;;
        v) # variant
            VARIANT=$OPTARG;;
        c) # cleanup
            CLEANUP=TRUE;;
        b) # build examples
            BUILD_EXAMPLES=TRUE;;
        h) # display Help
            Help
            exit;;
        \?) # Invalid option
            echo "Error: Invalid option"
            echo "---------------------"
            Help
            exit;;
    esac
done

if [[ "${CLEANUP}" == "TRUE" ]]; then
    # Clean build directory
	rm -rv !("build-aarch64"|"build-with-docker");
fi

if [[ ! -f "build.ninja" ]]; then
    # Generate build files
    ${SCRIPTDIR}/build-aarch64 cmake .. -GNinja $(if [[ ! -z "${TARGET}" ]]; then echo "-DTARGET=${TARGET}"; fi) $(if [[ ! -z "${VARIANT}" ]]; then echo "-DVARIANT=${VARIANT}"; fi)
fi

# Build Emu68
${SCRIPTDIR}/build-aarch64 cmake --build . -- -j`nproc`

if [[ "${BUILD_EXAMPLES}" == "TRUE" ]]; then
    # Build examples
    mkdir -p ${SRCDIR}/Build && cd ${SRCDIR}/examples && ${SCRIPTDIR}/build-m68k-amigaos make && cp -v ${SRCDIR}/Build/* ${BUILDDIR}/
fi

cd ${BUILDDIR}
