name: Master

on:
  push:
    branches:
      - master

env:
  # Run all cargo commands with --verbose.
  CARGO_TERM_VERBOSE: true
  RUST_BACKTRACE: 1

jobs:
  build:
    name: Build
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-gnu
          - os: windows-latest
            target: x86_64-pc-windows-gnu
          - os: macos-latest
            target: x86_64-apple-darwin
    steps:
      - uses: actions/checkout@v2

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Cache.
      - name: Cargo cache registry, index and build
        uses: actions/cache@v2.1.4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-cache-${{ hashFiles('**/Cargo.lock') }}

      # Build
      - name: Build
        run: cargo build --release
      
      # copy artifacts to new artifacts directory
      - shell: bash
        run: |
          [[ -d "artifacts" ]] && rm -rf artifacts
          mkdir artifacts
          find "target/release" -maxdepth 1 -type f -exec cp '{}' artifacts \;
          ls -al artifacts

      - uses: actions/upload-artifact@master
        with:
          name: sn_client-${{ matrix.target }}-prod
          path: artifacts

  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    needs: [build]
    env:
      AWS_ACCESS_KEY_ID: AKIAVVODCRMSJ5MV63VB
      AWS_SECRET_ACCESS_KEY: ${{ secrets.DEPLOY_USER_SECRET_ACCESS_KEY }}
      AWS_DEFAULT_REGION: eu-west-2
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      COMMIT_MESSAGE: ${{ github.event.head_commit.message }}

    steps:
      # Checkout and get all the artifacts built in the previous jobs.
      - uses: actions/checkout@v2
      
      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      - uses: actions/download-artifact@master
        with:
          name: sn_client-x86_64-pc-windows-gnu-prod
          path: artifacts/prod/x86_64-pc-windows-gnu/release

      - uses: actions/download-artifact@master
        with:
          name: sn_client-x86_64-unknown-linux-gnu-prod
          path: artifacts/prod/x86_64-unknown-linux-gnu/release

      - uses: actions/download-artifact@master
        with:
          name: sn_client-x86_64-apple-darwin-prod
          path: artifacts/prod/x86_64-apple-darwin/release

      # On version change create `deploy` directory and put the artifacts into tar.gz/zip archives for upload
      - name: Create release `zip and .tar.gz files
        if: "startsWith(env.COMMIT_MESSAGE, 'chore(release):')"
        run: |
          client_version=$(grep "^version" < Cargo.toml | head -n 1 | awk '{ print $3 }' | sed 's/\"//g')
          echo "::set-output name=client_version::$client_version"
          ls .
          rm -f *.tar
          rm -rf deploy
          mkdir -p deploy/prod


          zip -j sn_client-${client_version}-x86_64-unknown-linux-gnu.zip \
          artifacts/prod/x86_64-unknown-linux-gnu/release/libsn_client.rlib

          zip -j sn_client-latest-x86_64-unknown-linux-gnu.zip \
          artifacts/prod/x86_64-unknown-linux-gnu/release/libsn_client.rlib

          zip -j sn_client-${client_version}-x86_64-pc-windows-gnu.zip \
          artifacts/prod/x86_64-pc-windows-gnu/release/libsn_client.rlib

          zip -j sn_client-latest-x86_64-pc-windows-gnu.zip \
          artifacts/prod/x86_64-pc-windows-gnu/release/libsn_client.rlib

          zip -j sn_client-${client_version}-x86_64-apple-darwin.zip \
          artifacts/prod/x86_64-apple-darwin/release/libsn_client.rlib

          zip -j sn_client-latest-x86_64-apple-darwin.zip \
          artifacts/prod/x86_64-apple-darwin/release/libsn_client.rlib


          tar -C artifacts/prod/x86_64-unknown-linux-gnu/release \
          -zcvf sn_client-${client_version}-x86_64-unknown-linux-gnu.tar.gz libsn_client.rlib

          tar -C artifacts/prod/x86_64-pc-windows-gnu/release \
          -zcvf sn_client-${client_version}-x86_64-pc-windows-gnu.tar.gz libsn_client.rlib

          tar -C artifacts/prod/x86_64-apple-darwin/release \
          -zcvf sn_client-${client_version}-x86_64-apple-darwin.tar.gz libsn_client.rlib


          mv *.tar.gz deploy/prod
          mv *.zip deploy/prod

          echo ""
          echo "List contents of deploy/prod/ :"
          ls -al deploy/prod

      # Upload all the release archives to S3.
      - name: Upload to S3
        if: "startsWith(env.COMMIT_MESSAGE, 'chore(release):')"
        run: aws s3 sync deploy/prod s3://sn-client --acl public-read

  # Automatic publish, triggered by a commit starting with "chore(release):".
  publish:
    name: Publish
    needs: [deploy]
    runs-on: ubuntu-latest
    # Is this a version change commit?
    if: "startsWith(github.event.head_commit.message, 'chore(release):')"
    steps:
      - uses: actions/checkout@v2
      # checkout with fetch-depth: '0' to be sure to retrieve all commits to look for the semver commit message
        with:
          fetch-depth: '0'

      # Install Rust
      - uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true

      # Publish to crates.io.
      - name: Cargo Login
        run: cargo login ${{ secrets.CRATES_IO_TOKEN }}

      - name: Cargo Publish
        run: cargo publish
