name: "Build and test"
on:
  push:
    branches:
      - master
  pull_request:

jobs:
  build:
    runs-on: ubuntu-latest-personal
    steps:
      - uses: actions/checkout@v3

      - name: Install latest nightly toolchain
        uses: actions-rs/toolchain@v1
        with:
            toolchain: nightly
            override: true

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Build
        run: cargo build --all-features --verbose --release

  check:
    runs-on: ubuntu-latest-personal
    steps:
      - uses: actions/checkout@v3

      - name: Install latest nightly toolchain
        uses: actions-rs/toolchain@v1
        with:
            toolchain: nightly
            override: true
            components: rustfmt, clippy

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Check code format
        run: cargo fmt -- --check

      - name: Check clippy
        run: cargo clippy --all-features -- --deny warnings

  test:
    runs-on: ubuntu-latest-personal
    steps:
      - uses: actions/checkout@v3
      - uses: cargo-bins/cargo-binstall@main

      - name: Install mpv
        run: apt-get update && apt-get install -y mpv

      - name: Install latest nightly toolchain
        uses: actions-rs/toolchain@v1
        with:
            toolchain: nightly
            override: true
            components: llvm-tools-preview

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Create necessary directories
        run: mkdir -p target/test-report

      - name: Run tests
        run: |
          cargo test --all-features --release --no-fail-fast -- -Zunstable-options --format json --report-time \
            | tee target/test-report/test-report.json
        env:
          RUSTFLAGS: "-Cinstrument-coverage"
          LLVM_PROFILE_FILE: "target/coverage/%p-%m.profraw"

      - name: Install markdown-test-report
        run: cargo binstall -y markdown-test-report

      - name: Generate test report
        run: markdown-test-report target/test-report/test-report.json --output target/test-report/test-report.md

      - name: Install rsync
        run: apt-get update && apt-get install -y rsync

      - name: Install SSH key
        run: |
          mkdir -p ~/.ssh
          cat <<EOF >~/.ssh/key
          ${{ secrets.OYSTEIKT_GITEA_WEBDOCS_SSH_KEY }}
          EOF
          chmod 600 ~/.ssh/key

      # - name: Upload test report
      #   uses: actions/upload-artifact@v3
      #   with:
      #     name: test-report.md
      #     path: target/test-report/test-report.md

      - name: Upload test report
        run: |
          rsync \
            --archive \
            --compress \
            --verbose \
            --mkpath \
            --rsh="ssh -oBatchMode=yes -oStrictHostKeyChecking=accept-new -i ~/.ssh/key" \
            "target/doc/" \
            "oysteikt@microbel.pvv.ntnu.no:mpvipc/${{ gitea.ref_name }}/coverage/"

      - name: Install grcov
        run: cargo binstall -y grcov

      - name: Generate coverage report
        run: |
          grcov \
            --source-dir . \
            --binary-path ./target/release/deps/ \
            --excl-start 'mod test* \{' \
            --ignore 'tests/*' \
            --ignore "*test.rs" \
            --ignore "*tests.rs" \
            --ignore "*github.com*" \
            --ignore "*libcore*" \
            --ignore "*rustc*" \
            --ignore "*liballoc*" \
            --ignore "*cargo*" \
            -t html \
            -o ./target/coverage/html \
            target/coverage/

      # - name: Upload coverage report
      #   uses: actions/upload-artifact@v3
      #   with:
      #     name: coverage
      #     path: target/coverage/html

      - name: Upload coverage report
        run: |
          rsync \
            --archive \
            --compress \
            --verbose \
            --mkpath \
            --rsh="ssh -oBatchMode=yes -oStrictHostKeyChecking=accept-new -i ~/.ssh/key" \
            "target/doc/" \
            "oysteikt@microbel.pvv.ntnu.no:mpvipc/${{ gitea.ref_name }}/coverage/"

  docs:
    runs-on: ubuntu-latest-personal
    steps:
      - uses: actions/checkout@v3

      - name: Install latest nightly toolchain
        uses: actions-rs/toolchain@v1
        with:
            toolchain: nightly
            override: true

      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2

      - name: Build docs
        run: cargo doc --all-features --document-private-items --release

      - name: Transfer files
        uses: https://git.pvv.ntnu.no/oysteikt/rsync-action@main
        with:
          source: target/doc/
          target: mpvipc/${{ gitea.ref_name }}/docs/
          username: oysteikt
          ssh-key: ${{ secrets.OYSTEIKT_GITEA_WEBDOCS_SSH_KEY }}
          host: microbel.pvv.ntnu.no
          known-hosts: "microbel.pvv.ntnu.no ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBEq0yasKP0mH6PI6ypmuzPzMnbHELo9k+YB5yW534aKudKZS65YsHJKQ9vapOtmegrn5MQbCCgrshf+/XwZcjbM="

    # ssh-key: ${{ secrets.SSH_KEY }}
    #   - name: Install rsync
    #     run: apt-get update && apt-get install -y rsync

    #   - name: Install SSH key
    #     run: |
    #       mkdir -p ~/.ssh
    #       cat <<EOF >~/.ssh/key
    #       ${{ secrets.OYSTEIKT_GITEA_WEBDOCS_SSH_KEY }}
    #       EOF
    #       chmod 600 ~/.ssh/key

    #   - name: Deploy docs
    #     run: |
    #       rsync \
    #         --archive \
    #         --compress \
    #         --verbose \
    #         --mkpath \
    #         --rsh="ssh -oBatchMode=yes -oStrictHostKeyChecking=accept-new -i ~/.ssh/key" \
    #         "target/doc/" \
    #         "oysteikt@microbel.pvv.ntnu.no:mpvipc/${{ gitea.ref_name }}/docs/"