diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000000000000000000000000000000000000..45ee85bd87be3132b065876aa68c20edcd2458e3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*.log
+build_lofar.sh.config
diff --git a/DP3-buildfix.patch b/DP3-buildfix.patch
index 99c39dfb0ec9362f4318fba220a004e79a59551c..290b428b70aecf8b6a16b82a9f8065d70bc441d7 100644
--- a/DP3-buildfix.patch
+++ b/DP3-buildfix.patch
@@ -1,35 +1,135 @@
+diff --git a/CMake/FindCasacore.cmake b/CMake/FindCasacore.cmake
+index e4689f7..9068fdd 100644
+--- a/CMake/FindCasacore.cmake
++++ b/CMake/FindCasacore.cmake
+@@ -17,6 +17,8 @@
+ #
+ # Variables used by this module:
+ #  CASACORE_ROOT_DIR         - Casacore root directory. 
++#  BLAS_LIBS                 - override BLAS library
++#  LAPACK_LIBS               - override LAPACK library
+ #
+ # Variables defined by this module:
+ #  CASACORE_FOUND            - System has Casacore, which means that the
+@@ -232,7 +234,21 @@ else(NOT CASACORE_INCLUDE_DIR)
+     elseif(${_comp} STREQUAL fits)
+       casacore_find_package(CFITSIO REQUIRED)
+     elseif(${_comp} STREQUAL scimath_f)
+-      casacore_find_package(LAPACK REQUIRED)
++      # If only looking for LAPACK, no library will be added if LAPACK
++      # is part of BLAS, as it is customary nowadays. So look for both
++      # to avoid confusing linker errors on symbols used in headers/templates.
++      if(DEFINED ENV{BLAS_LIBS})
++        set(BLAS_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{BLAS_LIBS})
++      else()
++        casacore_find_package(BLAS   REQUIRED)
++      endif()
++      if(DEFINED ENV{LAPACK_LIBS})
++        set(LAPACK_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{LAPACK_LIBS})
++      else()
++        casacore_find_package(LAPACK REQUIRED)
++      endif()
+     endif(${_comp} STREQUAL casa)
+   endforeach(_comp ${_find_components})
+ endif(NOT CASACORE_INCLUDE_DIR)
+diff --git a/CMake/PythonInstall.cmake b/CMake/PythonInstall.cmake
+index 173f995..c15bbab 100644
+--- a/CMake/PythonInstall.cmake
++++ b/CMake/PythonInstall.cmake
+@@ -24,10 +24,11 @@
+ # $Id: PythonInstall.cmake 32905 2015-11-17 15:31:54Z schaap $
+ 
+ # Search for the Python interpreter.
+-find_package(PythonInterp)
++# No. We expect the toplevel CMakeLists.txt to have found python already!
++#find_package(PythonInterp)
+ 
+ # Derive the Python site-packages installation directory and build directory.
+-if(PYTHON_EXECUTABLE)
++if(Python_EXECUTABLE)
+   set(_cmd
+     "from distutils.sysconfig import get_python_lib"
+     "from os.path import join"
+@@ -36,7 +37,7 @@ if(PYTHON_EXECUTABLE)
+        'site-packages'))"
+   )
+   execute_process(
+-    COMMAND "${PYTHON_EXECUTABLE}" "-c" "${_cmd}"
++    COMMAND "${Python_EXECUTABLE}" "-c" "${_cmd}"
+     OUTPUT_VARIABLE _pydir
+     ERROR_VARIABLE _pyerr
+     OUTPUT_STRIP_TRAILING_WHITESPACE)
+@@ -57,7 +58,7 @@ if(PYTHON_EXECUTABLE)
+     message(STATUS "Build directory for Python extensions:        ${PYTHON_BUILD_DIR}")
+     message(STATUS "Installation directory for Python extensions: ${PYTHON_INSTALL_DIR}")
+   endif()
+-endif(PYTHON_EXECUTABLE)
++endif(Python_EXECUTABLE)
+ 
+ 
+ #
+@@ -66,9 +67,9 @@ endif(PYTHON_EXECUTABLE)
+ macro(python_install)
+ 
+   # Precondition check.
+-  if(NOT PYTHON_EXECUTABLE)
++  if(NOT Python_EXECUTABLE)
+     message(FATAL_ERROR "python_install: Python interpreter not available")
+-  endif(NOT PYTHON_EXECUTABLE)
++  endif(NOT Python_EXECUTABLE)
+ 
+   # Parse arguments.
+   # apart from the python files list, there are two additional arguments
 diff --git a/CMakeLists.txt b/CMakeLists.txt
-index 515aae1..0dfbcc6 100644
+index 515aae1..671419d 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -1,6 +1,8 @@
- # FindHDF5 uses NATIVE_COMMAND in separate_arguments, which requires
- # CMake 3.9.
--cmake_minimum_required(VERSION 3.9)
-+# ThOr: Let's turn it up to 3.12 for proper FindPython, as a helper
-+# to finding the correct Boost python component.
-+cmake_minimum_required(VERSION 3.12)
- 
- project(DP3)
- 
-@@ -44,10 +46,15 @@ endif(IDGAPI_LIBRARIES AND IDGAPI_INCLUDE_DIRS)
+@@ -44,10 +44,40 @@ endif(IDGAPI_LIBRARIES AND IDGAPI_INCLUDE_DIRS)
  
  find_package(CFITSIO REQUIRED)
  
-+find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
+-#Prevent accidentally finding old BoostConfig.cmake file from casapy
++# An attempt at common logic for Python and Boost:
++# 0. Try CMake 3.12's FindPython, fall back to FindPythonInterp/Libs.
++# 1. Use a default Python being found (environment should provide that).
++# 2. Use that Python's version to look for modern versioned Boost pythonXY
++#    component.
++# 3. If that fails, resort to unversioned Boost python.
++# Baseline for this is CMake 3.5, being available in LTS distros.
++# We could require Boost >= 1.67 and CMake >= 3.12 and get rid
++# of the fallbacks.
++
++find_package(Python COMPONENTS Interpreter Development)
++if(NOT Python_FOUND)
++  message(STATUS "Falling back to deprecated Python search.")
++  find_package(PythonInterp REQUIRED)
++  find_package(PythonLibs REQUIRED)
++  set(Python_VERSION ${PYTHON_VERSION_STRING})
++  set(Python_VERSION_MAJOR ${PYTHON_VERSION_MAJOR})
++  set(Python_VERSION_MINOR ${PYTHON_VERSION_MINOR})
++  set(Python_LIBRARIES ${PYTHON_LIBRARIES})
++  set(Python_INCLUDE_DIRS ${PYTHON_INCLUDE_DIRS})
++endif()
 +
- #Prevent accidentally finding old BoostConfig.cmake file from casapy
  set(Boost_NO_BOOST_CMAKE ON)
 -find_package(Boost COMPONENTS date_time filesystem python3 program_options system unit_test_framework REQUIRED)
++set(boostcomp date_time filesystem program_options system unit_test_framework)
++set(boostpy ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
++find_package(Boost 1.67.0 COMPONENTS ${boostcomp} python${boostpy})
++if(NOT Boost_FOUND)
++  message(STATUS "Falling back to old Boost search.")
++  set(boostpy "")
++  find_package(Boost REQUIRED COMPONENTS ${boostcomp} python${boostpy})
++endif()
 +
-+set(PY ${Python3_VERSION_MAJOR}${Python3_VERSION_MINOR})
-+find_package(Boost COMPONENTS date_time filesystem python${PY} program_options system unit_test_framework REQUIRED)
  include_directories(${Boost_INCLUDE_DIR})
-+include_directories(${Python3_INCLUDE_DIRS})
++include_directories(${Python_INCLUDE_DIRS})
  
  find_path(LOFAR_STATION_RESPONSE_DIR NAMES StationResponse/Station.h)
  find_library(LOFAR_STATION_RESPONSE_LIB stationresponse)
-@@ -165,8 +172,6 @@ set(DPPP_OBJECT $<TARGET_OBJECTS:DPPP_OBJ>)
+@@ -165,8 +195,6 @@ set(DPPP_OBJECT $<TARGET_OBJECTS:DPPP_OBJ>)
  #
  # Dependencies of AOFlagger
  #
@@ -38,26 +138,26 @@ index 515aae1..0dfbcc6 100644
  find_package(LibXml2 REQUIRED)
  find_package(PNG REQUIRED)
  find_library(FFTW3_LIB fftw3 REQUIRED)
-@@ -193,9 +198,9 @@ set(AOFLAGGERSTEP_OBJECT $<TARGET_OBJECTS:AOFlaggerStep_OBJ>)
+@@ -193,9 +221,9 @@ set(AOFLAGGERSTEP_OBJECT $<TARGET_OBJECTS:AOFlaggerStep_OBJ>)
  set(EXTRA_LIBRARIES ${EXTRA_LIBRARIES}
    ${AOFLAGGER_LIB}
    ${CASACORE_LIBRARIES}
 -  ${Boost_DATE_TIME_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY}  ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_PYTHON3_LIBRARY}
-+  Boost::date_time Boost::filesystem Boost::system Boost::program_options Boost::python${PY}
++  Boost::date_time Boost::filesystem Boost::system Boost::program_options Boost::python${boostpy}
    ${EXTRA_LIBRARIES}
 -  ${PYTHON_LIBRARIES}
-+  Python3::Python
++  ${Python_LIBRARIES}
    ${IDGAPI_LIBRARIES}
    Threads::Threads)
    
 diff --git a/PythonDPPP/CMakeLists.txt b/PythonDPPP/CMakeLists.txt
