#!/bin/bash
set -euo pipefail
SKIP_GIT_AUTHOR=false
for arg in "$@"; do
case "$arg" in
--skip-git-author) SKIP_GIT_AUTHOR=true ;;
esac
done
check_token() {
if [ -n "${GITCODE_TOKEN:-}" ]; then
printf '{"item":"token","status":"pass","detail":"env GITCODE_TOKEN (length=%d)","source":"env"}' "${#GITCODE_TOKEN}"
else
printf '{"item":"token","status":"fail","detail":"GITCODE_TOKEN not set"}'
fi
}
check_binary() {
local cmd="$1" label="${2:-$1}"
if command -v "$cmd" >/dev/null 2>&1; then
local ver
ver=$("$cmd" --version 2>&1 | head -1 || true)
printf '{"item":"%s","status":"pass","detail":"%s"}' "$label" "$ver"
else
printf '{"item":"%s","status":"fail","detail":"%s not found"}' "$label" "$cmd"
fi
}
check_tmp() {
if [ -w /tmp ]; then
printf '{"item":"tmp","status":"pass","detail":"writable"}'
else
printf '{"item":"tmp","status":"fail","detail":"not writable"}'
fi
}
check_git_author() {
if [ "$SKIP_GIT_AUTHOR" = true ]; then
printf '{"item":"git_author","status":"skip","detail":"--skip-git-author"}'
return
fi
local name email
name=$(git config --global user.name 2>/dev/null || true)
email=$(git config --global user.email 2>/dev/null || true)
if [ -n "$name" ] && [ -n "$email" ]; then
printf '{"item":"git_author","status":"pass","detail":"%s <%s>","source":"global"}' "$name" "$email"
else
printf '{"item":"git_author","status":"fail","detail":"global user.name/email not configured"}'
fi
}
RESULTS="["
RESULTS+="$(check_token),"
RESULTS+="$(check_binary git),"
RESULTS+="$(check_binary curl),"
RESULTS+="$(check_binary python3 python3),"
RESULTS+="$(check_tmp),"
RESULTS+="$(check_git_author)"
RESULTS+="]"
FAIL_COUNT=$(echo "$RESULTS" | python3 -c "import json,sys; print(sum(1 for r in json.load(sys.stdin) if r['status']=='fail'))")
ALL_COUNT=$(echo "$RESULTS" | python3 -c "import json,sys; print(len(json.load(sys.stdin)))")
PASS_COUNT=$((ALL_COUNT - FAIL_COUNT))
printf '{"results":%s,"summary":{"pass":%d,"fail":%d,"total":%d}}\n' \
"$RESULTS" "$PASS_COUNT" "$FAIL_COUNT" "$ALL_COUNT"
exit "$FAIL_COUNT"