---
name: Create deb packages
'on':
  push:
    tags:
      v**
  workflow_dispatch:
env:
  BUILD_DIR: ${{ github.workspace }}/build/
  BUILD_TYPE: Release
  CMAKE_FLAGS: >-
    -DENABLE_STATIC=OFF
    -DOpenGL_GL_PREFERENCE=GLVND
    -DENABLE_STRIP=ON
    -DWITH_GEOJSON=OFF
    -DWITH_X11=ON

jobs:
  common:
    runs-on: ubuntu-latest
    outputs:
      upload_url: ${{ steps.create_release.outputs.upload_url }}
      release_version: ${{ env.RELEASE_VERSION }}
    steps:
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: deb packages ${{ github.ref }}
          draft: true
          # TODO Add 'body-path:' or 'body:'
      - name: Get release version
        run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> "$GITHUB_ENV"
  pack:
    runs-on: ubuntu-latest
    needs: common
    strategy:
      fail-fast: false
      matrix:
        container:
          - andrettin/wyrmgus:bullseye
          - andrettin/wyrmgus:focal
          - andrettin/wyrmgus:groovy
    container: ${{ matrix.container }}
    steps:
      - name: Check code
        uses: actions/checkout@v2
      - name: Get distro info
        run: |
          lsb_id=$(lsb_release -si | tr -d '\n')
          lsb_codename=$(lsb_release -sc | tr -d '\n')
          echo "DISTRIB=${lsb_id}-${lsb_codename}" >> "$GITHUB_ENV"
      - name: Set CPU number
        run: echo "THREADS=$(nproc)" >> "$GITHUB_ENV"
      - name: Configure CMake
        run: >-
          cmake
          -S "$GITHUB_WORKSPACE"
          -B "$BUILD_DIR"
          -DCMAKE_BUILD_TYPE="$BUILD_TYPE"
          $CMAKE_FLAGS
      - name: Workaround for tolua.cpp target on CMake 3.16.3
        run: cmake -S "$GITHUB_WORKSPACE" -B "$BUILD_DIR"
      - name: Build Wyrmgus
        run: >-
          cmake
          --build "$BUILD_DIR"
          --target wyrmgus_main
          --parallel "$THREADS"
      - name: Create package
        run: >-
          cmake
          --build "$BUILD_DIR"
          --target package
          --parallel "$THREADS"
      - name: Get package name
        run: echo "PACKAGE_NAME=$(ls wyrmgus*.deb)" >> "$GITHUB_ENV"
        working-directory: ${{ env.BUILD_DIR }}
      - name: Lint package
        run: lintian "${BUILD_DIR}/${PACKAGE_NAME}"
        continue-on-error: true
      - name: Upload Release Asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ needs.common.outputs.upload_url }}
          asset_path: ${{ env.BUILD_DIR }}/${{ env.PACKAGE_NAME }}
          # Be careful here
          asset_name: "wyrmgus-${{ needs.common.outputs.release_version }}-\
            ${{ env.DISTRIB }}.deb"
          asset_content_type: application/vnd.debian.binary-package
