name: CMake Build Matrix

on:
  push:
    branches: build

jobs:
  build:
    name: ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    env:
      BUILD_ARCH: ${{ matrix.arch }}
    strategy:
      fail-fast: false
      matrix:
        python-version: [3.6, 3.9] # specify more python versions here (i.e. 3.9)
        arch: [x64]
        python-arch: [x64]
        os: [ windows-2019, ubuntu-20.04, ubuntu-22.04, macos-11 ]
        exclude:
          - os: ubuntu-20.04
            python-version: 3.6
          - os: ubuntu-22.04
            python-version: 3.6

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Python ${{ matrix.python-version }}
      if: runner.os != 'Linux'
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}
        architecture: ${{ matrix.python-arch }}

    - name:  Install Linux
      if: runner.os == 'Linux'
      shell: bash
      run:   |
        sudo apt-get update
        sudo apt-get install python3-dev libxml2-dev libfltk1.3-dev fluid libjpeg-dev libglm-dev libcminpack-dev libglew-dev swig doxygen graphviz texlive-latex-base zip

    - name:  Install Windows
      if: runner.os == 'Windows'
      shell: cmd
      run:   choco install -y swig

    - name:  Install MacOS
      if: runner.os == 'macos'
      shell: bash
      run:   brew install swig gcc@10

    - name:  Build Linux
      if: runner.os == 'Linux'
      shell: bash
      run:   |
        mkdir build buildlibs artifacts
        cd $GITHUB_WORKSPACE/buildlibs
        cmake -DCMAKE_BUILD_TYPE=Release -DVSP_USE_SYSTEM_LIBXML2=true -DVSP_USE_SYSTEM_FLTK=true -DVSP_USE_SYSTEM_GLM=true -DVSP_USE_SYSTEM_GLEW=true -DVSP_USE_SYSTEM_CMINPACK=true -DVSP_USE_SYSTEM_CPPTEST=false ../Libraries
        make -j2
        
        cd $GITHUB_WORKSPACE/build
        cmake -DCMAKE_BUILD_TYPE=Release -DVSP_CPACK_GEN=DEB -DVSP_LIBRARY_PATH=$GITHUB_WORKSPACE/buildlibs ../src
        make -j2 package
        
        DEBFILE=${GITHUB_WORKSPACE}/build/OpenVSP-*-Linux.deb
        VSPVER=$(ls $DEBFILE | awk 'BEGIN { FS = "-" } ; { print $2 }')
        UVER=$(lsb_release -sr)
        DEPLOY_FILE=${GITHUB_WORKSPACE}/build/OpenVSP-${VSPVER}-Ubuntu-${UVER}_amd64_${GITHUB_SHA}.deb
        
        cp $DEBFILE $DEPLOY_FILE
        cp $DEPLOY_FILE $GITHUB_WORKSPACE/artifacts
        
        ls $DEPLOY_FILE && (echo "DEB CREATION SUCCESSFUL" && exit 0) || (echo "DEB CREATION FAILED" && exit 1)

    - name:  Build Windows
      if: runner.os == 'Windows'
      shell: bash
      run:   |
        mkdir build buildlibs artifacts
        cd $GITHUB_WORKSPACE\\buildlibs
        cmake -DCMAKE_BUILD_TYPE=Release -A $BUILD_ARCH ..\\Libraries

        cmake --build . --config Release -j 2
        cd $GITHUB_WORKSPACE\\build

        cmake -DCMAKE_BUILD_TYPE=Release -DVSP_LIBRARY_PATH=$GITHUB_WORKSPACE\\buildlibs -A $BUILD_ARCH ..\\src

        cmake --build . --target package --config Release -j 2

        ZIPFILE=OpenVSP*.zip
        VSPVER=$(ls $ZIPFILE | awk 'BEGIN { FS = "-" } ; { print $2 }')
        WINVER=$(ls $ZIPFILE | awk 'BEGIN { FS = "-" } ; { print $3 }' | awk 'BEGIN { FS = "." } ; { print $1 }' )
        
        DEPLOY_FILE=OpenVSP-${VSPVER}-${WINVER}-Python${{ matrix.python-version }}_${GITHUB_SHA}.zip
        cp $ZIPFILE $DEPLOY_FILE
        cp $DEPLOY_FILE $GITHUB_WORKSPACE\\artifacts
        
        ls $DEPLOY_FILE && (echo "ZIP CREATION SUCCESSFUL" && exit 0) || (echo "ZIP CREATION FAILED" && exit 1)

    - name:  Build MacOS
      if: runner.os == 'macos'
      shell: bash
      run:   |
        mkdir build buildlibs artifacts
        cd $GITHUB_WORKSPACE/buildlibs
        cmake -DCMAKE_BUILD_TYPE=Release ../Libraries -DADEPT_CXX_COMPILER=/usr/local/bin/g++-10 -DADEPT_C_COMPILER=/usr/local/bin/gcc-10
        make -j2
        
        cd $GITHUB_WORKSPACE/build
        cmake -DVSP_LIBRARY_PATH=$GITHUB_WORKSPACE/buildlibs -DCXX_OMP_COMPILER=/usr/local/bin/g++-10 -DC_OMP_COMPILER=/usr/local/bin/gcc-10 -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX="~/OpenVSP_Install" ../src;
        make -j2 package
        
        ZIPFILE=${GITHUB_WORKSPACE}/build/OpenVSP-*-MacOS.zip
        VSPVER=$(ls $ZIPFILE | awk 'BEGIN { FS = "-" } ; { print $2 }')
        DEPLOY_FILE=${GITHUB_WORKSPACE}/build/OpenVSP-${VSPVER}-MacOS-Python${{ matrix.python-version }}_${GITHUB_SHA}.zip
        
        cp $ZIPFILE $DEPLOY_FILE
        cp $DEPLOY_FILE $GITHUB_WORKSPACE/artifacts
        
        ls $DEPLOY_FILE && (echo "ZIP CREATION SUCCESSFUL" && exit 0) || (echo "ZIP CREATION FAILED" && exit 1)

    - name: Generate Documentation
      if: matrix.os == 'ubuntu-20.04'
      shell: bash
      run:    |
        cd build
        cd vsp
        ./vsp -doc

        cp ../Doxyfile .
        doxygen Doxyfile

        cd doc
        zip -r html.zip html

        cp html.zip $GITHUB_WORKSPACE/artifacts
        ls html.zip && (echo "DOC GEN SUCCESSFUL" && exit 0) || (echo "DOC GEN FAILED" && exit 1)

    - name: Upload Artifacts
      uses: actions/upload-artifact@v3
      with:
        path: ${{ github.workspace }}/artifacts
