on:
  push:
    #branches-ignore:
    #  - main
    tags:
      - 'v*'
    #tag-ignore:
    #  - 'v*'
  workflow_dispatch: {}

env:
  GOTELEMETRY: off
  CGO_ENABLED: 1

permissions:
  contents: write

name: Build & Release
jobs:
  build-linux:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - goos: linux
            goarch: amd64
            out: aws-vault-linux-amd64
            cgo: "1"
            cc: gcc
            pkg: build-essential

          - goos: linux
            goarch: arm64
            out: aws-vault-linux-arm64
            cgo: "1"
            cc: aarch64-linux-gnu-gcc
            pkg: gcc-aarch64-linux-gnu

          - goos: linux
            goarch: ppc64le
            out: aws-vault-linux-ppc64le
            cgo: "1"
            cc: powerpc64le-linux-gnu-gcc
            pkg: gcc-powerpc64le-linux-gnu

          - goos: freebsd
            goarch: amd64
            out: aws-vault-freebsd-amd64
            cgo: "0"
            cc: ""
            pkg: ""

          - goos: windows
            goarch: amd64
            out: aws-vault-windows-amd64.exe
            cgo: "0"
            cc: ""
            pkg: ""

          - goos: windows
            goarch: arm64
            out: aws-vault-windows-arm64.exe
            cgo: "0"
            cc: ""
            pkg: ""

    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0 # disable shallow clone - get all

      #- name: Unshallow
      #  run: git fetch --prune --unshallow

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: 'stable'
          #go-version-file: 'go.mod'
          check-latest: true
        id: go

      - name: Install toolchain
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential ${{ matrix.pkg }}

      - name: Build
        env:
          GOOS: ${{ matrix.goos }}
          GOARCH: ${{ matrix.goarch }}
          CGO_ENABLED: ${{ matrix.cgo }}
          CC: ${{ matrix.cc }}
        run: |
          make ${{ matrix.out }}

      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.out }}
          path: ${{ matrix.out }}

  # we can cross-compile on Linux
  #build-windows:
  #  if: startsWith(github.ref, 'refs/tags/x')
  #  strategy:
  #    matrix:
  #      include:
  #        - os: windows-latest
  #          goos: winows
  #          goarch: amd64
  #          out: aws-vault-windows-amd64.exe
  #        - os: windows-11-arm
  #          goos: winows
  #          goarch: arm64
  #          out: aws-vault-windows-arm64.exe

  #  runs-on: ${{ matrix.os }}
  #  steps:
  #    - name: Checkout
  #      uses: actions/checkout@v6
  #      with:
  #        fetch-depth: 0 # disable shallow clone - get all

  #    - name: Set up Go
  #      uses: actions/setup-go@v6
  #      with:
  #        go-version: 'stable'
  #        #go-version-file: 'go.mod'
  #        check-latest: true
  #      id: go

  #    - name: Build
  #      env:
  #        GOOS: windows
  #        GOARCH: ${{ matrix.goarch }}
  #        CGO_ENABLED: 1
  #      run: |
  #        make ${{ matrix.out }}
  #    - uses: actions/upload-artifact@v7
  #      with:
  #        name: ${{ matrix.out }}
  #        path: ${{ matrix.out }}
  #        retention-days: 7

  build-macos:
    runs-on: macos-latest
    strategy:
      matrix:
        include:
          - goarch: amd64
            out: aws-vault-darwin-amd64
          - goarch: arm64
            out: aws-vault-darwin-arm64
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 0 # disable shallow clone - get all

      - name: Set up Go
        uses: actions/setup-go@v6
        with:
          go-version: 'stable'
          #go-version-file: 'go.mod'
          check-latest: true
        id: go

      - name: Build
        env:
          GOOS: darwin
          GOARCH: ${{ matrix.goarch }}
          CGO_ENABLED: 1
        run: |
          make ${{ matrix.out }}
      - uses: actions/upload-artifact@v7
        with:
          name: ${{ matrix.out }}
          path: ${{ matrix.out }}

  release:
    name: Release
    needs: [build-linux, build-macos]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6

      - name: Install UPX
        run: sudo apt-get update && sudo apt-get install -y upx

      - name: Download Artifacts
        uses: actions/download-artifact@v8
        with:
          path: dist
          merge-multiple: true

      # NOTE: Disabled for {windows-amd64} due Chocolately AV false positives
      - name: Compress binaries with UPX
        run: |
          chmod +x dist/aws-vault-*
          upx dist/aws-vault-linux*

      - name: Generate SHA256 checksums
        run: |
          cp dist/aws-vault-* .
          make aws-vault_sha256_checksums.txt
          cp aws-vault_sha256_checksums.txt dist/

      - name: Create GitHub Release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: softprops/action-gh-release@v3
        with:
          # Identify if this is a pre release by checking if the tag name contains -rc, -b, -a
          prerelease: ${{ contains(github.ref, '-rc') || contains(github.ref, '-b') || contains(github.ref, '-a') }}
          files: |
            dist/**
          tag_name: ${{ github.ref_name }}
          generate_release_notes: true
          #body: "Changelog of release ${{ github.ref_name }}."
          #append_body: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    #- name: Run GoReleaser
    #  uses: goreleaser/goreleaser-action@v6
    #  with:
    #    version: '~> v2' # latest
    #    args: release --clean
    #    workdir: .
    #  env:
    #    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    #    VERSION: ${{ env.VERSION }}
