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: download the monomer library
        run:  wget -nv https://github.com/MonomerLibrary/monomers/archive/refs/tags/ccp4-8.0.016.tar.gz

      # note that the data directory exists in the coot repo (which is where we are)
      - name: untar the monomer library
        run:  pwd && cd data && tar xf ../ccp4-8.0.016.tar.gz && ln -s monomers-ccp4-8.0.016 monomers

      - 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: 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
         brewsci/bio/ssm brewsci/bio/gemmi dwarfutils gsl rdkit glm gfortran

      # 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

      # homebrew pip refuses to do this:
      # - name: install nanobind
      #   run:  python3 -m pip install nanobind
      #
      # so try using pip3 to a user-install of nanobind

      - name: homebrew python-requests
        run:  brew install python-requests

      - name: download a Python
        run:  wget -nv http://www.python.org/ftp/python/3.10.13/Python-3.10.13.tgz

      - name: untar the Python
        run:  tar xf Python-3.10.13.tgz

      - name: configure, compile and install the Python
        run:  >
              cd Python-3.10.13 &&
              ./configure --prefix=$HOME/install &&
              make -j 4 &&
              make install

      - name: set the path
        run:  echo /Users/runner/install/bin >> $GITHUB_PATH

      - name: install nanobind
        run:  pip3 install nanobind

      - name: find nanobind in $HOME/install
        run:  find $HOME/install -name nanobind

      - name: create-build-directory-for-libcootapi
        run:  mkdir build-libcootapi

      - name: patch out CCP4-package in CMakeLists.txt
        run:  sed -i.backup -e 's/find_package.CCP4/#&/' CMakeLists.txt

      - name: run cmake
        run: >
            cd build-libcootapi &&
            cmake -DCMAKE_INSTALL_PREFIX=/Users/runner/install/chapi-Darwin-macos-gtk4
            -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_2/lib/libclipper-core.dylib
            -DCLIPPER-MMDB_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/lib/libclipper-mmdb.dylib
            -DCLIPPER-CCP4_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/lib/libclipper-ccp4.dylib
            -DCLIPPER-CONTRIB_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/lib/libclipper-contrib.dylib
            -DCLIPPER-MINIMOL_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/lib/libclipper-minimol.dylib
            -DCLIPPER-CIF_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/lib/libclipper-cif.dylib
            -DCLIPPER-CORE_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/include
            -DCLIPPER-MMDB_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/include
            -DCLIPPER-CCP4_INCLUDE_DIR=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/include
            -DMMDB2_LIBRARY=/opt/homebrew/lib/libmmdb2.dylib
            -DMMDB2_INCLUDE_DIR=/opt/homebrew/include
            -DFFTW2_INCLUDE_DIRS=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/fftw2/include
            -DFFTW2_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/fftw2/lib/libfftw.dylib
            -DRFFTW2_LIBRARY=/opt/homebrew/Cellar/clipper4coot/2.1.20180802_2/fftw2/lib/librfftw.dylib
            -Dnanobind_DIR=/Users/runner/install/lib/python3.10/site-packages/nanobind/cmake ..

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

      - name: Upload CMakeCache.txt
        uses: actions/upload-artifact@v4
        with:
            name: cmake-macos-homebrew-build-cache-package
            path: /Users/runner/work/coot/coot/build-libcootapi
            retention-days: 3

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

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

      - name: run the test-molecules-container test
        # this needs the monomer library
        env:
          MOORHEN_TEST_DATA_DIR: ../test-data
          COOT_REFMAC_LIB_DIR: /Users/runner/work/coot/coot # "data/monomers" is added before "a/ATP.cif" (say)
        # run:  cd build-libcootapi && ./test-molecules-container | egrep "^FAIL|^PASS|^UN|^LIGHTS|^, "
        run:  cd build-libcootapi && ./test-molecules-container
