#!/bin/bash

# Simple script to count unique BLE addresses
# Removes entries with device names starting with "lime-"
# Supports both file input and piped input

# Check if input is piped or from file
if [[ -p /dev/stdin ]]; then
    # Input is piped
    INPUT_SOURCE="/dev/stdin"
elif [[ $# -eq 0 ]]; then
    echo "Usage: $0 <input_file>"
    echo "   or: cat <input_file> | $0"
    exit 1
else
    # Input is from file
    INPUT_FILE="$1"
    if [[ ! -f "$INPUT_FILE" ]]; then
        echo "Error: File '$INPUT_FILE' not found"
        exit 1
    fi
    INPUT_SOURCE="$INPUT_FILE"
fi

# Filter out lime- entries and count unique MAC addresses
UNIQUE_COUNT=$(grep -v "lime-" "$INPUT_SOURCE" | \
               grep -oE "^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}:[0-9a-fA-F]{2}" | \
               sort -u | \
               wc -l)

echo "Unique BLE addresses: $UNIQUE_COUNT"
