#!/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

# 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`

# 64-bit
./mkdist mcstas $1 "" "" rpm64-fedora "" -- rpm
./mkdist mcstas-comps $1 "" "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-matlab-mcplot $1 tools/matlab/mcplot/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-matlab-mcdisplay $1 tools/matlab/mcdisplay/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mccodelib $1 tools/Python/mccodelib/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcplot-pyqtgraph $1 tools/Python/mcplot/pyqtgraph/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcplot-matplotlib $1 tools/Python/mcplot/matplotlib/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcplot-html $1 tools/Python/mcplot/html/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcresplot $1 tools/Python/mcresplot/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcrun $1 tools/Python/mcrun/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcgui $1 tools/Python/mcgui/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcdisplay-webgl $1 tools/Python/mcdisplay/webgl/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcdisplay-pyqtgraph $1 tools/Python/mcdisplay/pyqtgraph/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcdisplay-mantid $1 tools/Python/mcdisplay/mantid_xml/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcdisplay-cad $1 tools/Python/mcdisplay/cad/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-clusterscripts $1 tools/cluster-scripts/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-manuals $1 docpkg/manuals/mcstas/ "" rpm64-fedora "" -- rpm
./mkdist mcstas-tools-python-mcdoc $1 tools/Python/mcdoc/ "" rpm64-fedora "" -- rpm

cd 3rdparty/mcpl
rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DMCPL_ENABLE_CPACK=ON
make
cpack -G RPM .
RPM=`ls *.rpm`
RPMNAME=`echo $RPM | sed s/Linux/rpm64-fedora/g`
mv $RPM $WORK/dist/$RPMNAME

cd $WORK

cd 3rdparty/ncrystal
rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DNCRYSTAL_ENABLE_CPACK=ON
make
cpack -G RPM .
RPM=`ls *.rpm`
RPMNAME=`echo $RPM | sed s/Linux/rpm64-fedora/g`
mv $RPM $WORK/dist/$RPMNAME

cd $WORK

#build metapackages?
if [ "x$2" != "x" ]; then
  cd meta-pkgs/rpm

  for controlfile in `ls mcstas-suite-python.spec`
  do
    sed -i.bak s/@VERSION@/${1}/g $controlfile
    rpmbuild -bb --target x86_64 $controlfile
    mv $controlfile.bak $controlfile
  done
  mv $HOME/rpmbuild/RPMS/x86_64/mcstas-suite-*rpm $WORK/dist
  cp $WORK/INSTALL-McStas/Linux/fedora/README.md $WORK/dist
fi
