#!/bin/bash
# ==============================================================================
# Smart Student Portal - Automated Ubuntu / Debian Installer
# OSOL AL-ELM UNIVERSITY COLLEGE (Pharmacy Department)
# Version: 2.2.0
# ==============================================================================

set -e

if [ "$EUID" -ne 0 ]; then
    echo "ERROR: Please run as root (sudo bash install-ubuntu.sh)"
    exit 1
fi

echo "============================================================"
echo "  Smart Student Portal - Ubuntu Installer (v2.2.0)"
echo "  OSOL AL-ELM UNIVERSITY COLLEGE"
echo "============================================================"

# Update and install core system dependencies
echo "[1/6] Updating package cache and installing prerequisites..."
apt-get update -qq
apt-get install -y -qq python3 python3-pip python3-venv curl iptables

# Optimize Linux kernel network backlog and open file limits for 1,000 students
echo "[2/6] Configuring OS concurrency and network limits..."
sysctl -w net.core.somaxconn=2048 >/dev/null 2>&1 || true
sysctl -w net.ipv4.tcp_max_syn_backlog=2048 >/dev/null 2>&1 || true
if [ ! -f /etc/security/limits.d/99-smart-portal.conf ]; then
    cat <<EOF > /etc/security/limits.d/99-smart-portal.conf
* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535
EOF
fi

# Install the deb package if present in the same directory
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEB_FILE="$DIR/smart-student-portal_2.2.0_all.deb"

if [ -f "$DEB_FILE" ]; then
    echo "[3/6] Installing package from $DEB_FILE..."
    dpkg -i "$DEB_FILE" || apt-get install -f -y
else
    echo "[3/6] Setting up /opt/smart-student-portal directly..."
    mkdir -p /opt/smart-student-portal
    cp -r "$DIR"/* /opt/smart-student-portal/ 2>/dev/null || true
    python3 -m venv /opt/smart-student-portal/.venv
    /opt/smart-student-portal/.venv/bin/pip install --upgrade pip
    /opt/smart-student-portal/.venv/bin/pip install flask werkzeug bcrypt requests gunicorn
fi

# Firewall rules for Classroom Wi-Fi port 8000
echo "[4/6] Configuring firewall port 8000..."
if command -v ufw >/dev/null 2>&1; then
    ufw allow 8000/tcp comment 'Smart Student Portal Classroom' >/dev/null 2>&1 || true
fi

# Verify service status
echo "[5/6] Checking systemd service status..."
systemctl status classroom-attendance.service --no-pager || true

# Test local endpoint
echo "[6/6] Performing healthcheck verification..."
sleep 2
HEALTH_VERSION=$(curl -s http://127.0.0.1:8000/api/version || true)
if echo "$HEALTH_VERSION" | grep -q "2.2.0"; then
    echo "SUCCESS: Smart Student Portal v2.2.0 is up and healthy!"
    echo "Student PWA endpoint active at http://127.0.0.1:8000/student"
else
    echo "Service is booting... Access http://127.0.0.1:8000 to verify."
fi

echo "============================================================"
echo "  Installation Complete!"
echo "  Local Web URL : http://127.0.0.1:8000"
echo "  Management CLI: smart-student-portal {status|restart|sync|logs}"
echo "============================================================"