-index 9194dc7..69f945a 100644
+index 9194dc7..c8284a8 100644
 --- a/PythonDPPP/CMakeLists.txt
 +++ b/PythonDPPP/CMakeLists.txt
 @@ -1,5 +1,5 @@
  # If Boost-Python3 found, build this package. Otherwise give a warning.
 -if(${Boost_PYTHON3_FOUND})
-+if(${Boost_PYTHON${PY}_FOUND})
++if(${Boost_PYTHON${boostpy}_FOUND})
  
    include(PythonInstall)
  
diff --git a/LOFARBeam-buildfix.patch b/LOFARBeam-buildfix.patch
index e64ccc0849f8e8f10da777daf43d19ede3b3ad83..6895b940fa4051e2667ae11867b7d16543a40431 100644
--- a/LOFARBeam-buildfix.patch
+++ b/LOFARBeam-buildfix.patch
@@ -1,3 +1,39 @@
+diff --git a/CMake/FindCasacore.cmake b/CMake/FindCasacore.cmake
+index 51d8339..299530f 100644
+--- a/CMake/FindCasacore.cmake
++++ b/CMake/FindCasacore.cmake
+@@ -17,6 +17,8 @@
+ #
+ # Variables used by this module:
+ #  CASACORE_ROOT_DIR         - Casacore root directory. 
++#  BLAS_LIBS                 - override BLAS library
++#  LAPACK_LIBS               - override LAPACK library
+ #
+ # Variables defined by this module:
+ #  CASACORE_FOUND            - System has Casacore, which means that the
+@@ -232,7 +234,21 @@ else(NOT CASACORE_INCLUDE_DIR)
+     elseif(${_comp} STREQUAL fits)
+       casacore_find_package(CFITSIO REQUIRED)
+     elseif(${_comp} STREQUAL scimath_f)
+-      casacore_find_package(LAPACK REQUIRED)
++      # If only looking for LAPACK, no library will be added if LAPACK
++      # is part of BLAS, as it is customary nowadays. So look for both
++      # to avoid confusing linker errors on symbols used in headers/templates.
++      if(DEFINED ENV{BLAS_LIBS})
++        set(BLAS_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{BLAS_LIBS})
++      else()
++        casacore_find_package(BLAS   REQUIRED)
++      endif()
++      if(DEFINED ENV{LAPACK_LIBS})
++        set(LAPACK_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{LAPACK_LIBS})
++      else()
++        casacore_find_package(LAPACK REQUIRED)
++      endif()
+     endif(${_comp} STREQUAL casa)
+   endforeach(_comp ${_find_components})
+ endif(NOT CASACORE_INCLUDE_DIR)
 diff --git a/CMakeLists.txt b/CMakeLists.txt
 index 2867d9c..7d41a23 100644
 --- a/CMakeLists.txt
diff --git a/aoflagger-buildfix.patch b/aoflagger-buildfix.patch
index 28d6068a2aa7e1a068f4b8732dc276d693a0f4b7..3ba3cd0c1e1103b65679e93c674b2ef79f2aa00f 100644
--- a/aoflagger-buildfix.patch
+++ b/aoflagger-buildfix.patch
@@ -1,8 +1,56 @@
+diff --git a/CMake/FindCasacore.cmake b/CMake/FindCasacore.cmake
+index e4689f7..9068fdd 100644
+--- a/CMake/FindCasacore.cmake
++++ b/CMake/FindCasacore.cmake
+@@ -17,6 +17,8 @@
+ #
+ # Variables used by this module:
+ #  CASACORE_ROOT_DIR         - Casacore root directory. 
++#  BLAS_LIBS                 - override BLAS library
++#  LAPACK_LIBS               - override LAPACK library
+ #
+ # Variables defined by this module:
+ #  CASACORE_FOUND            - System has Casacore, which means that the
+@@ -232,7 +234,21 @@ else(NOT CASACORE_INCLUDE_DIR)
+     elseif(${_comp} STREQUAL fits)
+       casacore_find_package(CFITSIO REQUIRED)
+     elseif(${_comp} STREQUAL scimath_f)
+-      casacore_find_package(LAPACK REQUIRED)
++      # If only looking for LAPACK, no library will be added if LAPACK
++      # is part of BLAS, as it is customary nowadays. So look for both
++      # to avoid confusing linker errors on symbols used in headers/templates.
++      if(DEFINED ENV{BLAS_LIBS})
++        set(BLAS_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{BLAS_LIBS})
++      else()
++        casacore_find_package(BLAS   REQUIRED)
++      endif()
++      if(DEFINED ENV{LAPACK_LIBS})
++        set(LAPACK_FOUND YES)
++        list(APPEND CASACORE_LIBRARIES $ENV{LAPACK_LIBS})
++      else()
++        casacore_find_package(LAPACK REQUIRED)
++      endif()
+     endif(${_comp} STREQUAL casa)
+   endforeach(_comp ${_find_components})
+ endif(NOT CASACORE_INCLUDE_DIR)
 diff --git a/CMakeLists.txt b/CMakeLists.txt
-index fd1d7c7..777a05e 100644
+index fd1d7c7..4dd733c 100644
 --- a/CMakeLists.txt
 +++ b/CMakeLists.txt
-@@ -25,18 +25,35 @@ find_path(GSL_INCLUDE_DIR NAMES gsl/gsl_version.h)
+@@ -19,27 +19,61 @@ find_package(CFITSIO REQUIRED)
+ find_path(FFTW3_INCLUDE_DIR NAMES fftw3.h)
+ 
+ find_library(GSL_LIB NAMES gsl)
+-find_library(GSL_CBLAS_LIB NAMES gslcblas)
++if(DEFINED ENV{CBLAS_LIBS})
++  message(STATUS "Using CBLAS_LIBS from environment: $ENV{CBLAS_LIBS}")
++  set(GSL_CBLAS_LIB $ENV{CBLAS_LIBS})
++else()
++  find_library(GSL_CBLAS_LIB NAMES gslcblas)
++endif()
+ find_path(GSL_INCLUDE_DIR NAMES gsl/gsl_version.h)
+ 
  find_package(Threads REQUIRED)
  find_package(LibXml2 REQUIRED)
  find_package(PNG REQUIRED)
@@ -44,9 +92,26 @@ index fd1d7c7..777a05e 100644
 +# errors with missing symbols if not explicitly linking to fftw3_threads.
 +find_library(FFTW3_THREADS_LIB fftw3_threads OPTIONAL)
  enable_language(Fortran OPTIONAL)
- find_package(BLAS REQUIRED)
- find_package(LAPACK REQUIRED)
-@@ -53,7 +70,7 @@ include_directories(${FFTW3_INCLUDE_DIR})
+-find_package(BLAS REQUIRED)
+-find_package(LAPACK REQUIRED)
++if(DEFINED ENV{BLAS_LIBS})
++  message(STATUS "Using BLAS_LIBS from environment: $ENV{BLAS_LIBS}")
++  set(BLAS_FOUND YES)
++  set(BLAS_LIBRARIES $ENV{BLAS_LIBS})
++else()
++  find_package (BLAS REQUIRED)
++endif()
++if(DEFINED ENV{LAPACK_LIBS})
++  message(STATUS "Using LAPACK_LIBS from environment: $ENV{LAPACK_LIBS}")
++  set(LAPACK_FOUND YES)
++  set(LAPACK_LIBRARIES $ENV{LAPACK_LIBS})
++else()
++  find_package (LAPACK REQUIRED)
++endif()
+ if(CMAKE_SYSTEM_NAME MATCHES "Linux")
+   find_library(RT_LIBRARY rt)
+ #  list(APPEND LOFAR_EXTRA_LIBRARIES ${RT_LIBRARY})
+@@ -53,7 +87,7 @@ include_directories(${FFTW3_INCLUDE_DIR})
  if(GSL_INCLUDE_DIR)
  	include_directories(${GSL_INCLUDE_DIR})
  endif(GSL_INCLUDE_DIR)
