#!/bin/bash
#!/bin/bash
# CalDAV Calendar Tool - Supports Google, iCloud, and Work Calendars
cd "$(dirname "$0")"

# Get Apple ID and iCloud password from environment
# Note: Use original iCloud email (anthonym_au@icloud.com), not the alias
APPLE_ID="${CALENDAR_ICLOUD_ID:-anthonym_au@icloud.com}"
APPLE_PASS="${CALENDAR_ICLOUD_PASS:-mvas-vwsk-ktiv-anex}"

# Get work calendar credentials from environment
WORK_EMAIL="${CALENDAR_WORK_EMAIL:-Anthony.martin@pacificenergy.com.au}"
WORK_PASS="${CALENDAR_WORK_PASS:-RecOvery2026!}"
WORK_URL="${CALENDAR_WORK_URL:-https://outlook.office365.com/EWS/Exchange.asmx}"

# Choose which calendar to use
CALENDAR_TYPE="${1:-google}"  # Default to Google
CALENDAR_URL=""

if [ "$CALENDAR_TYPE" = "icloud" ]; then
  if [ -z "$APPLE_ID" ] || [ -z "$APPLE_PASS" ]; then
    echo "Error: CALENDAR_ICLOUD_ID and CALENDAR_ICLOUD_PASS must be set for iCloud" >&2
    echo "Run: export CALENDAR_ICLOUD_ID='your@email.com' CALENDAR_ICLOUD_PASS='password'" >&2
    exit 1
  fi
  CALENDAR_URL="https://caldav.icloud.com/${APPLE_ID}/calendars/"
elif [ "$CALENDAR_TYPE" = "work" ]; then
  if [ -z "$WORK_EMAIL" ] || [ -z "$WORK_URL" ]; then
    echo "Error: CALENDAR_WORK_EMAIL and CALENDAR_WORK_URL must be set for work calendar" >&2
    exit 1
  fi
  CALENDAR_URL="$WORK_URL"
else
  # Google Calendar (default)
  CALENDAR_URL="https://calendar.google.com/calendar/dav/"
fi

echo "📅 Using $CALENDAR_TYPE calendar" >&2

# Add calendar type to env for Python script
export CALENDAR_TYPE
export CALENDAR_URL

/home/openclaw/.local/bin/uv run --with caldav --with icalendar --with pytz cal.py "$@"
