#!/bin/bash
# ============================================================
# Instrument Cluster — Phase 1 SSH Commands
# Run these as root on 192.81.168.5
# Paste each block one at a time, verify before next block
# ============================================================

# ── BLOCK 1: Install Node.js 20 ───────────────────────────
# Paste this entire block, press Enter, wait for completion
curl -fsSL https://rpm.nodesource.com/setup_20.x | bash -
dnf install -y nodejs
node --version   # should print v20.x.x
npm --version    # should print 10.x.x

# ── BLOCK 2: Install PM2 globally ─────────────────────────
npm install -g pm2
pm2 --version    # should print 5.x.x

# ── BLOCK 3: Create API server directory ──────────────────
mkdir -p /home/ic-api
cd /home/ic-api
# (you will upload server.js, package.json, .env here next)

# ── BLOCK 4: Block port 3001 from public ──────────────────
firewall-cmd --permanent --remove-port=3001/tcp 2>/dev/null || true
firewall-cmd --reload
# Verify port is NOT listed:
firewall-cmd --list-ports

# ── BLOCK 5: Enable Apache proxy modules ──────────────────
# cPanel manages Apache — use EasyApache or WHM
# Run in WHM terminal or SSH:
grep -r "proxy_module" /etc/apache2/conf.d/ 2>/dev/null || \
grep -r "proxy" /usr/local/apache/conf/httpd.conf | head -5
# If modules not enabled, enable via WHM > EasyApache 4 > Apache Modules
# Check: mod_proxy, mod_proxy_http must be enabled

# ── BLOCK 6: Create MySQL database via CLI ────────────────
# (or use cPanel MySQL Databases — either works)
mysql -u root -p << 'SQL'
CREATE DATABASE IF NOT EXISTS ic_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER IF NOT EXISTS 'ic_user'@'localhost' IDENTIFIED BY 'REPLACE_WITH_STRONG_PASSWORD';
GRANT ALL PRIVILEGES ON ic_production.* TO 'ic_user'@'localhost';
FLUSH PRIVILEGES;
SQL

# ── BLOCK 7: Verify database created ──────────────────────
mysql -u root -p -e "SHOW DATABASES LIKE 'ic_production';"

# ── BLOCK 8: Enable MySQL event scheduler ─────────────────
mysql -u root -p -e "SET GLOBAL event_scheduler = ON;"
echo "event_scheduler = ON" >> /etc/my.cnf

echo ""
echo "Phase 1 complete — report back with output of each block"
