#!/bin/sh
#
# valkey-healthcheck — exit 0 if the server answers PING, 1 otherwise.
#
# Designed for container HEALTHCHECK, Kubernetes liveness/readiness
# probes, and monitoring agents. Honours the same env vars as
# valkey-cli so it can target TLS, auth, sockets, alternate hosts, etc.
#
# Examples:
#   valkey-healthcheck
#   VALKEY_HOST=127.0.0.1 VALKEY_PORT=6380 valkey-healthcheck
#   valkey-healthcheck -s /run/valkey/valkey-server.sock
#   REDISCLI_AUTH=secret valkey-healthcheck --tls
#
# Any extra args are forwarded to valkey-cli verbatim.

set -eu

HOST="${VALKEY_HOST:-127.0.0.1}"
PORT="${VALKEY_PORT:-6379}"

REPLY=$(valkey-cli -h "${HOST}" -p "${PORT}" "$@" PING 2>/dev/null || true)

case "${REPLY}" in
    PONG) exit 0 ;;
    *)    echo "valkey-healthcheck: no PONG reply (got: ${REPLY:-<empty>})" >&2
          exit 1 ;;
esac
