#!/usr/bin/env bash

# rappid-build script for mcstas-kernel dev:

# 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

#git checkout mcstas-3.x
#git pull

WORK=`pwd`

export MCINSTALL_PREFIX=$HOME/McStas

if [[ -d $HOME/McStas/mcstas/2.x-dev ]]
then
    rm -rf $HOME/McStas/mcstas/2.x-dev/*
fi
./mkdist mcstas 2.x-dev "" "" mac "" -- justinst
./mkdist mcstas-comps 2.x-dev "" "" mac "" -- justinst
./mkdist mcstas-tools-python-mcrun 2.x-dev tools/Python/mcrun/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcgui 2.x-dev tools/Python/mcgui/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mccodelib 2.x-dev tools/Python/mccodelib/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-pyqtgraph 2.x-dev tools/Python/mcdisplay/pyqtgraph/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-pyqtgraph 2.x-dev tools/Python/mcplot/pyqtgraph/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-svg 2.x-dev tools/Python/mcplot/svg/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-matplotlib 2.x-dev tools/Python/mcplot/matplotlib/ "" mac "" -- justinst
cp tools/other/mcsplit/mcsplit.py $MCINSTALL_PREFIX/mcstas/2.x-dev/bin/

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


# Ensure we are configured for 10 node MPI runs
sed -i.bak s/\'4\'/\'10\'/g $HOME/McStas/mcstas/2.x-dev/tools/Python/mccodelib/mccode_config.json
x
ln -sf  /Applications/McStas-2.7.1.app/Contents/Resources/mcstas/2.7.1/miniconda3 $MCINSTALL_PREFIX/miniconda3
