#!/bin/bash
# Quest 2 Network Monitor
# Pings the Quest 2 and emails if it's online

QUEST_IP="192.168.1.128"
QUEST_MAC="2C:26:17:95:8F:BA"
EMAIL="davidb715@gmail.com"
SUBJECT="Meta Quest 2 is ON the network"

# Try to ping the Quest 2
if ping -c 2 -W 2 "$QUEST_IP" &>/dev/null; then
    DETECTED=true
else
    # Fallback: check ARP table for the MAC
    arp -n | grep -i "$QUEST_MAC" &>/dev/null && DETECTED=true || DETECTED=false
fi

if [ "$DETECTED" = "true" ]; then
    TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S')
    BODY="Meta Quest 2 was detected on the network at $TIMESTAMP.\n\nIP: $QUEST_IP\nMAC: $QUEST_MAC"
    echo -e "$BODY" | msmtp "$EMAIL" --subject="$SUBJECT" 2>/dev/null || \
    echo -e "Subject: $SUBJECT\n\n$BODY" | msmtp "$EMAIL"
    echo "[$TIMESTAMP] Quest 2 detected — alert sent."
else
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] Quest 2 not found."
fi
