if [[ "${target_platform}" == osx-* ]]; then
  # See https://conda-forge.org/docs/maintainer/knowledge_base.html#newer-c-features-with-old-sdk
  CXXFLAGS="${CXXFLAGS} -D_LIBCPP_DISABLE_AVAILABILITY"
fi

pyrcc5 chisurf/gui/resources/resource.qrc -o chisurf/gui/resources/resource.py

# Install modules
#################
# Update submodules
git submodule sync --recursive
git submodule update --init --recursive --force

# Install python modules
########################

# Labellib
cd modules/labellib
git fetch --tags && git checkout tags/2020.10.05
cd thirdparty/pybind11 && git checkout v2.13 && git pull && cd ../..
cd thirdparty && rm -rf eigen
git clone https://gitlab.com/libeigen/eigen
cd eigen && git switch 3.4
cd ../../../..
pip install modules/labellib --no-deps --prefix="$PREFIX"

pip install modules/clsmview --no-deps --prefix="$PREFIX"
pip install modules/ndxplorer --no-deps --prefix="$PREFIX"
pip install modules/tttrconvert --no-deps --prefix="$PREFIX"
pip install modules/quest --no-deps --prefix="$PREFIX"
pip install modules/lltf --no-deps --prefix="$PREFIX"

# Build chinet
cd modules/chinet
rm -rf build && mkdir build && cd build
cmake -S .. -B . \
  -DCMAKE_CXX_COMPILER="${CXX}" \
  -DCMAKE_INSTALL_PREFIX="$PREFIX" \
  -DBUILD_PYTHON_INTERFACE=ON \
  -DWITH_AVX=OFF \
  -DCMAKE_BUILD_TYPE=Release \
  -DBoost_USE_STATIC_LIBS=OFF \
  -DCMAKE_SWIG_OUTDIR="${PREFIX}" \
  -DBUILD_PYTHON_DOCS=ON \
  -DPython_ROOT_DIR="${PREFIX}/bin" \
  -DCMAKE_LIBRARY_OUTPUT_DIRECTORY="${PREFIX}" \
  -DWITH_MONGODB=OFF \
  -G Ninja
ninja install -j ${CPU_COUNT}
cd ../../..


# Install main module
#####################
# Note: Version handling is now managed by the CustomBuildPy class in setup.py
# This ensures consistent version handling across both pip and conda builds
# The CustomBuildPy class will:
# 1. Replace the dynamic version with a hardcoded version (current date) during build
# 2. Restore the original dynamic version in the source code after the build
# Compile cython code
$PYTHON setup.py build_ext --force --inplace
# Install python code
$PYTHON setup.py install --single-version-externally-managed --record=record.txt
