#!/bin/bash
# SMirrors Installation Script
# Installs SMirrors binary, configuration, and systemd service files

set -e

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# Configuration
BINARY_NAME="smirrors"
INSTALL_DIR="/usr/local/bin"
CONFIG_DIR="/etc/smirrors"
DATA_DIR="/var/lib/smirrors"
CACHE_DIR="/var/cache/smirrors"
LOG_DIR="/var/log/smirrors"
RUN_DIR="/var/run/smirrors"
SYSTEMD_DIR="/etc/systemd/system"

echo -e "${GREEN}SMirrors Installation Script${NC}"
echo "================================"
echo

# Check if running as root
if [ "$EUID" -ne 0 ]; then
    echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
    exit 1
fi

# Check if binary exists
if [ ! -f "target/release/${BINARY_NAME}" ]; then
    echo -e "${YELLOW}Binary not found. Building release version...${NC}"
    cargo build --release

    if [ $? -ne 0 ]; then
        echo -e "${RED}Error: Build failed${NC}"
        exit 1
    fi
fi

# 1. Install binary
echo -e "${GREEN}[1/7]${NC} Installing binary to ${INSTALL_DIR}..."
cp "target/release/${BINARY_NAME}" "${INSTALL_DIR}/"
chmod 755 "${INSTALL_DIR}/${BINARY_NAME}"
echo "      ✓ Binary installed"

# 2. Create directories
echo -e "${GREEN}[2/7]${NC} Creating directories..."
mkdir -p "${CONFIG_DIR}"
mkdir -p "${DATA_DIR}"
mkdir -p "${CACHE_DIR}"
mkdir -p "${LOG_DIR}"
mkdir -p "${RUN_DIR}"
echo "      ✓ Directories created"

# 3. Set permissions
echo -e "${GREEN}[3/7]${NC} Setting permissions..."
chmod 755 "${CONFIG_DIR}"
chmod 755 "${DATA_DIR}"
chmod 755 "${CACHE_DIR}"
chmod 755 "${LOG_DIR}"
chmod 755 "${RUN_DIR}"
echo "      ✓ Permissions set"

# 4. Install configuration
echo -e "${GREEN}[4/7]${NC} Installing configuration..."
if [ ! -f "${CONFIG_DIR}/config.toml" ]; then
    if [ -f "config/smirrors.toml.example" ]; then
        cp "config/smirrors.toml.example" "${CONFIG_DIR}/config.toml"
        echo "      ✓ Default configuration installed"
    else
        # Initialize config using the binary
        "${INSTALL_DIR}/${BINARY_NAME}" init
        echo "      ✓ Configuration initialized"
    fi
else
    echo "      ⚠ Configuration already exists, skipping"
fi

# 5. Install systemd service files
echo -e "${GREEN}[5/7]${NC} Installing systemd service files..."
if [ -f "systemd/${BINARY_NAME}.service" ]; then
    cp "systemd/${BINARY_NAME}.service" "${SYSTEMD_DIR}/"
    echo "      ✓ Service file installed"
else
    echo -e "      ${YELLOW}⚠ Service file not found, skipping${NC}"
fi

if [ -f "systemd/${BINARY_NAME}.timer" ]; then
    cp "systemd/${BINARY_NAME}.timer" "${SYSTEMD_DIR}/"
    echo "      ✓ Timer file installed"
else
    echo -e "      ${YELLOW}⚠ Timer file not found, skipping${NC}"
fi

# 6. Reload systemd
echo -e "${GREEN}[6/7]${NC} Reloading systemd daemon..."
systemctl daemon-reload
echo "      ✓ Systemd reloaded"

# 7. Optionally enable and start service
echo -e "${GREEN}[7/7]${NC} Service setup..."
read -p "Enable SMirrors service to start on boot? [y/N] " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    systemctl enable "${BINARY_NAME}.service"
    systemctl enable "${BINARY_NAME}.timer"
    echo "      ✓ Service enabled"

    read -p "Start SMirrors service now? [y/N] " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        systemctl start "${BINARY_NAME}.timer"
        echo "      ✓ Service started"
    fi
else
    echo "      ⓘ Service not enabled (you can enable it later with: systemctl enable ${BINARY_NAME}.service)"
fi

echo
echo -e "${GREEN}Installation complete!${NC}"
echo
echo "Next steps:"
echo "  1. Edit configuration: ${CONFIG_DIR}/config.toml"
echo "  2. Test mirrors:       ${BINARY_NAME} test"
echo "  3. Update mirrors:     ${BINARY_NAME} update"
echo "  4. Check status:       ${BINARY_NAME} status"
echo "  5. View help:          ${BINARY_NAME} --help"
echo
echo "Systemd commands:"
echo "  Enable service:  sudo systemctl enable ${BINARY_NAME}.service ${BINARY_NAME}.timer"
echo "  Start service:   sudo systemctl start ${BINARY_NAME}.timer"
echo "  Check status:    sudo systemctl status ${BINARY_NAME}.service"
echo "  View logs:       sudo journalctl -u ${BINARY_NAME}.service -f"
echo
