#!/bin/bash

# Superball Thrower Daemon Installation Script
# This script installs and configures the Superball Thrower daemon

set -e

echo "=== Superball Thrower Daemon Installation ==="
echo

# Check if running as root
if [[ $EUID -eq 0 ]]; then
   echo "This script should not be run as root for security reasons."
   echo "Please run as a regular user with sudo privileges."
   exit 1
fi

# Check if Node.js is installed
if ! command -v node &> /dev/null; then
    echo "Error: Node.js is not installed."
    echo "Please install Node.js 16 or later from https://nodejs.org/"
    exit 1
fi

# Check Node.js version
NODE_VERSION=$(node --version | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -lt 16 ]; then
    echo "Error: Node.js version 16 or later is required."
    echo "Current version: $(node --version)"
    exit 1
fi

echo "✓ Node.js $(node --version) detected"

# Check if npm is installed
if ! command -v npm &> /dev/null; then
    echo "Error: npm is not installed."
    exit 1
fi

echo "✓ npm $(npm --version) detected"

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DAEMON_DIR="$SCRIPT_DIR"

echo "Installing to: $DAEMON_DIR"
echo

# Install Node.js dependencies
echo "Installing Node.js dependencies..."
cd "$DAEMON_DIR"
npm install

if [ $? -ne 0 ]; then
    echo "Error: Failed to install Node.js dependencies"
    exit 1
fi

echo "✓ Dependencies installed successfully"
echo

# Create user for daemon (if it doesn't exist)
DAEMON_USER="superball"
if ! id "$DAEMON_USER" &>/dev/null; then
    echo "Creating system user: $DAEMON_USER"
    sudo useradd --system --no-create-home --shell /bin/false "$DAEMON_USER"
    echo "✓ User $DAEMON_USER created"
else
    echo "✓ User $DAEMON_USER already exists"
fi

# Create directories
echo "Creating directories..."
sudo mkdir -p /var/log/superball
sudo mkdir -p /etc/superball
sudo chown "$DAEMON_USER:$DAEMON_USER" /var/log/superball
echo "✓ Directories created"

# Copy configuration file
if [ ! -f "/etc/superball/config.json" ]; then
    echo "Installing default configuration..."
    sudo cp "$DAEMON_DIR/config.json" /etc/superball/config.json
    sudo chown "$DAEMON_USER:$DAEMON_USER" /etc/superball/config.json
    sudo chmod 600 /etc/superball/config.json
    echo "✓ Configuration installed to /etc/superball/config.json"
    echo "⚠️  IMPORTANT: Edit /etc/superball/config.json with your private key and settings"
else
    echo "✓ Configuration already exists at /etc/superball/config.json"
fi

# Install systemd service
echo "Installing systemd service..."
sudo cp "$DAEMON_DIR/superball-thrower.service" /etc/systemd/system/
sudo sed -i "s|/path/to/thrower_daemon|$DAEMON_DIR|g" /etc/systemd/system/superball-thrower.service
sudo systemctl daemon-reload
echo "✓ Systemd service installed"

# Set permissions
echo "Setting permissions..."
sudo chown -R "$DAEMON_USER:$DAEMON_USER" "$DAEMON_DIR"
sudo chmod +x "$DAEMON_DIR/daemon.js"
echo "✓ Permissions set"

echo
echo "=== Installation Complete ==="
echo
echo "Next steps:"
echo "1. Edit the configuration file:"
echo "   sudo nano /etc/superball/config.json"
echo
echo "2. Add your private key and configure relays"
echo
echo "3. Enable and start the service:"
echo "   sudo systemctl enable superball-thrower"
echo "   sudo systemctl start superball-thrower"
echo
echo "4. Check service status:"
echo "   sudo systemctl status superball-thrower"
echo
echo "5. View logs:"
echo "   sudo journalctl -u superball-thrower -f"
echo
echo "Configuration file location: /etc/superball/config.json"
echo "Log file location: /var/log/superball/daemon.log"
echo "Service name: superball-thrower"
echo
echo "⚠️  SECURITY WARNING:"
echo "   Make sure to secure your private key in the configuration file!"
echo "   The config file should only be readable by the superball user."