#!/usr/bin/env bash
# =====================================================================
# Installation « de zéro » sur un VPS OVH Ubuntu (22.04 / 24.04).
# À lancer en root (ou sudo) sur le VPS fraîchement provisionné.
#
#   curl -fsSL https://raw.githubusercontent.com/AlexandreHandivia/sauvermonentreprise/main/deploy/bootstrap.sh | bash -s -- sauvermonentreprise.fr
#
# (ou : cloner le dépôt puis ./deploy/bootstrap.sh sauvermonentreprise.fr)
#
# Met en place : PHP 8.3, Composer, Node 22, Nginx, l'application, SQLite,
# le build des assets, les droits, et propose l'obtention du certificat TLS.
# Base par défaut : SQLite (zéro config, adapté à ce site de contenu). Pour
# PostgreSQL, basculer .env sur le bloc « STACK PRODUCTION » après coup.
# =====================================================================
set -euo pipefail

DOMAIN="${1:-sauvermonentreprise.fr}"
REPO="https://github.com/AlexandreHandivia/sauvermonentreprise.git"
APP_DIR="/var/www/sauvermonentreprise"
PHP_VER="8.3"

echo "→ Domaine : ${DOMAIN}"
echo "→ Dossier : ${APP_DIR}"

echo "→ Paquets système"
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get install -y software-properties-common curl git unzip ca-certificates
add-apt-repository -y ppa:ondrej/php || true
apt-get update -y
apt-get install -y nginx certbot python3-certbot-nginx \
    php${PHP_VER}-fpm php${PHP_VER}-cli php${PHP_VER}-sqlite3 php${PHP_VER}-pgsql \
    php${PHP_VER}-mbstring php${PHP_VER}-xml php${PHP_VER}-curl php${PHP_VER}-intl \
    php${PHP_VER}-zip php${PHP_VER}-gd php${PHP_VER}-bcmath

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

echo "→ Node 22"
if ! command -v node >/dev/null 2>&1; then
    curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
    apt-get install -y nodejs
fi

echo "→ Récupération du code"
mkdir -p "$(dirname "$APP_DIR")"
if [ -d "$APP_DIR/.git" ]; then
    git -C "$APP_DIR" pull --ff-only origin main
else
    git clone "$REPO" "$APP_DIR"
fi
cd "$APP_DIR"

echo "→ Environnement"
if [ ! -f .env ]; then
    cp .env.example .env
    sed -i "s#^APP_ENV=.*#APP_ENV=production#" .env
    sed -i "s#^APP_DEBUG=.*#APP_DEBUG=false#" .env
    sed -i "s#^APP_URL=.*#APP_URL=https://${DOMAIN}#" .env
fi
touch database/database.sqlite

echo "→ Dépendances + build"
composer install --no-dev --prefer-dist --optimize-autoloader --no-interaction
php artisan key:generate --force
php artisan migrate --force --seed
npm ci --no-audit --no-fund
npm run build

echo "→ Caches production"
php artisan config:cache && php artisan route:cache && php artisan view:cache

echo "→ Droits"
chown -R www-data:www-data storage bootstrap/cache database
chmod -R ug+rw storage bootstrap/cache

echo "→ Nginx"
sed "s/sauvermonentreprise.fr/${DOMAIN}/g; s#/var/www/sauvermonentreprise#${APP_DIR}#g; s/php8.4-fpm/php${PHP_VER}-fpm/g" \
    deploy/nginx.conf.example > /etc/nginx/sites-available/sauvermonentreprise
ln -sf /etc/nginx/sites-available/sauvermonentreprise /etc/nginx/sites-enabled/sauvermonentreprise
rm -f /etc/nginx/sites-enabled/default
nginx -t && systemctl reload nginx

echo ""
echo "✓ Application installée et servie en HTTP sur ${DOMAIN}."
echo ""
echo "PROCHAINES ÉTAPES :"
echo "  1. Dans le manager OVH → Zone DNS de ${DOMAIN} : faire pointer les"
echo "     enregistrements A de « ${DOMAIN} » et « www » vers l'IP de ce VPS :"
echo "        $(curl -fsSL https://api.ipify.org 2>/dev/null || echo '<IP du VPS>')"
echo "  2. Une fois le DNS propagé, activer le HTTPS :"
echo "        certbot --nginx -d ${DOMAIN} -d www.${DOMAIN}"
echo "  3. Sécuriser les comptes de démonstration (redaction@…, relecture@…)."
