name: Publish

on:
  workflow_dispatch:

jobs:
  build:
    name: ${{ matrix.name }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
        - name: "Raspberry Pi 32-bit"
          id: "32bit"  
        - name: "Raspberry Pi 64-bit"
          id: "64bit" 

    steps:
      - uses: actions/checkout@v4
        with:
          path: publish/build

      - name: Extract version from file
        id: get_version
        run: |
          VERSION=$(cut -d "'" -f2 < publish/build/pwnagotchi/_version.py)
          echo "VERSION=$VERSION" >> $GITHUB_ENV

      - name: Install qemu dependencies
        run: sudo apt update && sudo apt install qemu-user-static qemu-utils xz-utils -y

      - name: Build ${{ matrix.name }} img file
        run: cd publish/build; ls -la .; pwd; make packer; make ${{ matrix.id }}
      
      - name: Change name of .img.xz to add version
        run: |
          sudo chown runner:docker "pwnagotchi-${{ matrix.id }}.img"
          mv "pwnagotchi-${{ matrix.id }}.img" "pwnagotchi-${{ env.VERSION }}-${{ matrix.id }}.img"

      - name: PiShrink
        run: |
          wget https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.sh
          chmod +x pishrink.sh
          sudo mv pishrink.sh /usr/local/bin
          sudo pishrink.sh -aZ "pwnagotchi-${{ env.VERSION }}-${{ matrix.id }}.img"

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          prerelease: false
          make_latest: true
          tag_name: v${{ env.VERSION }}
          name: Pwnagotchi v${{ env.VERSION }}
          files: pwnagotchi-${{ env.VERSION }}-${{ matrix.id }}.img.xz
          generate_release_notes: true
