# -------------------- Annotation deploy-zeroirc.sh --------------------
# Installs Nginx, Node.js 20.x, Certbot (optional)  
# Copies the project to /opt/zeroirc-web  
# Installs npm dependencies  
# Creates a systemd service for Node.js  
# Configures Nginx (with or without SSL)  
# Starts the service
# ----------------------------------------------------------------------

#!/bin/bash
set -e

# -------------------- SETTINGS --------------------
PROJECT_DIR="/opt/zeroirc-web"
DOMAIN=""   # if SSL is needed – specify the domain, otherwise leave it empty
EMAIL=""    # email for Let's Encrypt (if SSL)
PORT=3000   # Node.js gateway port
NODE_VERSION=20

# Colors for output
GREEN='\033[0;32m'
NC='\033[0m'

# -------------------- PACKAGE INSTALLATION --------------------
echo -e "${GREEN}[1/7] Installing system dependencies...${NC}"
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx curl

# Node.js (NodeSource)
curl -fsSL https://deb.nodesource.com/setup_${NODE_VERSION}.x | sudo -E bash -
sudo apt install -y nodejs

# Certbot (only if a domain is specified)
if [ -n "$DOMAIN" ]; then
    sudo apt install -y certbot python3-certbot-nginx
fi

# -------------------- PROJECT COPYING --------------------
echo -e "${GREEN}[2/7] Copying project files...${NC}"
sudo mkdir -p $PROJECT_DIR
sudo cp -r . $PROJECT_DIR
sudo chown -R www-data:www-data $PROJECT_DIR

# -------------------- NODE DEPENDENCIES --------------------
echo -e "${GREEN}[3/7] Installing npm dependencies...${NC}"
cd $PROJECT_DIR
sudo -u www-data npm install --production

# -------------------- SYSTEMD SERVICE --------------------
echo -e "${GREEN}[4/7] Creating systemd service...${NC}"
sudo tee /etc/systemd/system/zeroirc-gateway.service > /dev/null <<EOF
[Unit]
Description=ZeroIRC WebSocket Gateway
After=network.target

[Service]
Type=simple
User=www-data
Group=www-data
WorkingDirectory=$PROJECT_DIR
ExecStart=/usr/bin/node server.js
Restart=on-failure
RestartSec=5
Environment=PORT=$PORT

[Install]
WantedBy=multi-user.target
EOF

sudo systemctl daemon-reload
sudo systemctl enable zeroirc-gateway

# -------------------- NGINX --------------------
echo -e "${GREEN}[5/7] Configuring Nginx...${NC}"
if [ -n "$DOMAIN" ]; then
    # Config with SSL
    sudo tee /etc/nginx/sites-available/zeroirc > /dev/null <<EOF
server {
    listen 80;
    server_name $DOMAIN;
    return 301 https://\$server_name\$request_uri;
}

server {
    listen 443 ssl;
    server_name $DOMAIN;

    ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;

    root $PROJECT_DIR;
    index index.html;

    location /ws/ {
        proxy_pass http://localhost:$PORT;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host \$host;
        proxy_read_timeout 86400s;
    }

    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOF
    # Obtaining an SSL certificate
    sudo certbot --nginx -d $DOMAIN --non-interactive --agree-tos -m $EMAIL
else
    # Config without SSL
    sudo tee /etc/nginx/sites-available/zeroirc > /dev/null <<EOF
server {
    listen 80;
    server_name _;

    root $PROJECT_DIR;
    index index.html;

    location /ws/ {
        proxy_pass http://localhost:$PORT;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header Host \$host;
        proxy_read_timeout 86400s;
    }

    location / {
        try_files \$uri \$uri/ =404;
    }
}
EOF
fi

sudo ln -sf /etc/nginx/sites-available/zeroirc /etc/nginx/sites-enabled/
sudo rm -f /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx

# -------------------- STARTING SERVICES --------------------
echo -e "${GREEN}[6/7] Starting services...${NC}"
sudo systemctl start zeroirc-gateway

# -------------------- DONE --------------------
echo -e "${GREEN}[7/7] Installation complete!${NC}"
if [ -n "$DOMAIN" ]; then
    echo "Application is available at: https://$DOMAIN"
else
    echo "Application is available at the server IP address (port 80)"
fi