name: Build Debian Package
on:
  push:
    tags: [ '[0-9]+.[0-9]+.[0-9]+' ]
  workflow_dispatch:
    inputs:
      version:
        description: 'The optional semantic version number. If not supplied the branch/tag will be used.'
        type: string

jobs:
  package-ubuntu-latest-amd64:
    runs-on: ubuntu-latest
    steps:
    - name: "Checkout"
      uses: actions/checkout@v3
      # Install packages necessary for building libcpr and package
    - name: "Update package list"
      run: sudo apt update
    - name: "Install cpr dependencies"
      run: sudo apt install -y libssl-dev libcurl4-openssl-dev libpsl-dev
    - name: "Install building tools"
      run: sudo apt install -y cmake debmake devscripts debhelper
      # Set version number
    - name: Set version based on input
      if: ${{ inputs.version }}
      run: echo "RELEASE_VERSION=${{ inputs.version }}" >> "$GITHUB_ENV"
    - name: Set version based on ref
      if: ${{ !inputs.version }}
      run: |
        mkdir -p cpr/build
        pushd cpr/build
        cmake .. -DCPR_BUILD_VERSION_OUTPUT_ONLY=ON -DCPR_USE_SYSTEM_LIB_PSL=ON -DCPR_USE_SYSTEM_CURL=ON
        echo "RELEASE_VERSION=$(cat version.txt)" >> $GITHUB_ENV
        popd
        rm -rf cpr/build
    - name: Print Version
      run: echo "deb version will be '${{ env.RELEASE_VERSION }}'"
      # Build package of runtime library
    - name: "Package build of runtime library"
      env: 
        VERSION: ${{ env.RELEASE_VERSION }}
      run: bash cpr/package-build/build-package.sh cpr

    - name: "Upload deb-packages"
      uses: actions/upload-artifact@v4
      with:
        name: artifact-deb
        path: ./*.deb
