name: Package for Launchpad PPA

on:
  workflow_call:
    secrets:
      PPA_GPG_PRIVATE_KEY:
        required: true
      PPA_SFTP_PRIVATE_KEY:
        required: true
    inputs:
      ppa_repo:
        description: Meshtastic PPA to target
        required: true
        type: string
      series:
        description: Ubuntu series to target
        required: true
        type: string

permissions:
  contents: read

jobs:
  build-debian-src:
    uses: ./.github/workflows/build_debian_src.yml
    secrets: inherit
    with:
      series: ${{ inputs.series }}
      build_location: ppa

  package-ppa:
    if: ${{ github.event_name != 'pull_request_target' && github.event_name != 'pull_request' }}
    runs-on: ubuntu-24.04
    needs: build-debian-src
    steps:
      - name: Checkout code
        uses: actions/checkout@v6
        with:
          submodules: recursive
          path: meshtasticd

      - name: Install deps
        shell: bash
        run: |
          sudo apt-get update -y --fix-missing
          sudo apt-get install -y dput openssh-client

      - name: Import GPG key
        uses: crazy-max/ghaction-import-gpg@v7
        with:
          gpg_private_key: ${{ secrets.PPA_GPG_PRIVATE_KEY }}
        id: gpg

      - name: Get release version string
        working-directory: meshtasticd
        run: |
          echo "deb=$(./bin/buildinfo.py deb)" >> $GITHUB_OUTPUT
        env:
          BUILD_LOCATION: ppa
        id: version

      - name: Download artifacts
        uses: actions/download-artifact@v8
        with:
          name: firmware-debian-${{ steps.version.outputs.deb }}~${{ inputs.series }}-src
          merge-multiple: true

      - name: Display structure of downloaded files
        run: ls -lah

      - name: Trust Launchpad's SSH key
        run: |
          mkdir -p ~/.ssh
          ssh-keyscan -H ppa.launchpad.net >> ~/.ssh/known_hosts

      - name: Setup dput config
        env:
          ppa_login: meshtasticorg
        run: |
          sudo tee /etc/meshtastic-dput.cf >/dev/null <<EOF
          [ppa]
          fqdn                    = ppa.launchpad.net
          method                  = ftp
          incoming                = ~%(ppa)s
          login                   = anonymous

          [ssh-ppa]
          fqdn                    = ppa.launchpad.net
          method                  = sftp
          incoming                = ~%(ssh-ppa)s
          login                   = ${ppa_login}
          EOF

      - name: Import SSH key
        uses: webfactory/ssh-agent@v0.10.0
        with:
          ssh-private-key: ${{ secrets.PPA_SFTP_PRIVATE_KEY }}
        id: ssh

      - name: Publish with dput (sftp)
        timeout-minutes: 30 # dput is terrible, sometimes runs 'forever'
        env:
          up_ppa_repo: ${{ inputs.ppa_repo }}
          up_series: ${{ inputs.series }}
          up_version: ${{ steps.version.outputs.deb }}
        run: >
          dput -c /etc/meshtastic-dput.cf
          ssh-${up_ppa_repo}
          meshtasticd_${up_version}~${up_series}_source.changes
