# #!/usr/bin/env bash
export CMAKE_BUILD_PARALLEL_LEVEL=${CMAKE_BUILD_PARALLEL_LEVEL:-$(python3 -c "import os;print(os.cpu_count())")}

if [ "x$1" = "x" ]; then
    # No arguments
    echo Please provide one argument,e.g : $0 2.0
    exit 1;
fi

export MCVERSION=${1}


./3rdparty/sync.sh

# This is an app bundle being built, but non-relocatable.
export MCINSTALL_PREFIX=/Applications/McXtrace-${MCVERSION}.app/
./mkdist mcxtrace-app ${MCVERSION} support/MacOSX/McCode.app/ "" mac-silicon "" -- OSXapp

# For the actual mcxtrace part, the packages need a 'deep' PREFIX
export MCINSTALL_PREFIX=/Applications/McXtrace-${MCVERSION}.app/Contents/Resources/
# 64-bit Mac OS
WORK=`pwd`


# Run conda to install the dependencies
echo
echo "*********************************************************"
echo "* Downloading python dependencies to embedded miniconda *"
echo "*********************************************************"
echo

if [ -e Miniforge3-$(uname)-$(uname -m).sh ]; then
  rm -f Miniforge3-$(uname)-$(uname -m).sh
fi
curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
chmod a+x Miniforge3-$(uname)-$(uname -m).sh
./Miniforge3-$(uname)-$(uname -m).sh -p ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3/ -b
eval "$(${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3/bin/conda shell.bash hook)"

CONDA_NO_PLUGINS=true mamba env update --name base --file environment.yml

cd $WORK

./mkdist mcxtrace ${MCVERSION} "" "" mac "" -- OSXapp
./mkdist mcxtrace-comps ${MCVERSION} "" "" mac "" -- OSXapp
./mkdist mcxtrace-tools-matlab-mxplot ${MCVERSION} tools/matlab/mcplot/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-matlab-mxdisplay ${MCVERSION} tools/matlab/mcdisplay/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxplot-pyqtgraph ${MCVERSION} tools/Python/mcplot/pyqtgraph/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxplot-matplotlib ${MCVERSION} tools/Python/mcplot/matplotlib/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxplot-html ${MCVERSION} tools/Python/mcplot/html/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxrun ${MCVERSION} tools/Python/mcrun/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mctest ${MCVERSION} tools/Python/mctest/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mcresplot ${MCVERSION} tools/Python/mcresplot/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mcgui ${MCVERSION} tools/Python/mcgui/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mccodelib ${MCVERSION} tools/Python/mccodelib/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxdisplay-webgl ${MCVERSION} tools/Python/mcdisplay/webgl/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxdisplay-pyqtgraph ${MCVERSION} tools/Python/mcdisplay/pyqtgraph/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxdisplay-matplotlib ${MCVERSION} tools/Python/mcdisplay/matplotlib/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxdisplay-cad ${MCVERSION} tools/Python/mcdisplay/cad/ "" mac "" -- OSXapp
./mkdist mcxtrace-tools-python-mxdoc ${MCVERSION} tools/Python/mcdoc/ "" mac "" -- OSXapp
./mkdist mcxtrace-clusterscripts ${MCVERSION} tools/cluster-scripts/ "" mac "" -- OSXapp
./mkdist mcxtrace-manuals ${MCVERSION} docpkg/manuals/mcxtrace/ "" mac "" -- OSXapp


# Copy in the mpicc snippets
cp support/MacOSX/mpicc-wrappers/* $MCINSTALL_PREFIX/mcxtrace/${MCVERSION}/bin

export MCINSTALL_PREFIX=/Applications/McXtrace-${MCVERSION}.app/Contents/Resources/

cd $WORK
MXDOC=`find $MCINSTALL_PREFIX -name mxdoc.py`
# Generate mcdoc pages
python ${MXDOC} -i

# Hack to make openmpi from the conda env available on Big Sur and later
ln -s $MCINSTALL_PREFIX/mcxtrace/${MCVERSION}/miniconda3/bin/mpi* $MCINSTALL_PREFIX/mcxtrace/${MCVERSION}/bin/

mkdir -p dist
# Move the App bundle back to the dist - or keep it in /Applications?
if [ "x$2" = "x" ]; then
    mv /Applications/McXtrace-${MCVERSION}.app dist
    ln -s /Applications dist/
    cd dist
    ln -s McXtrace-${MCVERSION}.app/Contents/Resources/mcxtrace/${MCVERSION}/mcxtrace-environment McXtrace-${MCVERSION}-environment.command
    cd ..
    cp support/MacOSX/Check-dependencies/* dist
    mv dist mcxtrace-${MCVERSION}
    mkdir dist
    tar cfz dist/mcxtrace-${MCVERSION}_arm64.tgz mcxtrace-${MCVERSION}
    cp INSTALL-McXtrace/macOS/README.md dist
    echo A tgz of your app is now located in the dist folder
    rm -rf mcxtrace-${MCVERSION}
else
    ln -s /Applications/McXtrace-${MCVERSION}.app dist/
    echo Your app is now located in /Applications \(link available in the dist folder\)
fi



