#!/bin/bash
# Conduit Matrix Homeserver Installation Script
# Location: Run this on target VPS after cloning timmy-config
# Reference: docs/matrix-fleet-comms/README.md

set -euo pipefail

# Configuration
CONDUIT_VERSION="0.8.0"  # Check https://gitlab.com/famedly/conduit/-/releases
CONDUIT_DIR="/opt/conduit"
DATA_DIR="$CONDUIT_DIR/data"
LOGS_DIR="$CONDUIT_DIR/logs"
SCRIPTS_DIR="$CONDUIT_DIR/scripts"
CONDUIT_USER="conduit"

echo "========================================"
echo "Conduit Matrix Homeserver Installer"
echo "Target: $CONDUIT_DIR"
echo "Version: $CONDUIT_VERSION"
echo "========================================"
echo

# Check root
if [ "$EUID" -ne 0 ]; then
    echo "Error: Please run as root"
    exit 1
fi

# Create conduit user
echo "[1/8] Creating conduit user..."
if ! id "$CONDUIT_USER" &>/dev/null; then
    useradd -r -s /bin/false -d "$CONDUIT_DIR" "$CONDUIT_USER"
    echo "  Created user: $CONDUIT_USER"
else
    echo "  User exists: $CONDUIT_USER"
fi

# Create directories
echo "[2/8] Creating directories..."
mkdir -p "$CONDUIT_DIR" "$DATA_DIR" "$LOGS_DIR" "$SCRIPTS_DIR"
chown -R "$CONDUIT_USER:$CONDUIT_USER" "$CONDUIT_DIR"

# Download Conduit
echo "[3/8] Downloading Conduit v${CONDUIT_VERSION}..."
ARCH=$(uname -m)
case "$ARCH" in
    x86_64)
        CONDUIT_ARCH="x86_64-unknown-linux-gnu"
        ;;
    aarch64)
        CONDUIT_ARCH="aarch64-unknown-linux-gnu"
        ;;
    *)
        echo "Error: Unsupported architecture: $ARCH"
        exit 1
        ;;
esac

CONDUIT_URL="https://gitlab.com/famedly/conduit/-/releases/download/v${CONDUIT_VERSION}/conduit-${CONDUIT_ARCH}"

curl -L -o "$CONDUIT_DIR/conduit" "$CONDUIT_URL"
chmod +x "$CONDUIT_DIR/conduit"
chown "$CONDUIT_USER:$CONDUIT_USER" "$CONDUIT_DIR/conduit"
echo "  Downloaded: $CONDUIT_DIR/conduit"

# Install configuration
echo "[4/8] Installing configuration..."
if [ -f "conduit.toml" ]; then
    cp conduit.toml "$CONDUIT_DIR/conduit.toml"
    chown "$CONDUIT_USER:$CONDUIT_USER" "$CONDUIT_DIR/conduit.toml"
    echo "  Installed: $CONDUIT_DIR/conduit.toml"
else
    echo "  Warning: conduit.toml not found in current directory"
fi

# Install systemd service
echo "[5/8] Installing systemd service..."
if [ -f "conduit.service" ]; then
    cp conduit.service /etc/systemd/system/conduit.service
    systemctl daemon-reload
    echo "  Installed: /etc/systemd/system/conduit.service"
else
    echo "  Warning: conduit.service not found in current directory"
fi

# Install scripts
echo "[6/8] Installing operational scripts..."
if [ -d "scripts" ]; then
    cp scripts/*.sh "$SCRIPTS_DIR/"
    chmod +x "$SCRIPTS_DIR"/*.sh
    chown -R "$CONDUIT_USER:$CONDUIT_USER" "$SCRIPTS_DIR"
    echo "  Installed scripts to $SCRIPTS_DIR"
fi

# Create backup directory
echo "[7/8] Creating backup directory..."
mkdir -p /backups/conduit
chown "$CONDUIT_USER:$CONDUIT_USER" /backups/conduit

# Setup cron for backups
echo "[8/8] Setting up backup cron job..."
if [ -f "$SCRIPTS_DIR/backup.sh" ]; then
    (crontab -l 2>/dev/null || true; echo "0 3 * * * $SCRIPTS_DIR/backup.sh >> $LOGS_DIR/backup.log 2>&1") | crontab -
    echo "  Backup cron job added (3 AM daily)"
fi

echo
echo "========================================"
echo "Installation Complete!"
echo "========================================"
echo
echo "Next steps:"
echo "  1. Configure DNS: matrix.timmy.foundation -> $(hostname -I | awk '{print $1}')"
echo "  2. Configure Caddy: cp Caddyfile /etc/caddy/conf.d/matrix.conf"
echo "  3. Start Conduit: systemctl start conduit"
echo "  4. Check health: $SCRIPTS_DIR/health.sh"
echo "  5. Create admin account (see README.md)"
echo
echo "Logs: $LOGS_DIR/"
echo "Data: $DATA_DIR/"
echo "Config: $CONDUIT_DIR/conduit.toml"
