name: Hardware

on:
  push:
    branches:
      - "main"
    tags:
      - "*"
  pull_request:
    paths:
      - "Hardware/**"
      - ".gitea/workflows/**"

#
jobs:
  KiCad_Hardware:
    runs-on: ubuntu-latest
    name: KiCad Hardware

    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
          submodules: true

      - uses: INTI-CMNB/KiBot@v2_k7
        with:
          # Config file
          config: Hardware/config.kibot.yaml
          # Prefix for output in config file.
          dir: _hardwareOutput

          # Hardware Files
          #
          schema: "Hardware/InstrumentVisor/InstrumentVisor.kicad_sch"
          board: "Hardware/InstrumentVisor/InstrumentVisor.kicad_pcb"
          #

      - name: upload results
        uses: actions/upload-artifact@v3
        with:
          name: Board-Hardware
          path: _hardwareOutput

  dev-publish:
    # publish only to devs
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-latest
    name: Dev-Publish
    needs: [KiCad_Hardware]

    steps:
      - uses: actions/download-artifact@v2
        with:
          name: Board-Hardware

      - name: Upload KiCAD Artifacts
        uses: edunad/actions-image@v2.0.3
        with:
          path: "*-3D_top.png"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          title: "PCB"

  publish:
    # Only run on tags
    if: github.event_name == 'push' && contains(github.ref, 'refs/tags/')
    runs-on: ubuntu-latest
    name: Publish
    needs: [KiCad_Hardware]

    steps:
      # Be in our repo
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - uses: actions/download-artifact@v2
        with:
          name: Board-Hardware

      - run: mkdir -p _staging/

      - run: cp *.step _staging/

      # Copy gerbers
      - run: cp PCBWay/*.zip _staging/Board-$(git rev-parse --short HEAD)-PCBWay.zip
      - run: cp JLCPCB/*.zip _staging/Board-$(git rev-parse --short HEAD)-JLCPCB.zip

      - name: Upload artifacts to release
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: "_staging/*"
          tag: ${{ github.ref }}
          overwrite: true
          prerelease: true
          #
          body: "Kitsune Scientific, Automated with github ci/cd."
          file_glob: true
