name: Build & Upload FAH Client DEB

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: [self-hosted]
    env:
      BRANCH: ${{ github.ref_name }}
      COMMIT: ${{ github.sha }}

    steps:
      - name: Clone current branch
        run: |
          git clone --branch "$BRANCH" https://mentalnet.xyz/forgejo/markmental/fah-debian13.git .
          echo "✅ Checked out branch $BRANCH ($COMMIT)"

      - name: Install packaging tools
        run: |
          apt-get update
          apt-get install -y dpkg-dev rsync fakeroot lintian

      - name: Build Debian package
        run: |
          mkdir -p build/"$BRANCH"/staging
          SHORT_HASH=$(echo "$COMMIT" | cut -c1-7)
          PKG_NAME="fahclient-${BRANCH}-${SHORT_HASH}.deb"

          # Copy only actual package data, excluding Git/CI junk
          rsync -a --exclude='.git*' --exclude='build' --exclude='.forgejo' ./ build/"$BRANCH"/staging/

          # Build from clean staging dir
          dpkg-deb --build build/"$BRANCH"/staging build/"$BRANCH"/"$PKG_NAME"
          echo "🎉 Built $PKG_NAME from clean staging directory"

      - name: Verify package
        run: |
          lintian build/"$BRANCH"/*.deb || true
          dpkg-deb --info build/"$BRANCH"/*.deb
          dpkg-deb --contents build/"$BRANCH"/*.deb | head -20

      - name: Upload .deb artifact
        uses: actions/upload-artifact@v3
        with:
          name: fahclient-${{ env.BRANCH }}-${{ env.COMMIT }}
          path: build/${{ env.BRANCH }}/*.deb

