#!/bin/bash

# BZZZ Service Installation Script
# Installs BZZZ as a systemd service

set -e

echo "🐝 Installing BZZZ P2P Task Coordination Service..."

# Check if running as root or with sudo
if [ "$EUID" -ne 0 ]; then
    echo "❌ This script must be run as root or with sudo"
    exit 1
fi

# Define paths
BZZZ_DIR="$(pwd)"
SERVICE_FILE="$BZZZ_DIR/bzzz.service"
SYSTEMD_DIR="/etc/systemd/system"

# Check if BZZZ binary exists
if [ ! -f "$BZZZ_DIR/bzzz" ]; then
    echo "❌ BZZZ binary not found at $BZZZ_DIR/bzzz"
    exit 1
fi

# Make binary executable
chmod +x "$BZZZ_DIR/bzzz"
echo "✅ Made BZZZ binary executable"

# Copy service file to systemd directory
cp "$SERVICE_FILE" "$SYSTEMD_DIR/bzzz.service"
echo "✅ Copied service file to $SYSTEMD_DIR/bzzz.service"

# Set proper permissions
chmod 644 "$SYSTEMD_DIR/bzzz.service"
echo "✅ Set service file permissions"

# Reload systemd daemon
systemctl daemon-reload
echo "✅ Reloaded systemd daemon"

# Enable service to start on boot
systemctl enable bzzz.service
echo "✅ Enabled BZZZ service for auto-start"

# Start the service
systemctl start bzzz.service
echo "✅ Started BZZZ service"

echo ""
echo "🎉 BZZZ P2P Task Coordination Service installed successfully!"
