#!/usr/bin/env bash
# One-time setup of a fresh Ubuntu 24.04 VPS for Lettrine. Run as root:
#   bash provision.sh
# Expects DNS for scrabble.prestaedit.app to point at this machine already.
set -euo pipefail

DOMAIN=scrabble.prestaedit.app
APP_DIR=/var/www/lettrine
REPO=git@github.com:PrestaEdit/scrabble.git
EMAIL=${CERTBOT_EMAIL:?"export CERTBOT_EMAIL=ton@email avant de lancer"}

echo "==> Paquets"
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
    nginx supervisor git unzip curl ufw certbot python3-certbot-nginx sqlite3 \
    php8.3-fpm php8.3-cli php8.3-sqlite3 php8.3-mbstring php8.3-xml php8.3-curl \
    php8.3-zip php8.3-intl php8.3-bcmath

if ! command -v composer >/dev/null; then
    echo "==> Composer"
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
fi

echo "==> Pare-feu"
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw --force enable

echo "==> Clé de déploiement GitHub (lecture seule)"
install -d -o www-data -g www-data -m 700 /var/www/.ssh
if [ ! -f /var/www/.ssh/id_ed25519 ]; then
    sudo -u www-data ssh-keygen -t ed25519 -N '' -C "lettrine-vps" -f /var/www/.ssh/id_ed25519
    sudo -u www-data sh -c 'ssh-keyscan github.com >> /var/www/.ssh/known_hosts'
fi
echo
echo "Ajoute cette clé dans GitHub → PrestaEdit/scrabble → Settings → Deploy keys (lecture seule) :"
cat /var/www/.ssh/id_ed25519.pub
read -rp "Appuie sur Entrée une fois la clé ajoutée… "

echo "==> Code"
install -d -o www-data -g www-data "$APP_DIR"
if [ ! -d "$APP_DIR/.git" ]; then
    sudo -u www-data git clone "$REPO" "$APP_DIR"
fi
cd "$APP_DIR/server"

if [ ! -f .env ]; then
    echo "==> .env de production"
    sudo -u www-data cp deploy/env.production .env
    sudo -u www-data sed -i "s|^REVERB_APP_KEY=.*|REVERB_APP_KEY=$(openssl rand -hex 16)|" .env
    sudo -u www-data sed -i "s|^REVERB_APP_SECRET=.*|REVERB_APP_SECRET=$(openssl rand -hex 32)|" .env
    sudo -u www-data touch database/database.sqlite
    sudo -u www-data composer install --no-dev --optimize-autoloader --no-interaction
    sudo -u www-data php artisan key:generate --force
fi

echo "==> Nginx"
cp deploy/nginx.conf /etc/nginx/sites-available/lettrine
ln -sf /etc/nginx/sites-available/lettrine /etc/nginx/sites-enabled/lettrine
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx

echo "==> HTTPS"
certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos -m "$EMAIL" --redirect

echo "==> Reverb (supervisor)"
cp deploy/reverb.conf /etc/supervisor/conf.d/lettrine-reverb.conf
supervisorctl reread && supervisorctl update

echo "==> Premier déploiement"
bash deploy/deploy.sh
sudo -u www-data php artisan dictionary:import

echo
echo "Terminé. Crée maintenant les joueurs :"
echo "  cd $APP_DIR/server && sudo -u www-data php artisan player:create Jonathan ton@email.fr --color=#FFD166"
echo "  cd $APP_DIR/server && sudo -u www-data php artisan player:create Florence son@email.fr --color=#B9A2FF"
echo
echo "Clé Reverb à reporter dans mobile/.env (PUSHER_APP_KEY) :"
grep '^REVERB_APP_KEY=' .env
