#!/bin/bash
# Cron VTE + permessi vte_updater per vte.ego.it
# Uso: sudo bash /var/www/html/server-ops/ego/ego-install-cron-php83-and-updater-perms.sh

set -euo pipefail

if [[ "${EUID:-0}" -ne 0 ]]; then
	echo "Esegui come root: sudo bash $0" >&2
	exit 1
fi

VTE="/var/www/html/vte23083_2711"
OPS="/var/www/html/server-ops/ego"

chmod 0755 "${OPS}/ego-vteUpdater-php83.sh" 2>/dev/null || true
chmod 0755 "${VTE}/cron/RunCron.sh" 2>/dev/null || true

chown -R www-data:www-data "$VTE/vte_updater"
chmod -R u+rwX,g+rwX "$VTE/vte_updater"

CRON_TMP="$(mktemp)"
(
	crontab -l 2>/dev/null \
		| grep -vF '# VTENEXT EGO' \
		| grep -vF '/var/www/html/vte23083_2711/cron/RunCron.sh' \
		|| true
	echo '# VTENEXT EGO — RunCron.sh con PHP 8.3'
	echo "* * * * * /usr/bin/env USE_PHP=/usr/bin/php8.3 ${VTE}/cron/RunCron.sh >>${VTE}/logs/cron.log 2>&1"
) >"$CRON_TMP"
crontab "$CRON_TMP"
rm -f "$CRON_TMP"

echo "[OK] Cron e permessi updater configurati."
