#!/bin/bash
# ============================================================
# 01_server_setup.sh
# Run as root on Ubuntu 20.04 — installs all dependencies
# ============================================================
set -e

echo "=== Updating system ==="
apt-get update && apt-get upgrade -y

echo "=== Installing base tools ==="
apt-get install -y curl git unzip software-properties-common build-essential

echo "=== Installing Python 3.12 ==="
add-apt-repository ppa:deadsnakes/ppa -y
apt-get update
# Install packages one by one to avoid apt regex matching issues
apt-get install -y python3.12
apt-get install -y python3.12-venv
apt-get install -y python3.12-dev
# Fallback: if python3.12-venv not available, install python3-venv
python3.12 -m venv --help &>/dev/null || apt-get install -y python3-venv

echo "=== Installing Node.js 20 ==="
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs

echo "=== Installing PostgreSQL ==="
apt-get install -y postgresql postgresql-contrib

echo "=== Installing Nginx ==="
apt-get install -y nginx

echo "=== Installing Certbot ==="
apt-get install -y certbot python3-certbot-nginx

echo "=== Done. Versions installed: ==="
python3.12 --version 2>/dev/null || python3 --version
node --version
npm --version
psql --version
nginx -v
