name: Continuous integration

on:
  push:
    branches:
      - main
    tags:
      - "v*.*.*"
  pull_request:
    branches:
      - main

jobs:

  test:
    name: Test
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Cache cargo assets
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ubuntu-latest-test-cargo-${{ hashFiles('**/Cargo.lock') }}

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

      - name: Run cargo test
        uses: actions-rs/cargo@v1
        with:
          command: test
          args: --workspace

  lint:
    name: Lint
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Cache cargo assets
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ubuntu-latest-lint-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          profile: minimal
          toolchain: stable
          override: true
          components: rustfmt, clippy

      - name: Run cargo check
        uses: actions-rs/cargo@v1
        with:
          command: check
          args: --workspace

      - name: Run cargo fmt
        uses: actions-rs/cargo@v1
        with:
          command: fmt
          args: --all -- --check

      - name: Run cargo clippy
        uses: actions-rs/cargo@v1
        with:
          command: clippy
          args: -- -W warnings

  build:
    if: startsWith(github.event.ref, 'refs/tags')
    name: Build ${{ matrix.arch }}
    needs: [test, lint]
    strategy:
      matrix:
        include:
          - arch: x86_64-apple-darwin
            os: macos-latest
            file: surreal-${{ github.ref_name }}.darwin-amd64
          - arch: aarch64-apple-darwin
            os: macos-latest
            file: surreal-${{ github.ref_name }}.darwin-arm64
          - arch: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            file: surreal-${{ github.ref_name }}.linux-amd64
          - arch: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            file: surreal-${{ github.ref_name }}.linux-arm64
          # - arch: x86_64-pc-windows-gnu
          #   os: windows-latest
          #   file: surreal-${{ github.ref_name }}.windows-amd64
    runs-on: ${{ matrix.os }}
    steps:

      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Cache cargo assets
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ matrix.arch }}-build-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Prepare environment
        if: contains(matrix.arch, 'windows') && endsWith(matrix.arch, '-gnu')
        run: echo "C:\msys64\usr\bin;$Env:Path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8

      - name: Install dependencies
        if: contains(matrix.arch, 'windows') && endsWith(matrix.arch, '-msvc')
        run: |
          vcpkg integrate install
          vcpkg install openssl:x64-windows-static

      - name: Install dependencies
        if: contains(matrix.arch, 'linux') && endsWith(matrix.arch, '-gnu')
        run: |
          sudo apt-get -y update
          sudo apt-get -y install musl-tools qemu-user libc6-dev-arm64-cross
          sudo apt-get -y install g++-aarch64-linux-gnu gcc-aarch64-linux-gnu

      - name: Install stable toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true
          target: ${{ matrix.arch }}

      - name: Output package versions
        run: go version ; cargo version ; rustc --version ; cmake --version ; gcc --version ; g++ --version ; perl -v

      - name: Run cargo build
        uses: actions-rs/cargo@v1
        with:
          command: build
          args: --release --locked --target ${{ matrix.arch }}

      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: us-east-2
          aws-access-key-id: ${{ secrets.AMAZON_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.AMAZON_SECRET_KEY }}

      - name: Package binaries
        if: ${{ !contains(matrix.arch, 'windows') }}
        shell: bash
        run: |
          cd target/${{ matrix.arch }}/release
          chmod +x surreal
          tar -zcvf ${{ matrix.file }}.tgz surreal
          echo $(shasum -a 256 ${{ matrix.file }}.tgz | cut -f1 -d' ') > ${{ matrix.file }}.txt
          aws s3 cp --cache-control 'no-store' ${{ matrix.file }}.tgz s3://download.surrealdb.com/${{ github.ref_name }}/
          aws s3 cp --cache-control 'no-store' ${{ matrix.file }}.txt s3://download.surrealdb.com/${{ github.ref_name }}/
          cd -

      - name: Package binaries
        if: ${{ contains(matrix.arch, 'windows') }}
        shell: bash
        run: |
          cd target/${{ matrix.arch }}/release
          cp surreal.exe ${{ matrix.file }}.exe
          echo $(shasum -a 256 ${{ matrix.file }}.exe | cut -f1 -d' ') > ${{ matrix.file }}.txt
          aws s3 cp --cache-control 'no-store' ${{ matrix.file }}.exe s3://download.surrealdb.com/${{ github.ref_name }}/
          aws s3 cp --cache-control 'no-store' ${{ matrix.file }}.txt s3://download.surrealdb.com/${{ github.ref_name }}/
          cd -

      - name: Upload artifacts
        uses: actions/upload-artifact@v2
        with:
          name: ${{ matrix.file }}
          path: |
            target/${{ matrix.arch }}/release/surreal
            target/${{ matrix.arch }}/release/${{ matrix.file }}.tgz
            target/${{ matrix.arch }}/release/${{ matrix.file }}.txt
            target/${{ matrix.arch }}/release/${{ matrix.file }}.exe

  package:
    if: startsWith(github.event.ref, 'refs/tags')
    name: Package macOS
    needs: [build]
    runs-on: macos-latest
    steps:

      - name: Download amd64 binary
        uses: actions/download-artifact@v3
        with:
          name: surreal-${{ github.ref_name }}.darwin-amd64
          path: amd64

      - name: Download arm64 binary
        uses: actions/download-artifact@v3
        with:
          name: surreal-${{ github.ref_name }}.darwin-arm64
          path: arm64

      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: us-east-2
          aws-access-key-id: ${{ secrets.AMAZON_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.AMAZON_SECRET_KEY }}

      - name: Package universal binary
        shell: bash
        run: |
          FILE="surreal-${{ github.ref_name }}.darwin-universal"
          lipo -create -output surreal amd64/surreal arm64/surreal
          chmod +x surreal
          tar -zcvf $FILE.tgz surreal
          echo $(shasum -a 256 $FILE.tgz | cut -f1 -d' ') > $FILE.txt
          aws s3 cp --cache-control 'no-store' $FILE.tgz s3://download.surrealdb.com/${{ github.ref_name }}/
          aws s3 cp --cache-control 'no-store' $FILE.txt s3://download.surrealdb.com/${{ github.ref_name }}/

  deploy:
    if: startsWith(github.event.ref, 'refs/tags')
    name: Deploy
    needs: [package]
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Configure AWS
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-region: us-east-2
          aws-access-key-id: ${{ secrets.AMAZON_ACCESS_KEY }}
          aws-secret-access-key: ${{ secrets.AMAZON_SECRET_KEY }}

      - name: Download artifacts
        uses: actions/download-artifact@v3

      - name: Publish release
        uses: softprops/action-gh-release@v1
        with:
          name: "Release ${{ github.ref_name }}"
          files: |
            LICENSE
            **/*.tgz
            **/*.exe

      - name: Set version
        shell: bash
        run: |
          echo ${{ github.ref_name }} > latest.txt
          aws s3 cp --cache-control 'no-store' latest.txt s3://download.surrealdb.com/

      - name: Distribute binaries
        shell: bash
        run: |
          regions=("af-south-1" "ap-east-1" "ap-south-1" "ap-southeast-1" "ap-southeast-2" "ca-central-1" "eu-central-1" "eu-west-2" "me-south-1" "sa-east-1" "us-west-2")
          for region in ${regions[@]}; do
              aws s3 sync --delete --storage-class INTELLIGENT_TIERING --source-region eu-west-2 --region ${region} s3://download.surrealdb.com s3://download.${region}.surrealdb.com
          done

  docker:
    if: startsWith(github.event.ref, 'refs/tags')
    name: Docker
    needs: [build]
    runs-on: ubuntu-latest
    steps:

      - name: Checkout sources
        uses: actions/checkout@v2

      - name: Download amd64 binary
        uses: actions/download-artifact@v3
        with:
          name: surreal-${{ github.ref_name }}.linux-amd64
          path: amd64

      - name: Download arm64 binary
        uses: actions/download-artifact@v3
        with:
          name: surreal-${{ github.ref_name }}.linux-arm64
          path: arm64

      - name: Set file permissions
        shell: bash
        run: |
          chmod +x amd64/surreal arm64/surreal

      - name: Configure DockerHub
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKER_USER }}
          password: ${{ secrets.DOCKER_PASS }}

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Buildx
        uses: docker/setup-buildx-action@v2

      - name: Configure tag
        shell: bash
        run: |
          VERSION=${{ github.ref_name }}
          echo "VERSION=${VERSION:1}" >> $GITHUB_ENV

      - name: Build and Push
        uses: docker/build-push-action@v3
        with:
          context: .
          push: true
          platforms: linux/amd64,linux/arm64
          tags: surrealdb/surrealdb:latest,surrealdb/surrealdb:${{ env.VERSION }}