name: Build dfu-programmer

# concurrency:
#   group: ${{ github.workflow }}-${{ github.ref }}
#   cancel-in-progress: true

on:
  push:
  # pull_request:

jobs:
  Windows-x86:
    name: Windows x86
    runs-on: windows-latest

    defaults:
      run:
        shell: C:/msys64/usr/bin/bash.exe --noprofile --norc -e -x -o pipefail {0}

    steps:
      - uses: actions/checkout@v3

      - name: Override Version String
        if: "!startsWith(github.ref, 'refs/tags/v')"
        id: override
        shell: bash
        run: echo 'override=PACKAGE_STRING="dfu-programmer ${{ github.ref_name }} ${{ github.sha }}"' >> $GITHUB_OUTPUT

      - name: Get version
        if: startsWith(github.ref, 'refs/tags/v')
        id: version
        shell: bash
        run: echo version=${GITHUB_REF#refs/tags/v} >> $GITHUB_OUTPUT

      - name: Dependencies
        run: |
          # Fix issues with MSYS2 and cygwin tools conflicting by just using MSYS2
          export PATH=/usr/bin:/usr/local/bin

          # While some of these are already available, they are cygwin versions which are incompatible with MSYS2
          pacman --color always --noconfirm -S automake-wrapper autoconf-wrapper make mingw-w64-i686-gcc mingw-w64-i686-libusb

      - name: Bootstrap
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw32/bin
          ./bootstrap.sh

      - name: Configure
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw32/bin
          ./configure CFLAGS="-Werror -O2" --disable-dependency-tracking ${{ steps.override.outputs.override }}

      - name: Build
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw32/bin
          make -j4

      - name: Prep dist
        shell: pwsh
        run: |
          mkdir dist

          # The binary
          cp src/dfu-programmer.exe dist/

          # The docs
          cp docs/dfu-programmer.html dist/

          # Libusb 1.0
          cp C:/msys64/mingw32/bin/libusb-1.0.dll dist/
          cp C:/msys64/mingw32/share/licenses     dist/ -Recurse

      - name: Upload dist as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: Windows x86 - dfu-programmer
          path: dist

      - name: Test
        shell: pwsh
        run: dist/dfu-programmer.exe --help

      - name: Make release archives of binaries
        if: startsWith(github.ref, 'refs/tags/v')
        shell: pwsh
        working-directory: dist
        run: |
          7z a -tzip ../dfu-programmer-x86-${{ steps.version.outputs.version }}.zip *
          7z a -t7z  ../dfu-programmer-x86-${{ steps.version.outputs.version }}.7z  *

      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          fail_on_unmatched_files: true
          files: |
            dfu-programmer-x86-${{ steps.version.outputs.version }}.zip
            dfu-programmer-x86-${{ steps.version.outputs.version }}.7z

  Windows-x64:
    name: Windows x64
    runs-on: windows-latest

    defaults:
      run:
        shell: C:/msys64/usr/bin/bash.exe --noprofile --norc -e -x -o pipefail {0}

    steps:
      - uses: actions/checkout@v3

      - name: Override Version String
        if: "!startsWith(github.ref, 'refs/tags/v')"
        id: override
        shell: bash
        run: echo 'override=PACKAGE_STRING="dfu-programmer ${{ github.ref_name }} ${{ github.sha }}"' >> $GITHUB_OUTPUT

      - name: Get version
        if: startsWith(github.ref, 'refs/tags/v')
        id: version
        shell: bash
        run: echo version=${GITHUB_REF#refs/tags/v} >> $GITHUB_OUTPUT

      - name: Dependencies
        run: |
          # Fix issues with MSYS2 and cygwin tools conflicting by just using MSYS2
          export PATH=/usr/bin:/usr/local/bin

          # While some of these are already available, they are cygwin versions which are incompatible with MSYS2
          pacman --color always --noconfirm -S automake-wrapper autoconf-wrapper make mingw-w64-x86_64-gcc mingw-w64-x86_64-libusb

      - name: Bootstrap
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw64/bin
          ./bootstrap.sh

      - name: Configure
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw64/bin
          ./configure CFLAGS="-Werror -O2" --disable-dependency-tracking ${{ steps.override.outputs.override }}

      - name: Build
        run: |
          export PATH=/usr/bin:/usr/local/bin:/mingw64/bin
          make -j4

      - name: Prep dist
        shell: pwsh
        run: |
          mkdir dist

          # The binary
          cp src/dfu-programmer.exe dist/

          # The docs
          cp docs/dfu-programmer.html dist/

          # Libusb 1.0
          cp C:/msys64/mingw64/bin/libusb-1.0.dll dist/
          cp C:/msys64/mingw64/share/licenses     dist/ -Recurse

      - name: Upload dist as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: Windows x64 - dfu-programmer
          path: dist

      - name: Test
        shell: pwsh
        run: dist/dfu-programmer.exe --help

      - name: Make release archives of binaries
        if: startsWith(github.ref, 'refs/tags/v')
        shell: pwsh
        working-directory: dist
        run: |
          7z a -tzip ../dfu-programmer-x64-${{ steps.version.outputs.version }}.zip *
          7z a -t7z  ../dfu-programmer-x64-${{ steps.version.outputs.version }}.7z  *

      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          fail_on_unmatched_files: true
          files: |
            dfu-programmer-x64-${{ steps.version.outputs.version }}.zip
            dfu-programmer-x64-${{ steps.version.outputs.version }}.7z

  Linux:
    name: Linux x64
    runs-on: ubuntu-latest

    steps:
      - run: sudo apt-get install -y libusb-1.0-0-dev

      - uses: actions/checkout@v3

      - name: Override Version String
        if: "!startsWith(github.ref, 'refs/tags/v')"
        id: override
        run: echo 'override=PACKAGE_STRING="dfu-programmer ${{ github.ref_name }} ${{ github.sha }}"' >> $GITHUB_OUTPUT

      - name: Get version
        if: startsWith(github.ref, 'refs/tags/v')
        id: version
        run: echo version=${GITHUB_REF#refs/tags/v} >> $GITHUB_OUTPUT

      - name: Get Changes
        if: startsWith(github.ref, 'refs/tags/v')
        id: changes
        run: |
          echo "## Changes" > CHANGES
          echo "" >> CHANGES
          sed -ne '2,/^== Release/p' NEWS | head -n -2 >> CHANGES

      - name: bootstrap
        run: ./bootstrap.sh

      - name: configure
        run: ./configure CFLAGS="-Werror -O2" --disable-dependency-tracking ${{ steps.override.outputs.override }}

      - name: make
        run: make -j4

      - name: Test
        run: src/dfu-programmer --help

      - name: Upload Binary as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: Linux x64 - dfu-programmer binary
          path: src/dfu-programmer

      - name: make dist
        run: make -j4 dist

      - name: Upload Tarball as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: Linux x64 - dfu-programmer dist
          path: "*.tar.gz"

      - name: Make release archives of binaries
        if: startsWith(github.ref, 'refs/tags/v')
        working-directory: src
        run: |
          7z a -tzip ../dfu-programmer-linux-${{ steps.version.outputs.version }}.zip dfu-programmer
          7z a -t7z  ../dfu-programmer-linux-${{ steps.version.outputs.version }}.7z  dfu-programmer
      
      - name: Test Release Archives
        run: |
          mkdir -p test-build
          tar xzf dfu-programmer-*.tar.gz -C test-build
          cd test-build/dfu-programmer-*
          ./bootstrap.sh
          ./configure CFLAGS="-Werror -O2" --disable-dependency-tracking
          make -j4
          src/dfu-programmer --help

      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          body_path: CHANGES
          fail_on_unmatched_files: true
          files: |
            dfu-programmer-*.tar.gz
            dfu-programmer-linux-${{ steps.version.outputs.version }}.zip
            dfu-programmer-linux-${{ steps.version.outputs.version }}.7z

  macOS:
    name: macOS x64
    runs-on: macos-latest

    steps:
      - run: brew install automake

      - uses: actions/checkout@v3

      - name: Override Version String
        if: "!startsWith(github.ref, 'refs/tags/v')"
        id: override
        run: echo 'override=PACKAGE_STRING="dfu-programmer ${{ github.ref_name }} ${{ github.sha }}"' >> $GITHUB_OUTPUT

      - name: Get version
        if: startsWith(github.ref, 'refs/tags/v')
        id: version
        run: echo version=${GITHUB_REF#refs/tags/v} >> $GITHUB_OUTPUT

      - name: bootstrap
        run: ./bootstrap.sh

      - name: configure
        run: ./configure CFLAGS="-Werror -O2" --disable-dependency-tracking ${{ steps.override.outputs.override }}

      - name: make
        run: make -j4

      - name: Test
        run: src/dfu-programmer --help

      - name: Upload Binary as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: macOS - dfu-programmer binary
          path: src/dfu-programmer

      - name: make dist
        run: make -j4 dist

      - name: Upload Tarball as Build Artifact
        uses: actions/upload-artifact@v3.1.0
        with:
          name: macOS - dfu-programmer dist
          path: "*.tar.gz"

      - name: Make release archives of binaries
        if: startsWith(github.ref, 'refs/tags/v')
        working-directory: src
        run: |
          7z a -tzip ../dfu-programmer-macOS-${{ steps.version.outputs.version }}.zip dfu-programmer
          7z a -t7z  ../dfu-programmer-macOS-${{ steps.version.outputs.version }}.7z  dfu-programmer

      - name: Release
        uses: softprops/action-gh-release@v1
        if: startsWith(github.ref, 'refs/tags/v')
        with:
          fail_on_unmatched_files: true
          files: |
            dfu-programmer-macOS-${{ steps.version.outputs.version }}.zip
            dfu-programmer-macOS-${{ steps.version.outputs.version }}.7z
