#!/bin/sh
#
# shcp - CLI wrapper for SHCP Symfony console commands
#
# Usage:
#   shcp dns:check-sync
#   shcp email:bounce-stats --json
#   shcp ssl:renew --dry-run
#   shcp list                        # list all shcp: commands
#

set -e

SHCPD=/usr/sbin/shcpd
CONSOLE=/opt/shcp/bin/console

# SC-355: the Symfony kernel selects its cache namespace by EUID - root gets
# var/root-cache/, everyone else var/cache/. The panel daemon runs as user
# 'shcp', so a console command run as root (the usual `ssh root@box shcp <cmd>`)
# rebuilds a namespace the live panel never reads: it succeeds, changes nothing,
# and has already sent one investigation chasing a command that looked "missing"
# from a stale root-cache (shcp-base#1241). Warn loudly, then still run - the
# root path is legitimate for backups, cert install, setquota and user creation.
# Set SHCP_ALLOW_ROOT=1 to silence this for those callers / non-interactive use.
# Closed command-by-command classification is deferred to shcp-build#269.
if [ "$(id -u)" = 0 ] && [ "${SHCP_ALLOW_ROOT:-}" != 1 ]; then
	printf '%s\n' \
		'WARNING: running shcp as root targets the var/root-cache namespace.' \
		'         The live panel runs as user shcp and reads var/cache - this' \
		'         command will NOT affect it. To manage the panel, run:' \
		'             sudo -u shcp shcp <cmd>' \
		'         Set SHCP_ALLOW_ROOT=1 to silence this for root-required commands.' >&2
fi

if [ $# -eq 0 ] || [ "$1" = "list" ]; then
	exec "$SHCPD" php-cli "$CONSOLE" list shcp
fi

cmd="$1"
shift
exec "$SHCPD" php-cli "$CONSOLE" "shcp:$cmd" "$@"
