name: libcootapi CI Ubuntu

on:
  push:

jobs:

  build-libcootapi-for-ubuntu:

    runs-on: ubuntu-latest

    steps:

      - uses: actions/checkout@v4

      - name: Cache test data tar
        id: test-data-cache
        uses: actions/cache@v4
        with:
          path: test-molecule-container-test-data-20240316.tar.gz
          key: ubuntu-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
        run: if [ $(wget --spider -S https://www2.mrc-lmb.cam.ac.uk/personal/pemsley/coot/data/test-molecule-container-test-data-20240316.tar.gz |& awk '/^Length/ { print $2 }') != $(wc -c test-molecule-container-test-data-20240316.tar.gz |& awk '{print $1}') ] ; then if [ -e test-molecule-container-test-data-20240316.tar.gz ] ; then rm test-molecule-container-test-data-20240316.tar.gz ; fi && wget https://www2.mrc-lmb.cam.ac.uk/personal/pemsley/coot/data/test-molecule-container-test-data-20240316.tar.gz ; fi

      - name: untar test data
        run:  mkdir test-data && cd test-data && tar xvf ../test-molecule-container-test-data-20240316.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: Cache gemmi build
        id:   Cache-gemmi-build
        uses: actions/cache@v4
        with:
          path: build-gemmi
          key:  chapi-build-ubuntu-gemmi-cache-build

      - name: Update apt-get
        run:  sudo apt-get update

      - name: Install system dependencies
        run:  sudo apt-get install cmake libdw-dev libncurses5-dev robin-map-dev libjpeg-dev libpng-dev libcairo2-dev

      - name: Install linear algebra libraries
        run: sudo apt-get install libblas-dev liblapack-dev

      - name: Where are we?
        run:  pwd

      # cache restore goes before the build step
      - name: Cache chapi dependencies
        id:   cache-chapi-dependencies
        uses: actions/cache@v4
        with:
          path: /home/runner/install/chapi-Linux-ubuntu
          key:  chapi-deps-ubuntu

      - name: Run build-with-build-it-3-3
        env:
          CHAPI_ONLY: true
        run:  bash build-it-3-3 || echo "build-it-3-3 done"

      - name: Upload build-logs
        uses: actions/upload-artifact@v4
        with:
          name: ubuntu-libcootapi-chapi-dependency-logs
          path: /home/runner/public_html
          retention-days: 3

      - name: check the installed libs
        run:  find /home/runner/install/chapi-Linux-ubuntu/lib

      - name: check the installed share
        run:  find /home/runner/install/chapi-Linux-ubuntu/share

      - name: check the installed include
        run:  find /home/runner/install/chapi-Linux-ubuntu/include

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

      - name: set the path
        run:  echo /home/runner/install/chapi-Linux-ubuntu/bin >> $GITHUB_PATH

      - name: run cmake
        # I am not sure that defing nanobind_DIR here is useful.
        # This does not create coot_headless_api.pyi because timing issues
        run: >
            cd build-libcootapi &&
            cmake -DCMAKE_INSTALL_PREFIX=/home/runner/install/chapi-Linux-ubuntu
            -DCMAKE_BUILD_TYPE=Debug
            -DCMAKE_INSTALL_PREFIX=/home/runner/install/chapi-Linux-ubuntu
            -DCMAKE_PREFIX_PATH=/home/runner/install/chapi-Linux-ubuntu
            -DENHANCED_LIGAND_TOOLS=TRUE
            -DGEMMI_DIR=/home/runner/install/chapi-Linux-ubuntu/lib/cmake/gemmi
            -DRDKit_DIR=/home/runner/install/chapi-Linux-ubuntu/lib/cmake/rdkit
            -DSSM_INCLUDE_DIR=/home/runner/install/chapi-Linux-ubuntu/include
            -DSSM_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libssm.so
            -DCLIPPER-CORE_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-core.so
            -DCLIPPER-MMDB_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-mmdb.so
            -DCLIPPER-CCP4_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-ccp4.so
            -DCLIPPER-CONTRIB_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-contrib.so
            -DCLIPPER-MINIMOL_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-minimol.so
            -DCLIPPER-CIF_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libclipper-cif.so
            -DCLIPPER-CORE_INCLUDE_DIR=/home/runner/install/chapi-Linux-ubuntu/include
            -DCLIPPER-MMDB_INCLUDE_DIR=/home/runner/install/chapi-Linux-ubuntu/include
            -DCLIPPER-CCP4_INCLUDE_DIR=/home/runner/install/chapi-Linux-ubuntu/include
            -DMMDB2_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libmmdb2.so
            -DMMDB2_INCLUDE_DIR=/home/runner/install/chapi-Linux-ubuntu/include
            -DFFTW2_INCLUDE_DIRS=/home/runner/install/chapi-Linux-ubuntu/include
            -DFFTW2_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/libfftw.so
            -DRFFTW2_LIBRARY=/home/runner/install/chapi-Linux-ubuntu/lib/librfftw.so
            -Dnanobind_DIR=/home/runner/install/chapi-Linux-ubuntu/lib/python3.12/site-packages/nanobind/cmake ..

      - name: make
        run:  cd build-libcootapi && make -j 4 || echo "make done"

      - name: make part 2
        run:  cd build-libcootapi && make VERBOSE=1

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

      - name: make test-molecules-container
        run:  cd build-libcootapi && rm test-molecules-container && make VERBOSE=1 test-molecules-container

      - name: ldd the library
        run:  cd build-libcootapi && ldd ./libcootapi.so

      - name: ldd the binary
        run:  cd build-libcootapi && ldd ./test-molecules-container

      - name: run the test-molecules-container test
        env:
          MOORHEN_TEST_DATA_DIR: ../test-data
          LD_LIBRARY_PATH: /home/runner/install/chapi-Linux-ubuntu/lib
        # run:  cd build-libcootapi && ./test-molecules-container | egrep "^FAIL|^PASS|^UN|^LIGHTS|^, "
        run:  cd build-libcootapi && ./test-molecules-container

      # - name: test with chapi-using script
      #   env:
      #     MOORHEN_TEST_DATA_DIR: test-data
      #   run:  which python3 && cat python-tests/test_chapi2.py && python3 python-tests/test_chapi2.py

      # - name: where are we?
      #   run:  pwd && ls -lt

      # - name: make test-molecules-container
      #   run:  cd build-libcootapi && make test-molecules-container

      # - name: Upload binaries
      #   uses: actions/upload-artifact@v4
      #   with:
      #     name: ubuntu-libcootapi-chapi-binaries-package
      #     path: /home/runner/install
      #     retention-days: 3

