#!/bin/bash
#
# Distruzione automatica istanza vtemessiniegori alla data di fine trial.
# Legge: /var/www/html/vtemessiniegori/trial_settings.php ($trial_expiry_date, Y-m-d).
# Esegue solo se: oggi == data scadenza E ora locale >= 23:59.
#
# CRON root:
#   59 23 * * * /bin/bash /var/www/html/server-ops/mypantarei/vtemessiniegori_destroy_trial.sh >>/var/log/vtemessiniegori-trial-destroy.log 2>&1
#
# MySQL: mysql -u root (socket) oppure /root/.vtemessiniegori-destroy.cnf (chmod 600)

set -uo pipefail

TRIAL_DIR=/var/www/html/vtemessiniegori
SETTINGS="${TRIAL_DIR}/trial_settings.php"
LOG_TAG="vtemessiniegori-trial-destroy"
APACHE_CONF_HTTP=/etc/apache2/sites-available/vtemessiniegori.conf
APACHE_CONF_SSL=/etc/apache2/sites-available/vtemessiniegori-le-ssl.conf
DB_NAME=vtemessiniegori
DB_USER=vtemessiniegori
CERT_NAME=vte-messiniegori.mypantarei.net

log() {
	logger -t "$LOG_TAG" "$*" || true
	echo "$(date -Is) $*"
}

if [ ! -f "$SETTINGS" ]; then
	log "Nessun trial_settings.php: istanza già rimossa o non presente. Uscita."
	exit 0
fi

EXP=$(php -r "require '${SETTINGS}'; echo isset(\$trial_expiry_date) ? trim((string)\$trial_expiry_date) : '';")
if [ -z "$EXP" ]; then
	log "trial_expiry_date vuota: nessuna distruzione programmata."
	exit 0
fi

TODAY=$(date +%Y-%m-%d)
if [ "$TODAY" != "$EXP" ]; then
	exit 0
fi

HM=$(date +%H%M)
if [ "$HM" -lt 2359 ]; then
	exit 0
fi

LOCK=/tmp/vtemessiniegori-trial-destroy.lock
if ! mkdir "$LOCK" 2>/dev/null; then
	log "Lock presente, skip."
	exit 0
fi
trap 'rmdir "$LOCK" 2>/dev/null || true' EXIT

log "INIZIO distruzione istanza vtemessiniegori (data=$EXP)."

mysql_exec() {
	if mysql -u root -e "SELECT 1" &>/dev/null; then
		mysql -u root -e "$1"
	elif [ -f /root/.vtemessiniegori-destroy.cnf ]; then
		mysql --defaults-extra-file=/root/.vtemessiniegori-destroy.cnf -e "$1"
	else
		log "ERRORE: impossibile connettersi a MySQL (root o /root/.vtemessiniegori-destroy.cnf)."
		exit 1
	fi
}

mysql_exec "DROP DATABASE IF EXISTS \`${DB_NAME}\`;"
mysql_exec "DROP USER IF EXISTS '${DB_USER}'@'localhost';" 2>/dev/null || true
mysql_exec "FLUSH PRIVILEGES;" || true

if [ "$(id -u)" -eq 0 ]; then
	a2dissite vtemessiniegori.conf vtemessiniegori-le-ssl.conf 2>/dev/null || true
	rm -f "$APACHE_CONF_HTTP" "$APACHE_CONF_SSL" 2>/dev/null || true
	rm -f /etc/apache2/sites-enabled/vtemessiniegori.conf /etc/apache2/sites-enabled/vtemessiniegori-le-ssl.conf 2>/dev/null || true
else
	log "ATTENZIONE: senza root non posso modificare Apache."
fi

if [ -d "$TRIAL_DIR" ]; then
	rm -rf "$TRIAL_DIR"
	log "Rimossa directory $TRIAL_DIR"
fi

if command -v certbot &>/dev/null; then
	certbot delete --cert-name "$CERT_NAME" --non-interactive 2>/dev/null || true
fi

rm -f /etc/cron.d/vtemessiniegori 2>/dev/null || true

if systemctl is-active --quiet apache2 2>/dev/null; then
	if apache2ctl configtest 2>/dev/null; then
		systemctl reload apache2 && log "Apache ricaricato."
	else
		log "ATTENZIONE: apache2ctl configtest fallito."
		systemctl reload apache2 2>/dev/null || true
	fi
fi

log "FINE distruzione vtemessiniegori."
exit 0
