name: libcootapi CI Homebrew macOS

on:
  push:

jobs:

  build-libcootapi-macos-using-homebrew-deps:

    runs-on: macos-latest

    steps:

      - uses: actions/checkout@v4

      # Home on macOS is /Users/runner
      - name: where are we?
        run:  |
          pwd
          cd
          pwd

      - name: cache macos libcootapi build homebrew
        id: cache-macos-libcootapi-homebrew
        uses: actions/cache@v4
        with:
          path: /opt/homebrew
          key:  macos-libcootapi-homebrew-build-homebrew-cache

      - name: Cache test data tar
        id: macos-libcootapi-test-data-cache
        uses: actions/cache@v4
        with:
          path: test-molecule-container-test-data.tar.gz
          key: macos-libcootapi-test-data-cache

      - name: download test data
        run:  wget -N -nv https://www2.mrc-lmb.cam.ac.uk/personal/pemsley/coot/data/test-molecule-container-test-data.tar.gz

      - name: untar test data
        run:  mkdir test-data && cd test-data && tar xvf ../test-molecule-container-test-data.tar.gz

      - name: get LZA
        run: cd test-data && wget https://raw.githubusercontent.com/MonomerLibrary/monomers/refs/heads/master/l/LZA.cif

      - name: get NUT
        run: cd test-data && wget https://raw.githubusercontent.com/MonomerLibrary/monomers/refs/heads/master/n/NUT.cif

      - name: debug the files
        run: cd test-data && ls -l

      - name: Setup homebrew
        id:   setup-homebrew
        uses: Homebrew/actions/setup-homebrew@master

      - name: turn on brew analytics
        run:  brew analytics on

      # Sun 23 Jun 12:11:38 BST 2024
      #  this is consistently failing
      - name: brew update
        run:  |
          echo skipping brew update
          echo skipping brew upgrade

      # gfortran is needed for numpy.
      # numpy is needed for rdkit
      - name: Install Homebrew dependencies
        run:  >
         brew install boost boost-python3 brewsci/bio/clipper4coot robin-map nanobind
         brewsci/bio/ssm brewsci/bio/gemmi dwarfutils gsl rdkit glm gfortran

      - name: Install maeparser sure the linker can find maeparser. If CMake is used,
        run:  brew install maeparser

      # now in /opt/homebrew/opt/rdkit
      - name: tell me about rdkit installation
        run:  |
          ls /opt/homebrew/opt/rdkit
          ls /opt/homebrew/opt/rdkit/share/RDKit

      - name: test python3
        run:  which python3 && python3 --version

      - name: look for nanobind
        run: ls -l /opt/homebrew/lib/python3.13/site-packages/nanobind

      - name: look again for nanobind
        run: find /opt/homebrew/Cellar/nanobind

      - name: look 3 for nanobind
        run: ls -l /opt/homebrew/lib/python3.13/site-packages/nanobind/cmake

      - name: run cmake
        run: >
            mkdir build-libcootapi && cd build-libcootapi &&
            cmake
            -DCMAKE_BUILD_TYPE=Debug
            -DCMAKE_INSTALL_PREFIX=/Users/runner/install/chapi-Darwin-macos
            -DCMAKE_PREFIX_PATH=/opt/homebrew
            -DENHANCED_LIGAND_TOOLS=TRUE
            -DGEMMI_DIR=/opt/homebrew/lib/cmake/gemmi
            -DRDKit_DIR=/opt/homebrew/lib/cmake/rdkit
            -DSSM_INCLUDE_DIR=/opt/homebrew/include
            -DSSM_LIBRARY=/opt/homebrew/lib/libssm.dylib
            -DCLIPPER-CORE_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-core.dylib
            -DCLIPPER-MMDB_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-mmdb.dylib
            -DCLIPPER-CCP4_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-ccp4.dylib
            -DCLIPPER-CONTRIB_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-contrib.dylib
            -DCLIPPER-MINIMOL_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-minimol.dylib
            -DCLIPPER-CIF_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/lib/libclipper-cif.dylib
            -DCLIPPER-CORE_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/include
            -DCLIPPER-MMDB_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/include
            -DCLIPPER-CCP4_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/include
            -DMMDB2_LIBRARY=/opt/homebrew/lib/libmmdb2.dylib
            -DMMDB2_INCLUDE_DIR=/opt/homebrew/include
            -DFFTW2_INCLUDE_DIRS=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/fftw2/include
            -DFFTW2_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/fftw2/lib/libfftw.dylib
            -DRFFTW2_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_3/fftw2/lib/librfftw.dylib
            -Dnanobind_DIR=/opt/homebrew/lib/python3.13/site-packages/nanobind/cmake ..

      # what do we know about RDKit?
      - name: cat CMakeCache.txt
        run:  cd build-libcootapi && cat CMakeCache.txt

      - name: more diagnostics 1
        run: cd build-libcootapi && ls CMakeFiles

      - name: more diagnostics 2
        run: cd build-libcootapi && ls CMakeFiles/nanobind-static.dir

      - name: compile
        run:  cd build-libcootapi && make -j 4 VERBOSE=1

      - name: install
        run:  cd build-libcootapi && make install

      # - name: run the test-molecules-container test
      #   # run:  cd build-libcootapi && ./test-molecules-container | egrep "^FAIL|^PASS|^UN|^LIGHTS|^, "
      #   env:
      #     MOORHEN_TEST_DATA_DIR: ../test-data
      #   run:  cd build-libcootapi && ./test-molecules-container

      - name: Run test-molecules-container under lldb
        env:
          MOORHEN_TEST_DATA_DIR: ../test-data
        run: |
            cd  build-libcootapi && lldb --batch -o "run" -o "bt all" -o "quit" -- ./test-molecules-container || true


