# This workflow creates a release and builds and uploads
# a debian package, as well as linux, windows and mac binaries.
#
# Trigger this workflow by pushing a version tag e.g. v1.1.0.
# Add -rc or -rcN for a release candidate/ prerelease e.g. v1.1.0-rc or v1.1.0-rc1.
name: Release
on:
  push:
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
jobs:
  buildDeb:
    runs-on: ubuntu-latest
    outputs:
      version: ${{ env.VERS }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install latest version of go
        uses: actions/setup-go@v5
        with:
          go-version: 1.x
      - name: Make sf and roy binaries
        run: |
          go install github.com/richardlehane/siegfried/cmd/sf
          go install github.com/richardlehane/siegfried/cmd/roy
        env:
          CGO_ENABLED: 0
      - name: Set environment variables # trim refs/tags/v using 'cut -c12-'
        run: |
          VERSION=$(echo "${{ github.ref }}" | cut -c12-)
          echo "VERS=$(echo $VERSION | tr . -)" >> $GITHUB_ENV
          echo "VERSION=$VERSION" >> $GITHUB_ENV
          echo "SF_PATH=siegfried_$VERSION-1_amd64" >> $GITHUB_ENV
          echo "BIN_PATH=$(go env GOPATH)/bin" >> $GITHUB_ENV
      - name: Execute debbuilder script
        run: (chmod +x debbuilder.sh && ./debbuilder.sh)
      - name: Zip executables
        run: zip -j siegfried_${{ env.VERS }}_linux64.zip ${{ env.BIN_PATH }}/sf ${{ env.BIN_PATH }}/roy
      - name: Make self-contained sf binary
        run: go install -tags static github.com/richardlehane/siegfried/cmd/sf
        env: 
          CGO_ENABLED: 0
      - name: Zip self-contained executable
        run: zip -j siegfried_${{ env.VERS }}_linux64_static.zip ${{ env.BIN_PATH }}/sf      
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: release-artifacts-ubuntu
          path: |
            ${{ env.SF_PATH }}.deb
            siegfried_${{ env.VERS }}_linux64.zip
            siegfried_${{ env.VERS }}_linux64_static.zip
  buildMac:
    runs-on: macos-latest
    needs: buildDeb
    env: 
      VERSION: ${{needs.buildDeb.outputs.version}}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install latest version of go
        uses: actions/setup-go@v5
        with:
          go-version: 1.x
      - name: Make sf and roy binaries
        run: |
          go install github.com/richardlehane/siegfried/cmd/sf
          go install github.com/richardlehane/siegfried/cmd/roy
      - name: Zip executables
        run: zip -j siegfried_${{ env.VERSION }}_mac64.zip $(go env GOPATH)/bin/sf $(go env GOPATH)/bin/roy
      - name: Make self-contained sf binary
        run: go install -tags static github.com/richardlehane/siegfried/cmd/sf
      - name: Zip self-contained executable
        run: zip -j siegfried_${{ env.VERSION }}_mac64_static.zip $(go env GOPATH)/bin/sf
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: release-artifacts-mac
          path: |
            siegfried_${{ env.VERSION }}_mac64.zip
            siegfried_${{ env.VERSION }}_mac64_static.zip
  buildWin:
    runs-on: windows-latest
    needs: buildDeb
    env: 
      VERSION: ${{needs.buildDeb.outputs.version}}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install latest version of go
        uses: actions/setup-go@v5
        with:
          go-version: 1.x
      - name: Make sf and roy binaries
        run: |
          go install github.com/richardlehane/siegfried/cmd/sf
          go install github.com/richardlehane/siegfried/cmd/roy
      - name: Zip assets
        shell: pwsh
        run: |
          7z a siegfried_${{ env.VERSION }}_win64.zip ((go env GOPATH) + "\bin\*.exe")
          md siegfried
          Copy-Item cmd\roy\data\* siegfried -recurse
          7z a data_${{ env.VERSION }}.zip siegfried
      - name: Make self-contained sf binary
        run: go install -tags static github.com/richardlehane/siegfried/cmd/sf
      - name: Zip static binary
        shell: pwsh
        run: 7z a siegfried_${{ env.VERSION }}_win64_static.zip ((go env GOPATH) + "\bin\sf.exe")
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: release-artifacts-win
          path: |
            siegfried_${{ env.VERSION }}_win64.zip
            siegfried_${{ env.VERSION }}_win64_static.zip
            data_${{ env.VERSION }}.zip      
  buildWin7:
    runs-on: windows-latest
    needs: buildDeb
    env: 
      VERSION: ${{needs.buildDeb.outputs.version}}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install latest version of go
        uses: actions/setup-go@v5
        with:
          go-version: '1.20'
      - name: Make sf and roy binaries
        run: |
          go install github.com/richardlehane/siegfried/cmd/sf
          go install github.com/richardlehane/siegfried/cmd/roy
      - name: Zip binaries
        shell: pwsh
        run: |
          7z a siegfried_${{ env.VERSION }}_win7.zip ((go env GOPATH) + "\bin\*.exe")
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: release-artifacts-win7
          path: |
            siegfried_${{ env.VERSION }}_win7.zip
  buildWasm:
    runs-on: ubuntu-latest
    needs: buildDeb
    env: 
      VERSION: ${{needs.buildDeb.outputs.version}}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v4
      - name: Install latest version of go
        uses: actions/setup-go@v5
        with:
          go-version: 1.x
      - name: Make sf.wasm
        run: |
          GOOS=js GOARCH=wasm go build -o wasm/example/sf.wasm github.com/richardlehane/siegfried/wasm
      - name: Zip wasm
        run: zip -r -j siegfried_${{ env.VERSION }}_wasm.zip sf.wasm wasm/example wasm/README.md
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: release-artifacts-wasm
          path: |
            siegfried_${{ env.VERSION }}_wasm.zip   
  createRelease:
    runs-on: ubuntu-latest
    needs: [buildMac, buildWin, buildWin7, buildWasm] 
    steps:
      - name: Set version
        run: echo "VERSION=$(echo "${{ github.ref }}" | cut -c12-)" >> $GITHUB_ENV 
      - uses: actions/download-artifact@v4
        with:
          path: release-artifacts
          pattern: release-artifacts-*
          merge-multiple: true
      - name: Create release
        uses: softprops/action-gh-release@v2
        with:
          name: Version ${{ env.VERSION }}
          prerelease: ${{ contains(github.ref, 'rc') }}
          body: see CHANGELOG.md
          files: release-artifacts/*
