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

PATH=$PATH:$PWD/tools/download-tool

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}/
	echo Applying McCode patches to 3rdparty/${MODULE}
	cp -rp 3rdparty/patches/${MODULE}/* 3rdparty/${MODULE}/
    fi
done

WORK=`pwd`

# Workaround for having CRLF linefeeds in a couple of windows packages
rsync -avz --delete --exclude=".svn/" mcxtrace-comps/ mcxtrace-comps-win
find mcxtrace-comps-win -type f -not -name \*.off -exec unix2dos \{\} \;
s
# 64-bit
./mkdist mcxtrace $1 "" "" mingw64 "" -- nsis64
./mkdist mcxtrace-comps $1 mcxtrace-comps-win "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-matlab-mxplot $1 tools/matlab/mcplot/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mccodelib $1 tools/Python/mccodelib/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxplot-matplotlib $1 tools/Python/mcplot/matplotlib/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxplot-pyqtgraph $1 tools/Python/mcplot/pyqtgraph/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxrun $1 tools/Python/mcrun/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxtest $1 tools/Python/mctest/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxgui $1 tools/Python/mcgui/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxdisplay-pyqtgraph $1 tools/Python/mcdisplay/pyqtgraph/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxdisplay-matplotlib $1 tools/Python/mcdisplay/matplotlib/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-tools-python-mxdisplay-webgl $1 tools/Python/mcdisplay/webgl/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-manuals $1 docpkg/manuals/mcxtrace/ "" mingw64 "o" -- nsis64
./mkdist mcxtrace-tools-python-mxdoc $1 tools/Python/mcdoc/ "" mingw64 "" -- nsis64
./mkdist mcxtrace-mcpl $1 3rdparty/mcpl-package "" mingw64 "" -- nsis64

if [ "x$2" != "x" ]; then
    # Build the meta-packages also
    cd meta-pkgs/windows/Support
    if [  -f Miniforge3-Windows-x86_64.exe ]; then
	rm Miniforge3-Windows-x86_64.exe
    fi
    wget https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Windows-x86_64.exe
    cd ..

    rm -f python-install.bat.in.2 python-install.bat docupdate.bat.in.2 docupdate.bat
    
    sed s/@VERSION@/${1}/g python-install.bat.in > python-install.bat.in.2
    sed s/@FLAVOR@/mcxtrace/g python-install.bat.in.2 > python-install.bat
    sed s/@VERSION@/${1}/g docupdate.bat.in > docupdate.bat.in.2
    sed s/@P@/mx/g docupdate.bat.in.2 >docupdate.bat.in.3
    sed s/@FLAVOR@/mcxtrace/g docupdate.bat.in.3 > docupdate.bat

    sed s/@VERSION@/${1}/g McXtrace-metapackage64.iss > McXtrace-${1}-metapackage64.iss
    ./iscc McXtrace-${1}-metapackage64.iss
    mv Output/setup.exe dist/McXtrace-Metapackage-${1}-win64.exe
    cp $WORK/INSTALL-McXtrace-3.x/Windows/README.md dist/
    mkdir dist/single-packages
    mv dist/mcxtrace-* dist/single-packages
fi
