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

MCVERSION=`conda search mcstas |tail -1 | cut -b 31-41 |sed s/\ //g`

# This is an app bundle being built, but non-relocatable.

export MCINSTALL_PREFIX=/Applications/McStas-${MCVERSION}.app/

mkdir -p  support/MacOSX/McCode.app/tmp
cd support/MacOSX/McCode.app/tmp
rm -rf *
cmake .. -DBUILD_MCSTAS=ON -DCONDA_EMBED=ON -DMCVERSION=${MCVERSION} -DCMAKE_INSTALL_PREFIX=$MCINSTALL_PREFIX
make install

cd -
#./mkdist mcstas-app ${MCVERSION} support/MacOSX/McCode.app/ "" mac-silicon "" -- OSXapp

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

conda env create -p ${MCINSTALL_PREFIX}/Contents/Resources/mcstas/${MCVERSION}/miniconda3 --file mcstas-environment.yml

eval "$(${MCINSTALL_PREFIX}/Contents/Resources/mcstas/${MCVERSION}/miniconda3/bin/conda shell.bash hook)"
mcdoc -i
