#!/bin/bash
# Budgie on labwc session launcher with seatd and group checks

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

export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
export XDG_SESSION_TYPE=wayland
export GDK_BACKEND=wayland
export XDG_CURRENT_DESKTOP=Budgie:GNOME

echo -e "${GREEN}Starting Budgie on labwc session...${NC}"

# Check groups
if ! groups | grep -q "video"; then
    echo -e "${RED}❌ User not in video group${NC}"
    echo "Run: sudo usermod -aG video,input,render $USER && logout"
    exit 1
fi

if ! groups | grep -q "input"; then
    echo -e "${YELLOW}⚠️  User not in input group (optional)${NC}"
fi

if ! groups | grep -q "render"; then
    echo -e "${YELLOW}⚠️  User not in render group (optional)${NC}"
fi

# Start seatd if not running
if ! pgrep -x "seatd" > /dev/null; then
    echo -e "${YELLOW}Starting seatd...${NC}"
    sudo seatd -g video &
    sleep 2
fi

# Check seatd socket
if [ ! -e "/run/seatd.sock" ]; then
    echo -e "${RED}❌ seatd socket not found${NC}"
    exit 1
fi

# Kill any stale processes
pkill labwc 2>/dev/null
pkill budgie-panel 2>/dev/null
pkill budgie-desktop-view 2>/dev/null
sleep 1

# Start labwc
echo -e "${GREEN}Starting labwc...${NC}"
labwc -C /etc/xdg/labwc/rc.xml &
LABWC_PID=$!
sleep 3

# Start budgie components
echo -e "${GREEN}Starting budgie-panel...${NC}"
budgie-panel --replace &
echo -e "${GREEN}Starting budgie-desktop-view...${NC}"
budgie-desktop-view &

# Wait for labwc to exit
wait $LABWC_PID
