#!/bin/bash
# account-check <account>: CWP provider: exit 0 when <account> is a CWP hosting account, 1 when
# it is not, 2 on any fault. Run by hostpanel-ssh-key-agent (as root, empty environment, no shell)
# when agent.conf sets account_check_command. Reads root's MySQL credentials from /root/.my.cnf,
# the file CWP itself uses; the name is validated before it reaches the query.
set -euo pipefail
export HOME=/root
u="${1:-}"
[[ "$u" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]] || exit 2
n="$(/usr/bin/mysql --defaults-file=/root/.my.cnf -N -B -e "SELECT COUNT(*) FROM root_cwp.user WHERE username='${u}'" 2>/dev/null)" || exit 2
[ "$n" = "1" ] && exit 0
[ "$n" = "0" ] && exit 1
exit 2
