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

export WORK=`pwd`
MCCODE_ARCH=deb32
P=mx
FLAVOUR=mcxtrace

# From 2.1 onwards, let McStas reside in /usr/share on Debian-like systems
export MCINSTALL_PREFIX=/usr              # Debian standard /usr/bin and /usr/share/$FLAVOUR
export MCCODE_USE_LEGACY_DESTINATIONS=OFF # install in e.g. /usr/share/$FLAVOUR/resources
export ENABLE_CIF2HKL=OFF                 # cif2hkl is available as a separate package

./mkdist $FLAVOUR                                         $1 "" "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-comps                                   $1 "" "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-matlab-${P}plot                   $1 tools/matlab/mcplot/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-matlab-${P}display                $1 tools/matlab/mcdisplay/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-mccodelib                  $1 tools/Python/mccodelib/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}plot-pyqtgraph         $1 tools/Python/mcplot/pyqtgraph/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}plot-matplotlib        $1 tools/Python/mcplot/matplotlib/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}plot-html               $1 tools/Python/mcplot/html/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}run                    $1 tools/Python/mcrun/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}test                   $1 tools/Python/mctest/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}gui                    $1 tools/Python/mcgui/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}display-webgl          $1 tools/Python/mcdisplay/webgl/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}display-webgl-classic  $1 tools/Python/mcdisplay/webgl-classic/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}display-pyqtgraph      $1 tools/Python/mcdisplay/pyqtgraph/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}display-matplotlib     $1 tools/Python/mcdisplay/matplotlib/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}display-cad            $1 tools/Python/mcdisplay/cad/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-clusterscripts                          $1 tools/cluster-scripts/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-manuals                                 $1 docpkg/manuals/$FLAVOUR/ "" $MCCODE_ARCH "" -- deb
./mkdist $FLAVOUR-tools-python-${P}doc                    $1 tools/Python/mcdoc/ "" $MCCODE_ARCH "" -- deb

cd 3rdparty/mcpl
rm -rf build
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/usr -DMCPL_ENABLE_CPACK=ON
make
cpack -G DEB .
DEB=`ls *.deb`
DEBNAME=`echo $DEB | sed s/Linux/$MCCODE_ARCH/g`
mv $DEB $WORK/dist/$DEBNAME

cd $WORK

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

  for controlfile in `ls control.$FLAVOUR*`
  do
    sed -i.bak s/@VERSION@/${1}/g $controlfile
    equivs-build $controlfile
    mv $controlfile.bak $controlfile
  done

  echo DEB metapackages done
  
  echo WORK dir is $WORK
  echo mv ${WORK}/meta-pkgs/deb/$FLAVOUR-suite*${1}*.deb ${WORK}/dist
  echo cp ${WORK}/INSTALL-McXtrace/Linux/debian/README.md ${WORK}/dist
  mv ${WORK}/meta-pkgs/deb/$FLAVOUR-suite*${1}*.deb ${WORK}/dist
  cp ${WORK}/INSTALL-McXtrace/Linux/debian/README.md ${WORK}/dist
fi
