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

# rapid-build script for mcstas-kernel dev:


#git checkout main
#git pull

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

./3rdparty/sync.sh

WORK=`pwd`

export MCINSTALL_PREFIX=$HOME/McStas
export CONDALOCATION=$MCINSTALL_PREFIX/mcstas/3.x-dev/miniconda3

if [[ -d $HOME/McStas/mcstas/3.x-dev ]]
then
    cd $HOME/McStas/mcstas/3.x-dev
    ls | grep -v miniconda3 | xargs -n1 rm -rf 
    cd -
fi

if [ ! -f Miniforge3-$(uname)-$(uname -m).sh ]; then
    # Download and embed a miniconda
    echo
    echo "*********************************************************"
    echo "* Downloading a miniconda3 for your app                 *"
    echo "*********************************************************"
    echo
    sleep 3
    curl -L -O "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"
fi

if [[ !  -d $CONDALOCATION ]]; then
    echo "*********************************************************"
    echo "* Embedding miniconda3 in your app                      *"
    echo "*********************************************************"
    chmod a+x Miniforge3-$(uname)-$(uname -m).sh
    ./Miniforge3-$(uname)-$(uname -m).sh -b -p $CONDALOCATION
    # Run conda to install the dependencies
    echo
    echo "*********************************************************"
    echo "* Downloading python dependencies to embedded miniconda *"
    echo "*********************************************************"
    echo
    export PATH=$CONDALOCATION/bin:$PATH
    source $CONDALOCATION/bin/activate
    which conda
    mamba update mamba -y
    mamba install -c conda-forge --override-channels cmake compilers flex bison nodejs flex bison gsl pyaml ply matplotlib numpy tornado scipy pillow pyqtgraph pyqt nomkl qscintilla2 nexusformat nexpy hdf5 openmpi mcpl ncrystal nexus -y
    mamba clean --all -y
    echo CMAKE:
    which cmake
else
    source $CONDALOCATION/bin/activate
    which conda
    echo CMAKE:
    which cmake
fi


export PATH=$CONDALOCATION/bin:$PATH

./mkdist mcstas 3.x-dev "" "" mac "" -- justinst
./mkdist mcstas-comps 3.x-dev "" "" mac "" -- justinst
./mkdist mcstas-tools-python-mcrun 3.x-dev tools/Python/mcrun/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdoc 3.x-dev tools/Python/mcdoc/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mctest 3.x-dev tools/Python/mctest/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcgui 3.x-dev tools/Python/mcgui/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mccodelib 3.x-dev tools/Python/mccodelib/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-pyqtgraph 3.x-dev tools/Python/mcdisplay/pyqtgraph/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-webgl 3.x-dev tools/Python/mcdisplay/webgl/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-cad 3.x-dev tools/Python/mcdisplay/cad/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-pyqtgraph 3.x-dev tools/Python/mcplot/pyqtgraph/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcdisplay-mantid 3.x-dev tools/Python/mcdisplay/mantid_xml/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-svg 3.x-dev tools/Python/mcplot/svg/ "" mac "" -- justinst
./mkdist mcstas-tools-python-mcplot-matplotlib 3.x-dev tools/Python/mcplot/matplotlib/ "" mac "" -- justinst
cp tools/other/mcsplit/mcsplit.py $MCINSTALL_PREFIX/mcstas/3.x-dev/bin/

# Ensure we are configured for 10 node MPI runs
sed -i.bak s/\'4\'/\'10\'/g $HOME/McStas/mcstas/3.x-dev/tools/Python/mccodelib/mccode_config.py
$HOME/McStas/mcstas/3.x-dev/bin/mcdoc -i
