#!/usr/bin/env bash
export PATH=$HOME/McStas/mcstas/2.x-dev/miniconda3/bin:$PATH
export CMAKE_BUILD_PARALLEL_LEVEL=${CMAKE_BUILD_PARALLEL_LEVEL:-$(python3 -c "import os;print(os.cpu_count())")}

# rappid-build script for mcstas-kernel dev:

git checkout mccode-legacy
git pull

# Ensure our 3rd party modules are in place and updated
THIRDPARTY=`ls 3rdparty | grep -v patches`
cd 3rdparty

cd -

./3rdparty/sync.sh  


for MODULE in `echo $THIRDPARTY`
do
    if [ -d "3rdparty/patches/${MODULE}" ]; then
	echo Making McCode cmake modules available for $MODULE
        rsync -avz cmake/ 3rdparty/${MODULE}/cmake
	echo Applying McCode patches to 3rdparty/${MODULE}
	cp -rp 3rdparty/patches/${MODULE}/* 3rdparty/${MODULE}/
    fi
done

WORK=`pwd`

export MCINSTALL_PREFIX=$HOME/McStas
export CC=gcc
export FC=gfortran

./mkdist mcstas 2.x-dev "" "" deb64 "" -- justinst
./mkdist mcstas-comps 2.x-dev "" "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcrun 2.x-dev tools/Python/mcrun/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcdoc 2.x-dev tools/Python/mcdoc/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mctest 2.x-dev tools/Python/mctest/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcgui 2.x-dev tools/Python/mcgui/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mccodelib 2.x-dev tools/Python/mccodelib/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-pyqtgraph 2.x-dev tools/Python/mcdisplay/pyqtgraph/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-webgl 2.x-dev tools/Python/mcdisplay/webgl/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcplot-pyqtgraph 2.x-dev tools/Python/mcplot/pyqtgraph/ "" deb64 "" -- justinst
./mkdist mcstas-tools-python-mcplot-matplotlib 2.x-dev tools/Python/mcplot/matplotlib/ "" deb64 "" -- justinst

export MCINSTALL_PREFIX=$HOME/McStas/mcstas/2.x-dev/
./mkdist mcstas-ncrystal 2.x-dev 3rdparty/ncrystal-package "" deb64 "" -- justinst
./mkdist mcstas-mcpl 2.x-dev 3rdparty/mcpl-package "" deb64 "" -- justinst
export MCINSTALL_PREFIX=$HOME/McStas

# Ensure we are configured for 10 node MPI runs
sed -i s/\'4\'/\'10\'/g $MCINSTALL_PREFIX/mcstas/2.x-dev/tools/Python/mccodelib/mccode_config.py



