#!/bin/bash
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2022 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for Swift project authors
#

set -eu

# A `realpath` alternative using the default C implementation.
filepath() {
  [[ $1 = /* ]] && echo "$1" || echo "$PWD/${1#./}"
}

# First get the absolute path to this file so we can get the
# absolute file path to the Swift-DocC-Plugin root source dir.
SWIFT_DOCC_PLUGIN_ROOT="$(dirname $(dirname $(filepath $0)))"

SWIFT_SKIP_BUILDING_UPSTREAM_DOCC=${SWIFT_SKIP_BUILDING_UPSTREAM_DOCC:=false}

if [[ $SWIFT_SKIP_BUILDING_UPSTREAM_DOCC != true ]]
then
  SWIFT_DOCC_ROOT="$SWIFT_DOCC_PLUGIN_ROOT/swift-docc"
  SWIFT_DOCC_RENDER_ARTIFACT_ROOT="$SWIFT_DOCC_PLUGIN_ROOT/swift-docc-render-artifact"
  export DOCC_HTML_DIR="$SWIFT_DOCC_RENDER_ARTIFACT_ROOT/dist"

  SWIFT_DOCC_REPO=${SWIFT_DOCC_REPO:="https://github.com/apple/swift-docc.git"}
  SWIFT_DOCC_RENDER_ARTIFACT_REPO=${SWIFT_DOCC_RENDER_ARTIFACT_REPO:="https://github.com/apple/swift-docc-render-artifact.git"}

  SWIFT_DOCC_BRANCH=${SWIFT_DOCC_BRANCH:="main"}
  SWIFT_DOCC_RENDER_ARTIFACT_BRANCH=${SWIFT_DOCC_RENDER_ARTIFACT_BRANCH:="main"}

  # The script will clone swift-docc and swift-docc-render-artifact at the
  # branches pulled from the environment above.  The tests will then run using
  # that built DocC. This can be useful for testing interdependent changes that
  # need to land together and make it possible to test multiple pull requests
  # together.

  echo "Cloning docc..."
  rm -rf "$SWIFT_DOCC_ROOT"
  git clone -b "$SWIFT_DOCC_BRANCH" "${SWIFT_DOCC_REPO}" "$SWIFT_DOCC_ROOT" || exit 1

  echo "Cloning docc-render-artifact..."
  rm -rf "$SWIFT_DOCC_RENDER_ARTIFACT_ROOT"
  git clone -b "${SWIFT_DOCC_RENDER_ARTIFACT_BRANCH}" "${SWIFT_DOCC_RENDER_ARTIFACT_REPO}" "$SWIFT_DOCC_RENDER_ARTIFACT_ROOT" || exit 1

  echo "Building docc..."
  swift build --package-path "$SWIFT_DOCC_ROOT" --product docc --configuration release || exit 1

  export DOCC_EXEC="$(swift build --package-path "$SWIFT_DOCC_ROOT" --show-bin-path --configuration release)/docc"
  if [[ ! -f "$DOCC_EXEC" ]]; then
    echo "docc executable not found, expected at $DOCC_EXEC"
    exit 1
  else
    echo "Using docc executable: $DOCC_EXEC"
  fi
fi
# Build and test Swift-DocC Plugin
swift test --parallel --package-path "$SWIFT_DOCC_PLUGIN_ROOT"

# Build and test Swift-DocC Plugin integration tests
# We use a shared cache when building the plugin so these tests shouldn't be run in parallel.
swift test --package-path "$SWIFT_DOCC_PLUGIN_ROOT/IntegrationTests"

# Run source code checks for the codebase.
LC_ALL=C "$SWIFT_DOCC_PLUGIN_ROOT"/bin/check-source

printf "\033[0;32mokay.\033[0m\n"
