name: Build - Linux

on:
  workflow_dispatch:
  # push:
  #   tags:
  #     - v*

env:
  ACTIONS_ALLOW_UNSECURE_COMMANDS: true

jobs:
  build-linux:
    runs-on: ubuntu-20.04
    strategy:
      matrix:
        node-version: [ 16.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Build
        run: npm install --build-from-source
        env:
          CI: true
      - name: Test
        run: npm run test
        env:
          CI: true
      - name: Upload
        run: node_modules/.bin/prebuild -r napi --upload -u ${{ secrets.GITHUB_TOKEN }}
        env:
          CI: true

  build-linux-armv7:
    runs-on: ubuntu-20.04

    strategy:
      matrix:
        node-version: [16.x]
    steps:
      - uses: actions/checkout@v2
      - name: Prepare Cross Compile
        run: |
          sudo apt update
          sudo apt install -y g++-arm-linux-gnueabihf gcc-arm-linux-gnueabihf
          mkdir sysroot && cd sysroot
          wget https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/ef5c4f84bcafb7a3796d36bb1db7826317dde51c/debian_sid_arm_sysroot.tar.xz
          tar xf debian_sid_arm_sysroot.tar.xz
          echo "ARM_SYSROOT=$(pwd)" >> $GITHUB_ENV
          ls -l
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Build & Upload
        run: |
          npm install --ignore-scripts
          node_modules/.bin/prebuild -r napi --backend cmake-js --arch arm --upload -u ${{ secrets.GITHUB_TOKEN }} -- --CDCMAKE_TOOLCHAIN_FILE:FILEPATH=./cmake/toolchain/armv7.cmake
        env:
          CI: true

  build-linux-arm64:
    runs-on: ubuntu-20.04

    strategy:
      matrix:
        node-version: [16.x]
    steps:
      - uses: actions/checkout@v2
      - name: Prepare Cross Compile
        run: |
          sudo apt update
          sudo apt install -y g++-aarch64-linux-gnu gcc-aarch64-linux-gnu
          mkdir sysroot && cd sysroot
          wget https://commondatastorage.googleapis.com/chrome-linux-sysroot/toolchain/953c2471bc7e71a788309f6c2d2003e8b703305d/debian_sid_arm64_sysroot.tar.xz
          tar xf debian_sid_arm64_sysroot.tar.xz
          echo "ARM64_SYSROOT=$(pwd)" >> $GITHUB_ENV
          ls -l
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - name: Build & Upload
        run: |
          npm install --ignore-scripts
          node_modules/.bin/prebuild -r napi --backend cmake-js --arch arm64 --upload -u ${{ secrets.GITHUB_TOKEN }} -- --CDCMAKE_TOOLCHAIN_FILE:FILEPATH=./cmake/toolchain/arm64.cmake
        env:
          CI: true

  # npm-publish:
  #   needs: [build-linux, build-linux-armv7, build-linux-arm64]
  #   name: npm-publish
  #   runs-on: ubuntu-latest
  #   steps:
  #     - name: Checkout repository
  #       uses: actions/checkout@v2
  #     - name: Set up Node.js
  #       uses: actions/setup-node@v1
  #       with:
  #         node-version: 16.x
  #         registry-url: "https://registry.npmjs.org"
  #     - name: Publish
  #       run: |
  #         npm install
  #         npm publish
  #       env:
  #         NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
