#!/usr/bin/env bash
set -euo pipefail

MOSAIC_HOME="${MOSAIC_HOME:-$HOME/.config/mosaic}"
TARGET_DIR="$(pwd)"

usage() {
  cat <<USAGE
Usage: $(basename "$0") [--target <dir>]

Run quality-rails verification checks inside a target repository.

Examples:
  $(basename "$0")
  $(basename "$0") --target ~/src/my-project
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --target)
      TARGET_DIR="${2:-}"
      shift 2
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unknown argument: $1" >&2
      usage >&2
      exit 1
      ;;
  esac
done

if [[ ! -d "$TARGET_DIR" ]]; then
  echo "[mosaic-quality] Target directory does not exist: $TARGET_DIR" >&2
  exit 1
fi

SCRIPT="$MOSAIC_HOME/tools/quality/scripts/verify.sh"
if [[ ! -x "$SCRIPT" ]]; then
  echo "[mosaic-quality] Missing verify script: $SCRIPT" >&2
  exit 1
fi

echo "[mosaic-quality] Running verification in $TARGET_DIR"
(
  cd "$TARGET_DIR"
  "$SCRIPT"
)
