set -xe

PROJECT_DIR="$1"
PLATFORM=$(PYTHONPATH=tools python -c "import openblas_support; print(openblas_support.get_plat())")

printenv
# Update license
cat $PROJECT_DIR/tools/wheels/LICENSE_win32.txt >> $PROJECT_DIR/LICENSE.txt

# Install Openblas
PYTHONPATH=tools python -c "import openblas_support; openblas_support.make_init('scipy')"
mkdir -p /c/opt/32/lib/pkgconfig
mkdir -p /c/opt/64/lib/pkgconfig

# delvewheel is the equivalent of delocate/auditwheel for windows.
python -m pip install delvewheel

# make the DLL available for tools/wheels/repair_windows.sh. If you change
# this location you need to alter that script.
mkdir -p /c/opt/openblas/openblas_dll
which strip

target=$(python -c "import tools.openblas_support as obs; plat=obs.get_plat(); ilp64=obs.get_ilp64(); target=f'openblas_{plat}.zip'; obs.download_openblas(target, plat, ilp64);print(target)")

# The 32/64 bit Fortran wheels are currently coming from different locations.
if [[ $PLATFORM == 'win-32' ]]; then
  # 32-bit openBLAS
  # Download 32 bit openBLAS and put it into c/opt/32/lib
  unzip $target -d /c/opt/
  cp /c/opt/32/bin/*.dll /c/opt/openblas/openblas_dll
  # rm /c/opt/openblas/if_32/32/lib/*.dll.a
else
  # 64-bit openBLAS
  unzip $target -d /c/opt/
  cp /c/opt/64/bin/*.dll /c/opt/openblas/openblas_dll
fi
# attempt to deal with:
# https://github.com/scipy/scipy/pull/20362#issuecomment-2028517797
python -c "import tools.openblas_support as obs; obs.reformat_pkg_file('C:/opt/')"
