# #!/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
conda env create -p ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3 --file environment.yml

eval "$(conda shell.bash hook)"
conda activate ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3

# Temporary workaround for issue with conda...
if [ ! -f ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3/lib/librhash.0.dylib ]; then
    ln -sf ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3/lib/librhash.1.dylib ${MCINSTALL_PREFIX}/mcxtrace/${MCVERSION}/miniconda3/lib/librhash.0.dylib
fi

cd $WORK

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

cd 3rdparty/nexus-code
if [ -d build ]; then
    rm -rf build
fi
mkdir -p build; cd build
cmake -DCMAKE_INSTALL_PREFIX=/Applications/McXtrace-${MCVERSION}.app/Contents/Resources/mcxtrace/${MCVERSION}/ -DHDF5_ROOT=/Applications/McXtrace-${MCVERSION}.app/Contents/Resources/mcxtrace/${MCVERSION}/miniconda3/ -DCMAKE_MACOSX_RPATH=ON -DCMAKE_INSTALL_RPATH=/Applications/McXtrace-${MCVERSION}.app/Contents/Resources/mcxtrace/${MCVERSION}/lib  ..
make install
cd $WORK

sed -i.bak s+GETPATH\(+GETPATH\(miniconda3/+g /Applications/McXtrace-${MCVERSION}.app/Contents/Resources/mcxtrace/${MCVERSION}/samples/Fluorescence.comp

# 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-3.x/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



