name: Oxen Electron Wallet Build

on:
  push:
    branches:
      - master
      - development

jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, macos-latest, ubuntu-latest]
    steps:
      - name: Checkout git repo
        uses: actions/checkout@v1

      # Read node version from `.nvmrc` file
      - name: Read nvm rc
        id: nvmrc
        uses: browniebroke/read-nvmrc-action@v1

      - name: Install node
        uses: actions/setup-node@v1
        with:
          node-version: ${{ steps.nvmrc.outputs.node_version }}

      - name: Install dependencies
        run: npm install

      - name: Download oxend binaries
        run: ./download-oxen-bins.sh
        env:
          OS: ${{ runner.os }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        working-directory: ./downloads

      - name: Extract zip binaries
        if: runner.os == 'Windows'
        run: unzip latest
        shell: bash
        working-directory: ./downloads

      - name: Extract xz binaries
        if: runner.os != 'Windows'
        run: tar -xf latest
        shell: bash
        working-directory: ./downloads

      - name: Move oxend binaries
        run: |
          find ./downloads -type f -name "oxend*" -exec cp '{}' ./bin \;
          find ./downloads -type f -name "oxen-wallet-rpc*" -exec cp '{}' ./bin \;
        shell: bash

      - name: Verify binaries
        run: ls ./bin
        shell: bash

      - name: Build window and linux binaries
        if: runner.os != 'macOS'
        run: npm run build
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Build mac binaries
        if: runner.os == 'macOS'
        run: npm run build
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          CSC_LINK: ${{ secrets.MAC_CERTIFICATE }}
          CSC_KEY_PASSWORD: ${{ secrets.MAC_CERTIFICATE_PASSWORD }}
          SIGNING_APPLE_ID: ${{ secrets.SIGNING_APPLE_ID }}
          SIGNING_APP_PASSWORD: ${{ secrets.SIGNING_APP_PASSWORD }}
          SIGNING_TEAM_ID: ${{ secrets.SIGNING_TEAM_ID }}

      - name: Remove un-needed artifacts
        run: rm -r -- ./*/
        shell: bash
        working-directory: ./dist/electron/Packaged

      - name: Upload artifacts
        uses: actions/upload-artifact@v1
        with:
          name: ${{ runner.OS }}
          path: dist/electron/Packaged
