name: Build local wallet

on:
    push:
        tags:
            - "*.*.*"
            # branches: [testaction]

jobs:
    build:
        runs-on: macOS-latest

        strategy:
            matrix:
                node-version: [16.13.x]

        steps:
            - name: Install coreutils for macOS
              run: brew install coreutils
            - uses: actions/checkout@v2
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}
            - run: brew install jq
            - run: echo "PackageVersion=$(cat package.json | jq '.version' | sed 's/\"//g')" >> $GITHUB_ENV
            - name: Update package.json
              run: |
                  sed -i -e "s/${{ env.PackageVersion }}/${{ github.ref_name }}/g" package.json
            - run: yarn
            - name: Build package
              run: npm run prepackage
            - name: Build dmg
              run: ./node_modules/.bin/electron-builder -p never --mac dmg
            - name: Build deb
              run: ./node_modules/.bin/electron-builder -p never --linux deb
            - name: Build exe
              run: ./node_modules/.bin/electron-builder -p never --windows
            - name: Calc hash
              id: calc_hash
              run: |
                  echo "dmghash=$(sha256sum build/binaries/*.dmg|cut -c-64)" >> $GITHUB_OUTPUT
                  echo "debhash=$(sha256sum build/binaries/*.deb|cut -c-64)" >> $GITHUB_OUTPUT
                  echo "exehash=$(sha256sum build/binaries/*.exe|cut -c-64)" >> $GITHUB_OUTPUT
            - name: Create Body
              id: create_body
              uses: bitshares/generate_release_notes@v1
              with:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
                  categories: |
                      [{
                        "title": "New features",
                        "labels": ["feature", "enhancement"]
                      }, {
                        "title": "Bug fixes and improvements",
                        "labels": ["bug", "bugfix"]
                      }]
                  template: |
                      Release Notes
                      _________________________________

                      $CHANGES

                      Binaries for download
                      --------
                      | Platform | SHA256 Checksum |
                      |---|---|
                      |[Microsoft Windows](https://github.com/bitshares/bitshares-ui/releases/download/${{ github.ref_name }}/BitShares_${{ github.ref_name }}.exe)|`${{steps.calc_hash.outputs.exehash}}`|
                      |[Mac](https://github.com/bitshares/bitshares-ui/releases/download/${{ github.ref_name }}/BitShares_${{ github.ref_name }}.dmg)|`${{steps.calc_hash.outputs.dmghash}}`|
                      |[Linux](https://github.com/bitshares/bitshares-ui/releases/download/${{ github.ref_name }}/BitShares_${{ github.ref_name }}.deb)|`${{steps.calc_hash.outputs.debhash}}`|
            - name: Create Release
              id: create_release
              uses: actions/create-release@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
              with:
                  tag_name: ${{ github.ref_name }}
                  release_name: Release ${{ github.ref_name }}
                  body: ${{ steps.create_body.outputs.body }}
                  draft: true
            - name: Upload dmg
              uses: actions/upload-release-asset@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  upload_url: ${{ steps.create_release.outputs.upload_url }}
                  asset_path: ./build/binaries/BitShares_${{ github.ref_name }}.dmg
                  asset_name: BitShares_${{ github.ref_name }}.dmg
                  asset_content_type: application/octet-stream
            - name: Upload deb
              uses: actions/upload-release-asset@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  upload_url: ${{ steps.create_release.outputs.upload_url }}
                  asset_path: ./build/binaries/BitShares_${{ github.ref_name }}.deb
                  asset_name: BitShares_${{ github.ref_name }}.deb
                  asset_content_type: application/x-deb
            - name: Upload exe
              uses: actions/upload-release-asset@v1
              env:
                  GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
              with:
                  upload_url: ${{ steps.create_release.outputs.upload_url }}
                  asset_path: ./build/binaries/BitShares_${{ github.ref_name }}.exe
                  asset_name: BitShares_${{ github.ref_name }}.exe
                  asset_content_type: application/octet-stream
