#!/bin/bash
# ============================================
# Ring2All Repository Setup Script
# Run: wget https://repo.softswitchone.com/apt/setup_repo && chmod +x setup_repo && ./setup_repo
# ============================================
set -e

REPO_URL="https://repo.softswitchone.com/apt"

echo ""
echo "=========================================="
echo "  Ring2All Repository Setup"
echo "=========================================="
echo ""

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo "Error: Please run as root"
    echo "Usage: sudo ./setup_repo"
    exit 1
fi

# Create keyrings directory
mkdir -p /etc/apt/keyrings

# Download GPG key
echo "Downloading GPG key..."
curl -fsSL ${REPO_URL}/gpgkey/ring2all.gpg | gpg --dearmor --yes -o /etc/apt/keyrings/ring2all.gpg 2>/dev/null || true
chmod 644 /etc/apt/keyrings/ring2all.gpg
echo "✓ GPG key installed"

# Add repository sources
echo "Adding repository sources..."
# Ring2All Softswitch Repository
cat > /etc/apt/sources.list.d/ring2all.list << 'EOF'
# Base: FreeSWITCH core packages
deb [signed-by=/etc/apt/keyrings/ring2all.gpg] https://repo.softswitchone.com/apt/base stable main

# Core: Softswitch core packages (db, admin, api, telephony, etc.)
deb [signed-by=/etc/apt/keyrings/ring2all.gpg] https://repo.softswitchone.com/apt/core stable main

# Devel: Development headers, debug symbols, ESL
deb [signed-by=/etc/apt/keyrings/ring2all.gpg] https://repo.softswitchone.com/apt/devel stable main

# Extras: Optional and auxiliary FreeSWITCH modules
deb [signed-by=/etc/apt/keyrings/ring2all.gpg] https://repo.softswitchone.com/apt/extras stable main

# Audios: Pre-recorded audio files and MOH
deb [signed-by=/etc/apt/keyrings/ring2all.gpg] https://repo.softswitchone.com/apt/audios stable main
EOF
echo "✓ Repository sources added"

# Add NodeSource repository for Node.js 22 LTS (if not already configured)
if ! grep -r "deb.nodesource.com" /etc/apt/sources.list.d/ 2>/dev/null | grep -q "node_22.x"; then
    echo "Adding NodeSource repository for Node.js 22 LTS..."
    mkdir -p /etc/apt/keyrings
    curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor --yes -o /etc/apt/keyrings/nodesource.gpg 2>/dev/null || true
    echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_22.x nodistro main" > /etc/apt/sources.list.d/nodesource.list
    echo "✓ NodeSource Node.js 22 LTS repository added"
else
    echo "✓ NodeSource Node.js 22 LTS repository already present"
fi

# Add official nginx.org repository (patched upstream Stable branch >= 1.30.7)
echo "Adding official NGINX repository..."
mkdir -p /usr/share/keyrings /etc/apt/sources.list.d /etc/apt/preferences.d
NGINX_CODENAME=$(. /etc/os-release 2>/dev/null && echo "${VERSION_CODENAME:-}")
[ -z "$NGINX_CODENAME" ] && NGINX_CODENAME=$(lsb_release -cs 2>/dev/null || echo "trixie")
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor --yes -o /usr/share/keyrings/nginx-archive-keyring.gpg 2>/dev/null || true
chmod 644 /usr/share/keyrings/nginx-archive-keyring.gpg 2>/dev/null || true
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] https://nginx.org/packages/debian ${NGINX_CODENAME} nginx" > /etc/apt/sources.list.d/nginx.list
printf "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" > /etc/apt/preferences.d/99nginx
echo "✓ Official NGINX repository configured (${NGINX_CODENAME})"

echo ""
echo "=========================================="
echo "  Repository Setup Complete!"
echo "=========================================="
echo ""
echo "Next steps:"
echo "  1. apt update"
echo "  2. apt install -y nginx             (Install official nginx.org web server before Ring2All)"
echo "  3. apt install softswitch-all       (Complete All-in-One Carrier Platform)"
echo ""
echo "Or install application suites individually:"
echo "  apt install softswitch-pbx-all   (Ring2All PBX Suite)"
echo "  apt install softswitch-sbc-all   (Ring2All SBC Suite)"
echo "  apt install softswitch-bss-all   (Ring2All BSS Suite)"
echo "  apt install softswitch-dms-all   (Ring2All DMS Suite)"
echo "  apt install softswitch-nms-all   (Ring2All NMS Suite)"
echo ""
echo "Or install components individually:"
echo "  apt install softswitch-pbx-db"
echo "  apt install softswitch-pbx-admin"
echo "  apt install softswitch-pbx-api"
echo "  apt install softswitch-pbx-telephony"
echo ""
