name: Debian package build

on:
  push:
    branches:
      - main
    paths-ignore:
      - '.clang-format'
      - '.mdl-styles'
      - '*.md'
      - 'docs/**'
      - 'licenses/**'
      - 'website/**'

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

jobs:
  build:
    name: Release package ${{ matrix.arch }}
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        arch:
          - amd64

    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          export DEBIAN_FRONTEND=noninteractive
          sudo apt-get update
          sudo apt-get install -y $(cat packages/debian-11-development.txt)

      - name: Inject versioned package name
        run: |
          VERSION=$(./scripts/get-version.sh version-and-hash)

          # Debianize the package version by replacing the first dash after
          # the numbered tag with a tilde. Subsequence dashes are ok.
          #
          VERSION=${VERSION/-/\~}

          # Debian includes a "-rev" postfix version to let the package
          # maintainer release multiple versions of the same upstream source.
          # Because the project always increments the tag's minor version and/or
          # commit hash, our revision is always 1.
          #
          REVISION=1

          PACKAGE_NAME=dosbox-staging_${VERSION}-${REVISION}_${{ matrix.arch }}

          echo "PACKAGE_NAME=${PACKAGE_NAME}" >> $GITHUB_ENV

      - name: Build the package
        run: |
          ./scripts/build-debian-package.sh

          # Move the package into the repo so the uploader can access it
          mv ../${{ env.PACKAGE_NAME }}.deb .

      - uses: actions/upload-artifact@v4
        with:
          name: ${{ env.PACKAGE_NAME }}
          path: ${{ env.PACKAGE_NAME }}.deb
          compression-level: 0
