#!/bin/bash
# Sync MIB files and device profiles from LibreNMS
# Usage: ./scripts/sync-librenms.sh [LIBRENMS_PATH]
#
# This script:
# 1. Syncs MIB files from LibreNMS to priv/mibs/
# 2. Syncs YAML profile definitions to priv/profiles/
#
# Prerequisites:
# - LibreNMS repository cloned locally

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
LIBRENMS_PATH="${1:-$HOME/dev/librenms}"

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

echo -e "${GREEN}=== LibreNMS Sync Script ===${NC}"
echo "LibreNMS path: $LIBRENMS_PATH"
echo "Project root: $PROJECT_ROOT"
echo ""

# Verify LibreNMS path exists
if [ ! -d "$LIBRENMS_PATH" ]; then
    echo -e "${RED}Error: LibreNMS directory not found at $LIBRENMS_PATH${NC}"
    echo "Please provide the path to LibreNMS as the first argument:"
    echo "  ./scripts/sync-librenms.sh /path/to/librenms"
    exit 1
fi

# Verify LibreNMS has the expected structure
if [ ! -d "$LIBRENMS_PATH/mibs" ]; then
    echo -e "${RED}Error: MIBs directory not found at $LIBRENMS_PATH/mibs${NC}"
    exit 1
fi

if [ ! -d "$LIBRENMS_PATH/resources/definitions" ]; then
    echo -e "${RED}Error: Definitions directory not found at $LIBRENMS_PATH/resources/definitions${NC}"
    exit 1
fi

# Step 1: Sync MIB files
echo -e "${YELLOW}Step 1: Syncing MIB files...${NC}"
MIB_DEST="$PROJECT_ROOT/priv/mibs"
mkdir -p "$MIB_DEST"

# Sync vendor-specific MIB directories we care about
VENDOR_MIBS=(
    "cambium"
    "mikrotik"
    "ubnt"
    "cisco"
)

for vendor in "${VENDOR_MIBS[@]}"; do
    if [ -d "$LIBRENMS_PATH/mibs/$vendor" ]; then
        echo "  Syncing $vendor MIBs..."
        mkdir -p "$MIB_DEST/$vendor"
        rsync -a --delete "$LIBRENMS_PATH/mibs/$vendor/" "$MIB_DEST/$vendor/"
    else
        echo -e "  ${YELLOW}Warning: $vendor MIBs not found, skipping${NC}"
    fi
done

# Count synced MIBs
MIB_COUNT=$(find "$MIB_DEST" -type f 2>/dev/null | wc -l | tr -d ' ')
echo -e "${GREEN}  Synced $MIB_COUNT MIB files${NC}"
echo ""

# Step 2: Sync YAML profile definitions
echo -e "${YELLOW}Step 2: Syncing YAML profile definitions...${NC}"
PROFILE_DEST="$PROJECT_ROOT/priv/profiles"
mkdir -p "$PROFILE_DEST/os_detection" "$PROFILE_DEST/os_discovery"

# Sync os_detection YAML files
echo "  Syncing os_detection definitions..."
rsync -a --delete "$LIBRENMS_PATH/resources/definitions/os_detection/" "$PROFILE_DEST/os_detection/"

# Sync os_discovery YAML files
echo "  Syncing os_discovery definitions..."
rsync -a --delete "$LIBRENMS_PATH/resources/definitions/os_discovery/" "$PROFILE_DEST/os_discovery/"

# Count synced profiles
DETECTION_COUNT=$(find "$PROFILE_DEST/os_detection" -name "*.yaml" 2>/dev/null | wc -l | tr -d ' ')
DISCOVERY_COUNT=$(find "$PROFILE_DEST/os_discovery" -name "*.yaml" 2>/dev/null | wc -l | tr -d ' ')
echo -e "${GREEN}  Synced $DETECTION_COUNT detection profiles, $DISCOVERY_COUNT discovery profiles${NC}"
echo ""

echo -e "${GREEN}=== Sync Complete ===${NC}"
echo ""
echo "Files synced to:"
echo "  - MIBs: priv/mibs/"
echo "  - Profiles: priv/profiles/"
echo ""
echo "To import profiles into the database, run:"
echo "  mix import_profiles --source-path priv/profiles"
echo ""
echo "Or profiles will be auto-imported on application startup."
