#!/bin/bash
# pre-push: block push if lint fails.
set -uo pipefail

if [ -n "${CI:-}" ] || [ -n "${GITHUB_ACTIONS:-}" ]; then
  exit 0
fi

cd "$(git rev-parse --show-toplevel 2>/dev/null || echo .)"

if [ -f ".venv/bin/activate" ]; then
  source .venv/bin/activate
fi

errors=""

# Python lint
if [ -d "apple" ]; then
  py_out=$(ruff check apple/ 2>&1)
  if [ $? -ne 0 ]; then
    errors+="=== ruff check apple/ ===\n${py_out}\n\n"
  fi
fi

# Go lint
if [ -f "go.mod" ]; then
  go_out=$(golangci-lint run ./... 2>&1)
  if [ $? -ne 0 ]; then
    errors+="=== golangci-lint run ./... ===\n${go_out}\n\n"
  fi
fi

if [ -n "$errors" ]; then
  echo -e "$errors" >&2
  exit 1
fi

exit 0