diff --git a/build_lofar.sh b/build_lofar.sh
new file mode 100644
index 0000000000000000000000000000000000000000..38388be422e37ee90090f65d506790c20d757c72
--- /dev/null
+++ b/build_lofar.sh
@@ -0,0 +1,695 @@
+#!/bin/bash
+
+mandatory_vars="\
+lofar_prefix      Installation prefix to use. Packages are put into sub-directories of that.
+casacore_version  Version of CASAcore.
+pycasa_version    Version of CASAcore Python bindings.
+dysco_version     Version of Dysco.
+pybdsf_version    Version of PyBDSF.
+wsclean_version   Version of WSClean.
+CC                C compiler command to use.
+CXX               C++ compiler command to use.
+FC                Fortran compiler command to use.
+CFLAGS            C compiler flags (maybe empty, but set).
+CXXFLAGS          C++ compiler flags (maybe empty, but set).
+FCFLAGS           Fortran compiler flags (maybe empty, but set).
+BLAS_LIBS         Linker flags to get BLAS (e.g. -lopenblas).
+CBLAS_LIBS        Linker flags to get CBLAS (e.g. -lcblas)
+LAPACK_LIBS       Linker flags to get LAPACK (e.g. -lopenblas).
+"
+
+optional_vars="\
+MAKE_JOBS    Number of parallel make jobs to run.
+LIBRARY_PATH Path list for link-time library search.
+LD_RUN_PATH  Path for run-time library search path fixed at build time.
+CPATH        Path list for C/C++ include file search.
+idg_mkl      Passed on to idg BUILD_WITH_MKL
+"
+
+config=$0.config
+
+if ! test -e "$config"; then
+  cat <<EOT
+
+HALTIS - Hamburg LOFAR Toolbox Installation Script
+
+Please create/link an appropriate config script named
+
+  $config
+
+. It should load the correct environment for the site, possibly
+using environment modules or just setting variables for paths
+and compilers directly. It also has to set up PATH so that the
+desired python interpreter is directly available as the command
+python (symlinks/wrappers to call python3 and python3-config should
+be enough). Furthermore, GNU Make is expected as make command,
+and the various dependencies are expected to be present, either
+in the base OS or in some additional environment you prepared.
+
+Uppercase variables may be exported, lowercase one should not.
+
+Mandatory variables:
+
+$mandatory_vars
+
+Optional variables:
+
+$optional_vars
+
+Essential variables are exportet by the build script. You
+should only export variables in addition to setting them if you
+really want their value to affect the builds via the environment
+(standard variables like PATH are expected to be exported already).
+
+EOT
+  exit 1
+fi
+
+
+echo "Loading $config ..."
+. "$config" || exit 1
+
+while read var help
+do
+  test -n "$var" || continue
+  echo "Checking mandatory variable $var ..."
+  is_set=$(eval printf %s "\${$var+set}")
+  val=$(eval printf %s "\$$var")
+  if test "$is_set" != set; then
+    echo "Mandatory variable $var is not set." >&2
+    exit 1
+  fi
+  if test -z "$val"; then
+    case "$var" in
+      *FLAGS) true ;;
+      *)
+        echo "Variable $var needs to be non-empty." >&2
+        exit 1
+      ;;
+    esac
+  fi
+done <<< "$mandatory_vars"
+
+case "$lofar_prefix" in
+/*)
+  echo "Good. Given prefix could be an absolute path."
+;;
+*)
+  echo "Please specify absolute LOFAR_PATH!" >&2
+  exit 1
+;;
+esac
+
+# ThOr: These functions emulare what proper environment modules would do.
+
+# Add all given paths to LD_RUN_PATH, staring from
+# the vanilla environment setting.
+# Also sync LIBRARY_PATH, as some seem to pick that up.
+use_prefix()
+{
+  if test -z "$env_run_path"; then
+    env_run_path=$LD_RUN_PATH
+    env_path=$PATH
+    env_cpath=$CPATH
+    #env_ldflags=$LDFLAGS
+  fi
+  LD_RUN_PATH=$env_run_path
+  PATH=$env_path
+  CPATH=$env_cpath
+  #LDFLAGS=$end_ldflags
+  for d in "$@"
+  do
+    LD_RUN_PATH="$d/lib:$LD_RUN_PATH"
+    PATH="$d/bin:$PATH"
+    CPATH="$d/include:$CPATH"
+    #LDFLAGS="-L$d/lib $LDFLAGS"
+  done
+}
+
+# Write the command to be executed to a shell script for debugging
+# and run it. Some minimal quoting is attempted.
+store_and_run()
+{
+  local script=$1; shift
+  export > "$script.env"
+  cat <<EOT > "$script"
+#!$SHELL
+#
+# This reconstructs the central configuraton/build command.
+# To debug interactively, consider loading $script.env, too.
+#
+EOT
+  for n in "$@"
+  do
+    case "$n" in
+      *\'*)
+        echo "Really a command/argument with verbatim ' in it? $n" >&2
+        # This might be broken.
+        printf '%s\\\n' "\"$n\""
+      ;;
+      *[^[:alnum:]_-/:+,.]*|'')
+        printf '%s\\\n' "'$n'"
+      ;;
+      *) # hopefully safe
+        printf '%s\\\n' "$n"
+      ;;
+    esac
+  done >> "$script"
+  echo >> "$script"
+  "$@"
+}
+
+# Run cmake, with standard arguments.
+# One of those is PORTABLE=ON to avoid messing
+# with our provided compiler flags.
+run_cmake()
+{
+  store_and_run "$prefix/configure.sh" cmake \
+    -DCMAKE_C_COMPILER="$CC" \
+    -DCMAKE_CXX_COMPILER="$CXX" \
+    -DCMAKE_Fortran_COMPILER="$FC" \
+    -DCMAKE_C_FLAGS="$CFLAGS" \
+    -DCMAKE_CXX_FLAGS="$CXXFLAGS" \
+    -DCMAKE_Fortran_FLAGS="$FCFLAGS" \
+    -DCMAKE_SKIP_RPATH=ON \
+    -DPORTABLE=ON \
+    "$@"
+}
+
+# Take full version, like 1.2.3 and return git tag
+# name. Some have v1.2.3, some use v1.2.3 but v1.2
+# for 1.2.0.
+git_version_tag()
+{
+  local fullvers=$1                        # 1.2.3
+  local mmvers=$(cut -f 1,2 -d . <<< "$1") # 1.2
+  local mvers=$(cut -f 1 -d . <<< "$1")    # 1
+  for v in "$fullvers" "$mmvers" "$mvers"
+  do
+    if git tag | grep -q "^v$v\$"; then
+      printf "v%s\n" "$v"
+      return
+    fi
+  done
+}
+
+# ThOr: Locate a libray with provided linker
+# Arguments: name to grep for (libpng), then compiler/linker arguments.
+# Example: find_libfile libpng $(pkg-config --libs libpng) to return
+# /some/prefix/lib/libpng16.so.16
+find_libfile()
+{
+  local libname=$1; shift # Grep ldd output for that.
+  wrk=$(mktemp -d) &&
+  (
+    cd $wrk &&
+    echo 'int main(){ return 0; }' > test.c &&
+    g++ "$@" -o test.bin test.c &&
+    ldd test.bin | grep "$libname" | cut -f 3 -d ' '
+  )
+  rm -rf "$wrk"
+}
+
+begin_pkg()
+{
+  pkg=$1
+  echo "$(date) $pkg"
+  prefix="$lofar_prefix/$pkg"
+  mkdir -p "$prefix"
+}
+
+mkdir -p "$lofar_prefix" &&
+env_python_path=$PYTHON_PATH &&
+scriptdir=$(cd $(dirname $0) && pwd) &&
+config="$scriptdir/$(basename $config)" &&
+pyver=$(python --version | cut -f 2 -d ' ' | cut -f 1,2 -d .) &&
+test -n "$pyver" &&
+case "$pyver" in
+  2.*)
+    build_python=ON
+    build_python3=OFF
+  ;;
+  3.*)
+    build_python=OFF
+    build_python3=ON
+  ;;
+  *)
+    echo "There's another Python? Please check if I am OK with that."
+    exit 1
+  ;;
+esac
+# Ensure that we know what include search path variable is really used.
+# Only leave CPATH set.
+unset INCLUDE &&
+unset C_INCLUDE_PATH &&
+
+#########################################
+# Install main LOFAR software packages. #
+#########################################
+
+begin_pkg casacore
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install CASAcore
+    #
+    echo Installing CASAcore... &&
+    cd $prefix &&
+    rm -rf bin build include lib presrc &&
+    if test -e src && test -e data; then
+        echo "Re-using existing sources."
+    else
+        rm -rf src data &&
+        git clone https://github.com/casacore/casacore.git presrc &&
+        if [ "${casacore_version}" != "latest" ]; then
+            ( cd presrc &&
+            git checkout tags/v${casacore_version} )
+        fi &&
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/casacore-buildfix.patch" ) &&
+        mkdir data &&
+        ( cd data &&
+          wget --retry-connrefused \
+            ftp://anonymous@ftp.astron.nl/outgoing/Measures/WSRT_Measures.ztar &&
+          tar xf WSRT_Measures.ztar ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix $prefix &&
+    run_cmake \
+      -DCMAKE_BUILD_TYPE=Release \
+      -DCMAKE_INSTALL_PREFIX=$lofar_prefix/casacore \
+      -DDATA_DIR:PATH=$lofar_prefix/casacore/data \
+      -DENABLE_TABLELOCKING=OFF \
+      -DUSE_OPENMP=ON \
+      -DUSE_FFTW3=ON \
+      -DUSE_HDF5=ON \
+      -DBUILD_PYTHON=$build_python \
+      -DBUILD_PYTHON3=$build_python3 \
+      ../src/ &&
+    make -j $J &&
+    make install &&
+    echo Installed CASAcore. &&
+    touch $prefix/.done
+else
+    echo CASAcore already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg python-casacore
+pycasacorelib="$prefix/lib/python$pyver/site-packages"
+# ThOr: Subsequent builds might need that, so export it always here.
+export PYTHONPATH=$pycasacorelib:$PYTHONPATH
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install-python-casacore
+    #
+    # Finding libraries is broken, patch the setup to include the previously installed boost and casacore libraries.
+    # note the the python executable needs to be the right one
+    cd $prefix &&
+    rm -rf lib bin src pre &&
+    if test -e src.tar.gz; then
+        # ThOr: Dunno how to clean, but we can extract archive to avoid re-dowload.
+        echo "Using archived sources." &&
+        tar -xf src.tar.gz
+    else
+        echo "Fresh download."
+        git clone https://github.com/casacore/python-casacore presrc &&
+        if [ "$pycasa_version" != "latest" ]; then
+            ( cd presrc && git checkout tags/v$pycasa_version )
+        fi &&
+        mv presrc src &&
+        tar -czf src.tar.gz src
+    fi &&
+    # ThOr: Note that ${VAR} serves no other purpose than to delimit
+    # the name VAR itself. It does not imply any quoting. "$VAR" is careful,
+    # ${VAR} is't. "${VAR}" would be. You should either do use the braces
+    # where required only (${VAR}_suffix) or apply them alwas as a matter
+    # of style. I myself don't like them where not needed.
+    mkdir -p $pycasacorelib &&
+    cd src &&
+    # ThOr: with the pythonversion/3 module, the plain python does the correct
+    # thing. But an alternative would be to call python3 setup.py instead of
+    # ./setup.py .
+    use_prefix $lofar_prefix/casacore $prefix &&
+    # This magically picks up some RPATH, not sure how, but does neither
+    # include confgured casacore path or its own install prefix. But it seems
+    # to honour LDFLAGS.
+    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" \
+        store_and_run "$prefix/install.sh" \
+        python setup.py build_ext --swig-opts=-c++  \
+        -I$lofar_prefix/casacore/include -L$lofar_prefix/casacore/lib \
+        install --prefix $prefix &&
+    echo Installed Python CASAcore. &&
+    touch $prefix/.done
+else
+    echo Python-CASAcore already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg dysco
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install Dysco
+    #
+    echo Installing Dysco...
+    cd $prefix &&
+    rm -rf lib bin build presrc &&
+    if [ -e src ]; then
+        echo "Using existing source."
+    else
+        git clone https://github.com/aroffringa/dysco.git presrc &&
+        if [ "$dysco_version" != "latest" ]; then
+            ( cd presrc &&
+              git checkout tags/"$(git_version_tag "$dysco_version")" )
+        fi &&
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/dysco-buildfix.patch" ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix $lofar_prefix/casacore $prefix &&
+    run_cmake \
+        -DCMAKE_INSTALL_PREFIX=$prefix \
+        -DCASACORE_ROOT_DIR=$lofar_prefix/casacore \
+        ../src &&
+    make -j $J &&
+    make install &&
+    echo Installed Dysco. &&
+    touch $prefix/.done
+else
+    echo Dysco already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg aoflagger
+if [ ! -e $prefix/.done ]; then
+    echo "Installing aoflagger..."
+    #
+    # Install-aoflagger
+    #
+    cd $prefix &&
+    rm -rf build bin lib presrc&&
+    if [ -e src ]; then
+        echo "Using existing sources."
+    else
+        git clone git://git.code.sf.net/p/aoflagger/code presrc
+        if [ "$aoflagger_version" != "latest" ]; then
+            ( cd presrc && git checkout tags/v$aoflagger_version )
+        fi &&
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/aoflagger-buildfix.patch" ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    # ThOr: aoflagger build finds system's libpng, but dependencies use
+    # libpng16 from pkgsrc. So that semi-hardcoded fix stays here.
+    # But the Boost+Python stuff got fixed via the patch above.
+    use_prefix $lofar_prefix/casacore $prefix &&
+    run_cmake \
+        -DCASACORE_ROOT_DIR=$lofar_prefix/casacore \
+        -DCMAKE_INSTALL_PREFIX=$prefix \
+        -DPNG_LIBRARY_RELEASE:FILEPATH="$(find_libfile libpng $(pkg-config --libs libpng))" \
+        -DBUILD_SHARED_LIBS=ON \
+        ../src &&
+    make -j $J &&
+    make install &&
+    echo "Done with aoflagger." &&
+    touch $prefix/.done
+else
+    echo AOFlagger already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg LOFARBeam
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install the standalone StationResponse libraries.
+    #
+    echo Installing LOFARBeam...
+    cd $prefix &&
+    rm -rf build bin lib presrc &&
+    if [ -e src ]; then
+        echo "Using existing sources."
+    else
+        git clone https://github.com/lofar-astron/LOFARBeam.git presrc
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/LOFARBeam-buildfix.patch" ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix $lofar_prefix/casacore $prefix &&
+    run_cmake \
+        -DCASACORE_ROOT_DIR=$lofar_prefix/casacore \
+        -DCMAKE_INSTALL_PREFIX=$prefix \
+        ../src &&
+    make -j $J &&
+    make install &&
+    echo Done with LOFARBeam. &&
+    touch $prefix/.done
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg idg
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install the Image Domain Gridder (IDG).
+    # 
+    echo Installing IDG.
+    mkdir -p $prefix &&
+    cd       $prefix &&
+    rm -rf bin lib presrc build &&    
+    if test -d src; then
+        echo "Using existing source."
+    else
+        git clone https://gitlab.com/astron-idg/idg.git presrc &&
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/idg-buildfix.patch" ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix $prefix &&
+    # ThOr: The FindMKL included (two copies!) tries to locate libmkl_rt and
+    # sets paths from that one. Uses MKL_LIB as environment hint.
+    MKL_LIB=$LD_RUN_PATH run_cmake \
+        -DCMAKE_INSTALL_PREFIX=$prefix \
+        -DBUILD_WITH_MKL=${idg_mkl:-OFF} \
+        -DBUILD_LIB_CPU=ON \
+        -DBUILD_LIB_CUDA=ON \
+        -DCUDA_INCLUDE_DIR=$CUDA_HOME/include \
+        -DCUDA_FFT_LIBRARY=$(find_libfile libcufft.so -lcufft) \
+        -DCUDA_NVTX_LIBRARY=$(find_libfile libnvToolsExt.so -lnvToolsExt) \
+        ../src &&
+    make -j $J &&
+    make install &&
+    echo "Done with IDG." &&
+    touch $prefix/.done
+else
+    echo IDG already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg DP3
+export "PYTHONPATH=$prefix/lib/python$pyver:$PYTHONPATH" &&
+if [ ! -e $prefix/.done ]; then
+    echo Installing DP3...
+    #
+    # Install DP3.
+    #
+    cd $prefix &&
+    rm -rf build bin lib presrc &&
+    if test -e src; then
+        echo "Using existing sources."
+    else
+        git clone https://github.com/lofar-astron/DP3.git presrc &&
+        echo "Patching build (for BLAS, Boost, Python, usually) ..." && 
+        (cd presrc && patch -Np1 < "$scriptdir/DP3-buildfix.patch" ) &&
+        mv presrc src
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix \
+      $lofar_prefix/casacore \
+      $lofar_prefix/idg \
+      $lofar_prefix/LOFARBeam \
+      $lofar_prefix/aoflagger \
+      $prefix &&
+    # ThOr: It's braindead that I still have to provide things like
+    # AOFLAGGER_INCLUDE_DIR and AOFLAGGER_LIB! You should consider
+    # just locating the library via LD_RUN_PATH or LIBRARY_PATH, set
+    # by use_prefix. Or install an aoflagger-config script, like NetCDF.
+    # Or a pkg-config file. Anything standard!
+    # Really … having to specify the full path to the .so file?! Why do
+    # you love hurting yourself (or your fellow astrophysicists) that much?
+    run_cmake \
+        -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
+        -DCASACORE_ROOT_DIR=$lofar_prefix/casacore \
+        -DIDGAPI_INCLUDE_DIRS:PATH=$lofar_prefix/idg/include \
+        -DIDGAPI_LIBRARIES:PATH=$lofar_prefix/idg/lib/libidg-api.so \
+        -DLOFAR_STATION_RESPONSE_DIR:PATH=$lofar_prefix/LOFARBeam/include \
+        -DLOFAR_STATION_RESPONSE_LIB=$lofar_prefix/LOFARBeam/lib/libstationresponse.so \
+        -DAOFLAGGER_INCLUDE_DIR=$lofar_prefix/aoflagger/include \
+        -DAOFLAGGER_LIB=$lofar_prefix/aoflagger/lib/libaoflagger.so \
+        ../src &&
+    make -j $J &&
+    make install &&
+    echo Installed DP3. &&
+    touch $prefix/.done
+else
+    echo DP3 already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg wsclean
+if [ ! -e $prefix/.done ]; then
+    #
+    # Install WSClean
+    #
+    # Sometimes there are FFT to update by hand in CMakeCache.txt
+    # ThOr: Please no! ;-)
+    echo Installing WSClean.
+    cd $prefix &&
+    rm -rf build bin lib presrc &&
+    if test -e src; then
+        echo "Using existing sources."
+    else
+        if [ "$wsclean_version" != "latest" ]; then
+            # ThOr: Untested ...
+            wget http://downloads.sourceforge.net/project/wsclean/wsclean-${wsclean_version}/wsclean-${wsclean_version}.tar.bz2 &&
+            tar -xf wsclean-${wsclean_version}.tar.bz2 &&
+            mkdir presrc &&
+            mv wsclean-${wsclean_version} presrc/wsclean
+        else
+            git clone git://git.code.sf.net/p/wsclean/code presrc &&
+            mv presrc src
+        fi
+    fi &&
+    mkdir build &&
+    cd    build &&
+    use_prefix $lofar_prefix/casacore $lofar_prefix/LOFARBeam $lofar_prefix/idg &&
+    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" run_cmake \
+        -DCMAKE_INSTALL_PREFIX=$prefix \
+        -DCASACORE_ROOT_DIR=$lofar_prefix/casacore \
+        -DIDGAPI_INCLUDE_DIRS=$lofar_prefix/idg/include \
+        -DIDGAPI_LIBRARIES=$lofar_prefix/idg/lib/libidg-api.so \
+        -DLOFAR_STATION_RESPONSE_INCLUDE_DIR=$lofar_prefix/LOFARBeam/include \
+        -DLOFAR_STATION_RESPONSE_LIB=$lofar_prefix/LOFARBeam/lib/libstationresponse.so \
+        ../src/wsclean &&
+    make -j $J &&
+    make install &&
+    echo "Done with WSClean." &&
+    touch $prefix/.done
+else
+    echo WSClean already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg pyBDSF
+pybdsflib="$prefix/lib/python$pyver/site-packages"
+export PYTHONPATH=$pybdsflib:$PYTHONPATH &&
+if [ ! -e $prefix/.done ]; then
+    echo Installing pyBDSF.
+    #
+    # Install pyBDSF.
+    #
+    cd $prefix &&
+    rm -rf build bin lib presrc &&
+    if test -e src; then
+        echo "Using existing sources."
+    else
+        git clone https://github.com/lofar-astron/PyBDSF presrc &&
+        if [ "$pybdsf_version" != "latest" ]; then
+            ( cd presrc && git checkout "tags/v$pybdsf_version" )
+        fi &&
+        mv presrc src
+    fi &&
+    mkdir -p $pybdsflib &&
+    cd src &&
+    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" \
+        NPY_DISTUTILS_APPEND_FLAGS=1 \
+        store_and_run "$prefix/install.sh" \
+        python setup.py install --prefix=$prefix &&
+    echo "Done with pyBDSF." &&
+    touch $prefix/.done
+else
+    echo pyBDSF already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+begin_pkg lsmtool
+lsmlib="$prefix/lib/python$pyver/site-packages" &&
+export PYTHONPATH=$lsmlib:$PYTHONPATH &&
+if [ ! -e $prefix/.done ]; then
+    echo Installing LSMTool.
+    #
+    # Install LSMTool.
+    #
+    cd $prefix &&
+    rm -rf build bin lib presrc &&
+    if test -e src; then
+        echo "Using existing sources."
+    else
+        git clone https://github.com/darafferty/LSMTool.git presrc &&
+        mv presrc src
+    fi &&
+    mkdir -p $lsmlib &&    
+    cd src &&
+    store_and_run "$prefix/install.sh" \
+      python setup.py install --prefix=$prefix &&
+    echo "Done with LSMTool." &&
+    touch $prefix/.done
+else
+    echo LSMTool already installed.
+fi >> "$prefix/build.log" 2>&1 || exit 1
+
+echo "$(date) done with packages"
+
+###############################
+# Finish up the installation. #
+###############################
+
+echo
+echo "Installation directory contents:"
+ls ${INSTALLDIR}
+
+# Use the same environment used to building.
+
+print_init()
+{
+  cat <<EOT
+# LOFAR Tools built by $USER\@$(hostname) on $(date)
+#
+# Sourcing this script shall re-create the build environment and
+# add things from the LOFAR prefix to appropriate path variables.
+# It does _not_ set LD_LIBRARY_PATH, as a proper build should use
+# RPATH in the binaries. But there is LD_RUN_PATH / LIBRARY_PATH
+# set for link-time usage. You can set LD_LIBRARY_PATH=\$LD_RUN_PATH
+# as a hack if some binary fails to find its libs. But this would
+# be a bug in the build script that should be fixed properly.
+#
+# Step 1: The build config with all variables used.
+#
+EOT
+  cat $config
+cat <<EOT
+#
+# Step 2: Paths to LOFAR tools in the prefix.
+#
+EOT
+  for p in lsmtool pyBDSF wsclean dysco DP3\
+         idg aoflagger python-casacore casacore
+  do
+    echo "# $p"
+    pp="$lofar_prefix/$p"
+    test -d "$pp/bin" &&
+    echo "export PATH=\"\$lofar_prefix/$p/bin:\$PATH\""
+    test -d "$pp/lib" &&
+      cat <<EOT
+export LD_RUN_PATH=\"\$lofar_prefix/$p/lib:\$LD_RUN_PATH\"
+export LIBRARY_PATH\"\$lofar_prefix/$p/lib:\$LIBRARY_PATH\"
+EOT
+    test -d "$pp/include" &&
+      echo "export CPATH=\"\$lofar_prefix/$p/include:\$CPATH\""
+    pylib="$p/lib/python$pyver/site-packages"
+    test -d "$lofar_prefix/$pylib" &&
+      echo "export PYTHONPATH=\"\$lofar_prefix/$pylib:\$PYTHONPATH\""
+  done
+}
+
+print_init > "$lofar_prefix/init.sh" &&
+echo "Finished installation. You can source $lofar_prefix/init.sh and run."
diff --git a/build_lofar.sh.config.rrz b/build_lofar.sh.config.rrz
new file mode 100644
index 0000000000000000000000000000000000000000..06c92ce091f8adbe3cea546e908c12478b6e5a9b
--- /dev/null
+++ b/build_lofar.sh.config.rrz
@@ -0,0 +1,37 @@
+# The setup on the Hamburg LOFAR GPU Cluster.
+
+lofar_prefix=$1 # Give installation directory as first argument.
+aoflagger_version=2.14.0
+casacore_version=latest
+dysco_version=1.2.0
+pybdsf_version=latest # weird C++ template error with 1.8.12
+pycasa_version=3.1.1
+wsclean_version=latest
+
+# This gives us CUDA, gcc, Open MPI and dependencies
+# from pkgsrc in separate prefixes.
+
+. /sw/profile/init.sh
+module switch env env/2019Q4-cuda-gcc-openmpi
+module load pythonversion/3
+# You could experiment with idg using MKL, but it is recommened
+# to try a consistent environment where everyone agrees on using
+# MKL for BLAS/LAPACK, too.
+#module load mkl/2020.0.166
+#idg_mkl=ON
+
+make_jobs=64
+
+export CC=gcc
+export CXX=g++
+export FC=gfortran
+export CFLAGS="-march=native -O3 -fno-math-errno -ftree-vectorize"
+export FCFLAGS="$CFLAGS"
+export CXXFLAGS="-D_GLIB_USE_CXX_ABI=1 -DBOOST_NO_CXX11_SCOPED_ENUMS $CFLAGS"
+# This should match what BLAS is used in the dependencies.
+# If you venture to link statically, you might have to include
+# BLAS_LIBS in LAPACK_LIBS, same for CBLAS, LAPACKE.
+export BLAS_LIBS=$RRZ_BLAS_LIBS       # from the env/ module
+export CBLAS_LIBS=$RRZ_CBLAS_LIBS     # from the env/ module
+export LAPACK_LIBS=$RRZ_LAPACK_LIBS   # from the env/ module
+export LAPACKE_LIBS=$RRZ_LAPACKE_LIBS # from the env/ modules
diff --git a/build_lofar_cepH.sh b/build_lofar_cepH.sh
deleted file mode 100644
index c812e17657dabf6cdbf35ca13d89562c8099621d..0000000000000000000000000000000000000000
--- a/build_lofar_cepH.sh
+++ /dev/null
@@ -1,536 +0,0 @@
-#!/bin/bash
-
-# General environment settings.
-. /sw/profile/init.sh
-module switch env env/2019Q4-cuda-gcc-openmpi
-# ThOr: This provides python and python-config pointing
-# to current python3 and python3-config.
-module load pythonversion/3
-
-echo "TODO: Ensure conistent use of one openblas variant (the one cblas and numpy use)"
-
-# ThOr: Maybe not even necessary, at least with cmake builds.
-#export LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS"
-
-export J=64
-# ThOr: Use command line argument for the prefix to ease testing.
-# Not all variables shall be exported to children!
-scriptdir=$(cd $(dirname $0) && pwd)
-INSTALLDIR=${1:-$HOME/opt/lofar_200318}
-# ThOr: I hate to hardcode things that can be fetched automatically.
-# This takes whatever python is linked to python3.
-PYTHON_VERSION=$(python3 --version | cut -f 2 -d ' ' | cut -f 1,2 -d .)
-PYTHON_VERSION_NODOT=$(echo $PYTHON_VERSION|tr -d .)
-export CC=`which gcc`
-export CXX=`which g++`
-# ThOr: Fortran is at least checked for by some builds.
-export FC=`which gfortran`
-# ThOr: removed C++-specific defines from CFLAGS
-export CFLAGS="-march=native -O3 -fno-math-errno -ftree-vectorize"
-export FCFLAGS="$CFLAGS"
-export CXXFLAGS="-D_GLIB_USE_CXX_ABI=1 -DBOOST_NO_CXX11_SCOPED_ENUMS -march=native -O3 -fno-math-errno -ftree-vectorize"
-# ThOr: Those should not be global, should they? Let's add -Wl,--as-needed,
-# so that unnecessary libraries are dropped from the build.
-# Trying to ensure openblas_openmp here, but that needs to be fixed
-# in the packages, recognizing that a dependency is already using
-# openblas and then not trying to use a different one:-/
-#export LDFLAGS="-lgsl -lcblas -lopenblas_openmp -Wl,--as-needed"
-
-# Ensure that we know what include search path variable is really used.
-# Only leave CPATH set.
-unset INCLUDE
-unset C_INCLUDE_PATH
-
-# Path to where the patch for python-casacore's setup is stored.
-#export PYTHON_CASACORE_PATCH=$HOME/opt/src/patch_python-casacore.patch
-#export PATCH_AOFLAGGER=/net/lofar1/data1/sweijen/software/LOFAR/aoflagger.patch
-
-# Settings relevant to the installed software.
-# ThOr: I recommend settling on with or without v and add the v on git
-# command instead.
-export AOFLAGGER_VERSION=v2.14.0
-export BOOST_DOT_VERSION=1.67.0
-export BOOST_VERSION=1_67_0
-export CASACORE_VERSION=latest
-export CFITSIO_VERSION=3.47
-export DYSCO_VERSION=v1.2.0
-export FFTW_VERSION=3.3.8
-export HDF5_VERSION=1.8.21
-export PYBDSF_VERSION=v1.8.12
-export PYTHON_CASACORE_VERSION=3.1.1
-export WSCLEAN_VERSION=latest
-export WCSLIB_VERSION=latest
-
-# ThOr: These functions emulare what proper environment modules would do.
-
-# Add all given paths to LD_RUN_PATH, staring from
-# the vanilla environment setting.
-# Also sync LIBRARY_PATH, as some seem to pick that up.
-use_prefix()
-{
-  if test -z "$env_run_path"; then
-    env_run_path=$LD_RUN_PATH
-    env_path=$PATH
-    env_cpath=$CPATH
-    #env_ldflags=$LDFLAGS
-  fi
-  LD_RUN_PATH=$env_run_path
-  PATH=$env_path
-  CPATH=$env_cpath
-  #LDFLAGS=$end_ldflags
-  for d in "$@"
-  do
-    LD_RUN_PATH="$d/lib:$LD_RUN_PATH"
-    PATH="$d/bin:$PATH"
-    CPATH="$d/include:$CPATH"
-    #LDFLAGS="-L$d/lib $LDFLAGS"
-  done
-}
-
-# ThOr: Run cmake, with standard arguments.
-run_cmake()
-{
-  cmake \
-  -DCMAKE_C_COMPILER="$CC" \
-  -DCMAKE_CXX_COMPILER="$CXX" \
-  -DCMAKE_Fortran_COMPILER="$FC" \
-  -DCMAKE_C_FLAGS="$CFLAGS" \
-  -DCMAKE_CXX_FLAGS="$CXXFLAGS" \
-  -DCMAKE_Fortran_FLAGS="$FCFLAGS" \
-  -DCMAKE_SKIP_RPATH=ON \
-  "$@"
-}
-
-# ThOr: Locate a libray with provided linker
-# Arguments: name to grep for (libpng), then compiler/linker arguments.
-# Example: find_libfile libpng $(pkg-config --libs libpng) to return
-# /some/prefix/lib/libpng16.so.16
-find_libfile()
-{
-  local libname=$1; shift # Grep ldd output for that.
-  wrk=$(mktemp -d) &&
-  (
-    cd $wrk &&
-    echo 'int main(){ return 0; }' > test.c &&
-    g++ "$@" -o test.bin test.c &&
-    ldd test.bin | grep "$libname" | cut -f 3 -d ' '
-  )
-  rm -rf "$wrk"
-}
-
-mkdir -p $INSTALLDIR
-
-# ThOr: We got the external libs covered vi pkgsrc.
-# Only need to care for the LOFAR code.
-
-#########################################
-# Install main LOFAR software packages. #
-#########################################
-
-if [ ! -e $INSTALLDIR/casacore/.done ]; then
-    #
-    # Install CASAcore
-    #
-    echo Installing CASAcore... &&
-    mkdir -p $INSTALLDIR/casacore &&
-    cd $INSTALLDIR/casacore &&
-    rm -rf bin lib &&
-    if test -e src && test -e data; then
-        echo "Re-using existing sources."
-    else
-        rm -rf src build data &&
-        mkdir -p build data &&
-        git clone https://github.com/casacore/casacore.git src &&
-        if [ "${CASACORE_VERSION}" != "latest" ]; then
-            ( cd src &&
-            git checkout tags/${CASACORE_VERSION} )
-        fi
-        ( cd data &&
-          wget --retry-connrefused \
-            ftp://anonymous@ftp.astron.nl/outgoing/Measures/WSRT_Measures.ztar &&
-          tar xf WSRT_Measures.ztar )
-    fi &&
-    cd build &&
-    use_prefix $INSTALLDIR/casacore
-    run_cmake \
-      -DCMAKE_BUILD_TYPE=Release \
-      -DCMAKE_INSTALL_PREFIX=${INSTALLDIR}/casacore \
-      -DDATA_DIR:PATH=${INSTALLDIR}/casacore/data \
-      -DENABLE_TABLELOCKING=OFF \
-      -DUSE_OPENMP=ON \
-      -DUSE_FFTW3=ON \
-      -DUSE_HDF5=ON \
-      -DBUILD_PYTHON=OFF \
-      -DBUILD_PYTHON3=ON \
-      ../src/ &&
-    make -j $J &&
-    make install &&
-    # ThOr: Let's see if that is really needed ...
-    #ln -s ${INSTALLDIR}/casacore/lib/libcasa_python3.so \
-    #      ${INSTALLDIR}/casacore/lib/libcasa_python.so &&
-    echo Installed CASAcore. &&
-    touch $INSTALLDIR/casacore/.done
-else
-    echo CASAcore already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/python-casacore
-pycasacorelib=$prefix/lib/python$PYTHON_VERSION/site-packages
-# ThOr: Subsequent builds might need that, so export it always here.
-export PYTHONPATH=$pycasacorelib:$PYTHONPATH
-
-if [ ! -e $prefix/.done ]; then
-    #
-    # Install-python-casacore
-    #
-    # Finding libraries is broken, patch the setup to include the previously installed boost and casacore libraries.
-    # note the the python executable needs to be the right one
-    mkdir -p $prefix &&
-    cd $prefix &&
-    rm -rf lib bin src &&
-    if test -e src.tar.gz; then
-        # ThOr: Dunno how to clean, but we can extract archive to avoid re-dowload.
-        echo "Using archived sources."
-        tar -xf src.tar.gz
-    else
-        echo "Fresh download."
-        git clone https://github.com/casacore/python-casacore src &&
-        if [ "$PYTHON_CASACORE_VERSION" != "latest" ]; then
-            ( cd src && git checkout tags/v$PYTHON_CASACORE_VERSION )
-        fi &&
-        tar -czf src.tar.gz src
-    fi &&
-    # ThOr: Note that ${VAR} serves no other purpose than to delimit
-    # the name VAR itself. It does not imply any quoting. "$VAR" is careful,
-    # ${VAR} is't. "${VAR}" would be. You should either do use the braces
-    # where required only (${VAR}_suffix) or apply them alwas as a matter
-    # of style. I myself don't like them where not needed.
-    mkdir -p $pycasacorelib &&
-    cd src &&
-    # ThOr: with the pythonversion/3 module, the plain python does the correct
-    # thing. But an alternative would be to call python3 setup.py instead of
-    # ./setup.py .
-    use_prefix $INSTALLDIR/casacore $prefix &&
-    # This magically picks up some RPATH, not sure how, but does neither
-    # include confgured casacore path or its own install prefix. But it seems
-    # to honour LDFLAGS.
-    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" \
-        ./setup.py build_ext --swig-opts=-c++  \
-        -I$INSTALLDIR/casacore/include -L$INSTALLDIR/casacore/lib \
-        install --prefix $prefix &&
-    echo Installed Python CASAcore. &&
-    touch $prefix/.done
-else
-    echo Python-CASAcore already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/dysco
-if [ ! -e $prefix/.done ]; then
-    #
-    # Install Dysco
-    #
-    echo Installing Dysco...
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf lib bin build  &&
-    if [ -e src ]; then
-        echo "Using existing source."
-    else
-        git clone https://github.com/aroffringa/dysco.git src
-    fi &&
-    mkdir build &&
-    cd    build &&
-    use_prefix $INSTALLDIR/casacore $prefix &&
-    run_cmake \
-        -DCMAKE_INSTALL_PREFIX=$prefix \
-        -DCASACORE_ROOT_DIR=$INSTALLDIR/casacore \
-        ../src &&
-    make -j $J &&
-    make install &&
-    echo Installed Dysco. &&
-    touch $prefix/.done
-else
-    echo Dysco already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/aoflagger
-if [ ! -e $prefix/.done ]; then
-    echo "Installing aoflagger..."
-    #
-    # Install-aoflagger
-    #
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf build bin lib presrc&&
-    if [ -e src ]; then
-        echo "Using existing sources."
-    else
-        git clone git://git.code.sf.net/p/aoflagger/code presrc
-        if [ "${AOFLAGGER_VERSION}" != "latest" ]; then
-            ( cd presrc && git checkout tags/${AOFLAGGER_VERSION} )
-        fi
-        if ! [ grep -q Boost::python presrc/src/CMakeLists.txt ]; then
-          echo "Patching aoflagger, mainly for Boost::python" 
-          (cd presrc && patch -Np1 < "$scriptdir/aoflagger-buildfix.patch" )
-        fi &&
-        mv presrc src
-    fi &&
-    mkdir build &&
-    cd    build &&
-    # ThOr: aoflagger build finds system's libpng, but dependencies use
-    # libpng16 from pkgsrc. So that semi-hardcoded fix stays here.
-    # But the Boost+Python stuff got fixed via the patch above.
-    use_prefix $INSTALLDIR/casacore $prefix &&
-    run_cmake \
-        -DCASACORE_ROOT_DIR=$INSTALLDIR/casacore \
-        -DCMAKE_INSTALL_PREFIX=$prefix \
-        -DPNG_LIBRARY_RELEASE:FILEPATH="$(find_libfile libpng $(pkg-config --libs libpng))" \
-        -DBUILD_SHARED_LIBS=ON -DPORTABLE=ON \
-        ../src &&
-    make -j $J &&
-    make install &&
-    echo "Done with aoflagger." &&
-    touch $prefix/.done
-else
-    echo AOFlagger already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/LOFARBeam
-if [ ! -e $prefix/.done ]; then
-    #
-    # Install the standalone StationResponse libraries.
-    #
-    echo Installing LOFARBeam...
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf build bin lib presrc &&
-    if [ -e src ]; then
-        echo "Using existing sources."
-    else
-        git clone https://github.com/lofar-astron/LOFARBeam.git presrc
-        if ! [ grep -q Boost:: presrc/CMakeLists.txt ]; then
-          echo "Patching LOFARBeam, mainly for Boost::python" 
-          (cd presrc && patch -Np1 < "$scriptdir/LOFARBeam-buildfix.patch" )
-        fi &&
-        mv presrc src
-    fi &&
-    mkdir build &&
-    cd    build &&
-    use_prefix $INSTALLDIR/casacore $prefix &&
-    run_cmake \
-        -DCASACORE_ROOT_DIR=$INSTALLDIR/casacore \
-        -DCMAKE_INSTALL_PREFIX=$prefix \
-        ../src &&
-    make -j $J &&
-    make install &&
-    echo Done with LOFARBeam. &&
-    touch $prefix/.done
-fi || exit 1
-
-prefix=$INSTALLDIR/idg
-if [ ! -e $prefix/.done ]; then
-    #
-    # Install the Image Domain Gridder (IDG).
-    # 
-    echo Installing IDG.
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf bin lib presrc build &&    
-    if test -d src; then
-        echo "Using existing source."
-    else
-        git clone https://gitlab.com/astron-idg/idg.git presrc &&
-        mv presrc src
-    fi &&
-    mkdir build &&
-    cd    build &&
-    use_prefix $prefix &&
-    # ThOr: The FindMKL included (two copies!) tries to locate libmkl_rt and
-    # sets paths from that one. Uses MKL_LIB as environment hint.
-    module load mkl/2020.0.166 &&
-    MKL_LIB=$LD_RUN_PATH run_cmake \
-        -DCMAKE_INSTALL_PREFIX=$prefix \
-        -DBUILD_WITH_MKL=ON \
-        -DBUILD_LIB_CPU=ON \
-        -DBUILD_LIB_CUDA=ON \
-        -DCUDA_INCLUDE_DIR=$CUDA_HOME/include \
-        -DCUDA_FFT_LIBRARY=$(find_libfile libcufft.so -lcufft) \
-        -DCUDA_NVTX_LIBRARY=$(find_libfile libnvToolsExt.so -lnvToolsExt) \
-        ../src &&
-    make -j $J &&
-    make install &&
-    module unload mkl &&
-    echo "Done with IDG." &&
-    touch $prefix/.done
-else
-    echo IDG already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/DP3
-if [ ! -e $prefix/.done ]; then
-    echo Installing DP3...
-    #
-    # Install DP3.
-    #
-    mkdir -p $prefix
-    cd       $prefix
-    rm -rf build bin lib presrc &&
-    if test -e src; then
-        echo "Using existing sources."
-    else
-        git clone https://github.com/lofar-astron/DP3.git presrc &&
-        if ! [ grep -q Boost::python presrc/src/CMakeLists.txt ]; then
-            echo "Patching build, mainly for Boost::python" 
-            (cd presrc && patch -Np1 < "$scriptdir/DP3-buildfix.patch" )
-        fi &&
-        mv presrc src
-    fi &&
-    mkdir build &&
-    cd    build &&
-    use_prefix \
-      $INSTALLDIR/casacore \
-      $INSTALLDIR/idg \
-      $INSTALLDIR/LOFARBeam \
-      $INSTALLDIR/aoflagger \
-      $prefix &&
-    # ThOr: It's braindead that I still have to provide things like
-    # AOFLAGGER_INCLUDE_DIR and AOFLAGGER_LIB! You should consider
-    # just locating the library via LD_RUN_PATH or LIBRARY_PATH, set
-    # by use_prefix. Or install an aoflagger-config script, like NetCDF.
-    # Or a pkg-config file. Anything standard!
-    # Really … having to specify the full path to the .so file?! Why do
-    # you love hurting yourself (or your fellow astrophysicists) that much?
-    run_cmake \
-        -DCMAKE_INSTALL_PREFIX:PATH=$prefix \
-        -DCASACORE_ROOT_DIR=$INSTALLDIR/casacore \
-        -DIDGAPI_INCLUDE_DIRS:PATH=$INSTALLDIR/idg/include \
-        -DIDGAPI_LIBRARIES:PATH=$INSTALLDIR/idg/lib/libidg-api.so \
-        -DLOFAR_STATION_RESPONSE_DIR:PATH=$INSTALLDIR/LOFARBeam/include \
-        -DLOFAR_STATION_RESPONSE_LIB=$INSTALLDIR/LOFARBeam/lib/libstationresponse.so \
-        -DAOFLAGGER_INCLUDE_DIR=$INSTALLDIR/aoflagger/include \
-        -DAOFLAGGER_LIB=$INSTALLDIR/aoflagger/lib/libaoflagger.so \
-        ../src &&
-    make -j $J &&
-    make install &&
-    echo Installed DP3. &&
-    touch $prefix/.done
-else
-    echo DP3 already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/wsclean
-if [ ! -e $prefix/.done ]; then
-    #
-    # Install WSClean
-    #
-    # Sometimes there are FFT to update by hand in CMakeCache.txt
-    # ThOr: Please no! ;-)
-    echo Installing WSClean.
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf build bin lib presrc &&
-    if test -e src; then
-        echo "Using existing sources."
-    else
-        if [ "$WSCLEAN_VERSION" != "latest" ]; then
-            # ThOr: Untested ...
-            wget http://downloads.sourceforge.net/project/wsclean/wsclean-${WSCLEAN_VERSION}/wsclean-${WSCLEAN_VERSION}.tar.bz2 &&
-            tar -xf wsclean-${WSCLEAN_VERSION}.tar.bz2 &&
-            mkdir presrc &&
-            mv wsclean-${WSCLEAN_VERSION} presrc/wsclean
-        else
-            git clone git://git.code.sf.net/p/wsclean/code presrc &&
-            mv presrc src
-        fi
-    fi &&
-    mkdir build &&
-    cd    build &&
-    use_prefix $INSTALLDIR/casacore $INSTALLDIR/LOFARBeam $INSTALLDIR/idg &&
-    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" cmake \
-        -DCMAKE_INSTALL_PREFIX=$prefix \
-        -DCASACORE_ROOT_DIR=$INSTALLDIR/casacore \
-        -DIDGAPI_INCLUDE_DIRS=$INSTALLDIR/idg/include \
-        -DIDGAPI_LIBRARIES=$INSTALLDIR/idg/lib/libidg-api.so \
-        -DLOFAR_STATION_RESPONSE_INCLUDE_DIR=$INSTALLDIR/LOFARBeam/include \
-        -DLOFAR_STATION_RESPONSE_LIB=$INSTALLDIR/LOFARBeam/lib/libstationresponse.so \
-        ../src/wsclean &&
-    make -j $J &&
-    make install &&
-    echo "Done with WSClean." &&
-    touch $prefix/.done
-else
-    echo WSClean already installed.
-fi || exit 1 
-
-prefix=$INSTALLDIR/pyBDSF &&
-pybdsflib=$prefix/lib/python$PYTHON_VERSION/site-packages
-export PYTHONPATH=$pybdsflib:$PYTHONPATH &&
-if [ ! -e $prefix/.done ]; then
-    echo Installing pyBDSF.
-    #
-    # Install pyBDSF.
-    #
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf build bin lib presrc &&
-    if test -e src; then
-        echo "Using existing sources."
-    else
-        git clone https://github.com/lofar-astron/PyBDSF presrc &&
-        mv presrc src
-    fi &&
-    mkdir -p $pybdsflib &&
-    cd src &&
-    LDFLAGS="$(rrz-build-linkpath -R) $LDFLAGS" \
-        NPY_DISTUTILS_APPEND_FLAGS=1 \
-        python setup.py install --prefix=$prefix &&
-    echo "Done with pyBDSF." &&
-    touch $prefix/.done
-else
-    echo pyBDSF already installed.
-fi || exit 1
-
-prefix=$INSTALLDIR/lsmtool &&
-lsmlib=$prefix/lib/python$PYTHON_VERSION/site-packages &&
-export PYTHONPATH=$lsmlib:$PYTHONPATH &&
-if [ ! -e $prefix/.done ]; then
-    echo Installing LSMTool.
-    #
-    # Install LSMTool.
-    #
-    mkdir -p $prefix &&
-    cd       $prefix &&
-    rm -rf build bin lib presrc &&
-    if test -e src; then
-        echo "Using existing sources."
-    else
-        git clone https://github.com/darafferty/LSMTool.git presrc &&
-        mv presrc src
-    fi &&
-    mkdir -p $lsmlib &&    
-    cd src &&
-    python setup.py install --prefix=$prefix &&
-    echo "Done with LSMTool." &&
-    touch $prefix/.done
-else
-    echo LSMTool already installed.
-fi || exit 1
-
-###############################
-# Finish up the installation. #
-###############################
-echo "Installation directory contents:"
-ls ${INSTALLDIR}
-#
-# init-lofar
-#
-echo export INSTALLDIR=$INSTALLDIR > $INSTALLDIR/init.sh
-
-echo export PYTHONPATH=\$INSTALLDIR/python-casacore/lib/python${PYTHON_VERSION}/site-packages/:\$INSTALLDIR/DP3/lib/python${PYTHON_VERSION}/site-packages/:\$INSTALLDIR/pyBDSF/lib/python$PYTHON_VERSION/site-packages:\$PYTHONPATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/aoflagger/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/casacore/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/DP3/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/dysco/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/wsclean/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export PATH=\$INSTALLDIR/pyBDSF/bin:\$PATH  >> $INSTALLDIR/init.sh
-echo export LD_LIBRARY_PATH=\$INSTALLDIR/aoflagger/lib:\$INSTALLDIR/casacore/lib:\$INSTALLDIR/DP3/lib:\$INSTALLDIR/dysco/lib:\$INSTALLDIR/idg/lib:\$INSTALLDIR/LOFARBeam/lib:\$LD_LIBRARY_PATH  >> $INSTALLDIR/init.sh
diff --git a/casacore-buildfix.patch b/casacore-buildfix.patch
new file mode 100644
index 0000000000000000000000000000000000000000..63c4e698e45830fecb25d2ee82f8ebdc6f591f7a
--- /dev/null
+++ b/casacore-buildfix.patch
@@ -0,0 +1,25 @@
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 0801faacd..96d23e77b 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -344,8 +344,18 @@ if (_usebison STREQUAL YES)
+     find_package (BISON REQUIRED)
+ endif (_usebison STREQUAL YES)
+ if (_uselapack STREQUAL YES)
+-    find_package (BLAS REQUIRED)
+-    find_package (LAPACK REQUIRED)
++    if(DEFINED ENV{BLAS_LIBS})
++      set(BLAS_FOUND YES)
++      set(BLAS_LIBRARIES $ENV{BLAS_LIBS})
++    else()
++      find_package (BLAS REQUIRED)
++    endif()
++    if(DEFINED ENV{LAPACK_LIBS})
++      set(LAPACK_FOUND YES)
++      set(LAPACK_LIBRARIES $ENV{LAPACK_LIBS})
++    else()
++      find_package (LAPACK REQUIRED)
++    endif()
+     if (USE_FFTW3)
+         if (FFTW3_DISABLE_THREADS)
+             find_package (FFTW3 COMPONENTS single double REQUIRED)
diff --git a/dysco-buildfix.patch b/dysco-buildfix.patch
new file mode 100644
index 0000000000000000000000000000000000000000..ca8cfef69be73c2c82842197e0e518078da72c0e
--- /dev/null
+++ b/dysco-buildfix.patch
@@ -0,0 +1,18 @@
+diff --git a/CMakeLists.txt b/CMakeLists.txt
+index 35bc9e8..d9bdd1b 100644
+--- a/CMakeLists.txt
++++ b/CMakeLists.txt
+@@ -10,7 +10,12 @@ find_package(Casacore REQUIRED COMPONENTS casa ms tables measures)
+ 
+ find_library(GSL_LIB NAMES gsl)
+ find_path(GSL_INCLUDE_DIR NAMES gsl/gsl_version.h)
+-find_library(GSL_CBLAS_LIB NAMES gslcblas)
++if(DEFINED ENV{CBLAS_LIBS})
++  message(STATUS "Using CBLAS_LIBS from environment: $ENV{CBLAS_LIBS}")
++  set(GSL_CBLAS_LIB $ENV{CBLAS_LIBS})
++else()
++  find_library(GSL_CBLAS_LIB NAMES gslcblas)
++endif()
+ 
+ find_package(Threads REQUIRED)
+ 
diff --git a/idg-buildfix.patch b/idg-buildfix.patch
new file mode 100644
index 0000000000000000000000000000000000000000..4bcc0e8d35a8f9db1de1ffb9948e0b1edc530888
--- /dev/null
+++ b/idg-buildfix.patch
@@ -0,0 +1,20 @@
+diff --git a/idg-api/CMakeLists.txt b/idg-api/CMakeLists.txt
+index 16babb07..6d7f6ac4 100644
+--- a/idg-api/CMakeLists.txt
++++ b/idg-api/CMakeLists.txt
+@@ -72,7 +72,14 @@ if (BUILD_WITH_MKL AND (${MKL_FOUND}))
+ else()
+     # Fallback to LAPACK libraries
+     # Use LAPACK headers
+-    find_package(LAPACK REQUIRED)
++    # One could try to also use underlying BLAS explicitly, to be
++    # safe. Usually, LAPACK is included in the same lib anyway, though.
++    if(DEFINED ENV{LAPACK_LIBS})
++      set(LAPACK_FOUND YES)
++      set(LAPACK_LIBRARIES $ENV{LAPACK_LIBS})
++    else()
++      find_package (LAPACK REQUIRED)
++    endif()
+ 
+     set (${PROJECT_NAME}_link_libraries
+         ${IDG_LIBRARY}