name: Linux Build

on:
  push:
    branches:
      - master
      - release_v*
    tags:
    - '*'
  pull_request:

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release
  SW_CLOUD_LOGIN: ${{ secrets.CLOUD_LOGIN }}
  CACHE_HOST: ${{ secrets.SSH_HOST }}
  GA_MEASUREMENT_ID: ${{ secrets.GA_MEASUREMENT_ID }}
  GA_API_SECRET: ${{ secrets.GA_API_SECRET }}

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
  
jobs:
  build:

    runs-on: ubuntu-latest
    container: akenmorris/manylinux-build-box-sw67
    
    steps:

    - name: Conda info
      run: conda info

    - name: Check space1
      run: df -h

    - name: Free some space 
      run: cd /__t ; rm -rf CodeQL go
      
    - name: Check space2
      run: df -h
      
    - name: Checkout code
      uses: actions/checkout@v3
      with:
        lfs: true

    - name: Workaround for permission issue
      run: git config --global --add safe.directory /__w/ShapeWorks/ShapeWorks
      
    - name: Get tags
      run: git fetch --unshallow origin +refs/tags/*:refs/tags/*

    - name: Check space3
      run: df -h

    - name: Conda Installs
      shell: bash -l {0}
      run: .github/workflows/gha_conda.sh

    - name: Restore Dependencies Cache
      id: cache-deps-restore
      uses: actions/cache/restore@v3
      with:
        path: /github/home/install
        key: ${{ runner.os }}-deps-${{ hashFiles('.github/workflows/Dockerfile', '.github/workflows/gha_deps.sh', 'install_shapeworks.sh', 'python_requirements.txt', 'build_dependencies.sh') }}

    - name: try import vtk
      shell: bash -l {0}
      run: conda activate shapeworks && python -c "import vtk"


    - name: Build Dependencies
      if: steps.cache-deps-restore.outputs.cache-hit != 'true'
      shell: bash -l {0}
      run: .github/workflows/gha_deps.sh

    - name: Save Dependencies Cache
      if: steps.cache-deps-restore.outputs.cache-hit != 'true'
      uses: actions/cache/save@v3
      with:
        path: /github/home/install
        key: ${{ runner.os }}-deps-${{ hashFiles('.github/workflows/Dockerfile', '.github/workflows/gha_deps.sh', 'install_shapeworks.sh', 'python_requirements.txt', 'build_dependencies.sh') }}

    - name: Check space4
      run: df -h

    - name: cmake
      shell: bash -l {0}
      run: conda activate shapeworks && mkdir build && cd build && cmake -DCMAKE_CXX_FLAGS=-g -DITK_DIR=$HOME/install/lib/cmake/ITK-5.4 -DVTK_DIR=$HOME/install/lib/cmake/vtk-9.5 -DXLNT_DIR=$HOME/install -DLIBIGL_DIR=$HOME/install -DOpenVDB_DIR=$HOME/install/lib/cmake/OpenVDB -DGEOMETRYCENTRAL_DIR=$HOME/install -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBuild_Studio=ON -DJKQTCommonSharedLib_DIR=$HOME/install/lib/cmake/JKQTCommonSharedLib -DJKQTMathTextSharedLib_DIR=$HOME/install/lib/cmake/JKQTMathTextSharedLib -DJKQTPlotterSharedLib_DIR=$HOME/install/lib/cmake/JKQTPlotterSharedLib -DACVD_DIR=$HOME/install -DCMAKE_PREFIX_PATH=${CONDA_PREFIX} -DCMAKE_INSTALL_PREFIX=${GITHUB_WORKSPACE}/shapeworks-install -DUSE_ORIGIN_RPATH=ON -DGA_MEASUREMENT_ID=$GA_MEASUREMENT_ID -DGA_API_SECRET=$GA_API_SECRET ..

    - name: Check space5
      run: df -h

    - name: make
      shell: bash -l {0}
      run: conda activate shapeworks && cd build && make -j4

    - name: Check space6
      run: df -h

    - name: Du
      run: cd / ; du -sh * || true

    - name: make install
      shell: bash -l {0}
      run: conda activate shapeworks && cd build && make install
      
    - name: Build Binary Package
      shell: bash -l {0}
      env:
        PR_NUMBER: ${{ github.event.number }}
      run: conda activate shapeworks && source ./devenv.sh ./build/bin && PATH=$HOME:$PATH ./Support/package.sh tag ${GITHUB_WORKSPACE}/shapeworks-install $HOME/install

    - name: Download test data
      shell: bash -l {0}
      run: .github/workflows/download_test_data.sh
      
    - name: make test
      shell: bash -l {0}
      run: conda activate shapeworks && source ./devenv.sh ./build/bin && cd build && ctest -VV

    - uses: actions/upload-artifact@v4
      with:
        name: artifact-${{github.sha}}-linux
        path: ${{runner.workspace}}/ShapeWorks/artifacts

    - name: Deploy
      id: deploy
      if: github.ref == 'refs/heads/master'
      uses: marvinpinto/action-automatic-releases@latest
      with:
        repo_token: "${{ secrets.GITHUB_TOKEN }}"
        automatic_release_tag: "dev-linux"
        prerelease: true
        title: "Development Build for Linux"
        files: |
             ${{runner.workspace}}/ShapeWorks/artifacts/*.tar.gz
      
