#!/bin/bash

# Define your monitors
EXTERNAL="DP-1" 
LAPTOP="eDP-1"  
REPLAY_DIR="$HOME/Videos/Replays"

# Ensure the output directory exists
mkdir -p "$REPLAY_DIR"

# Kill any existing recorder instances to prevent duplicates/errors
# 2>/dev/null hides the "no process found" message if it wasn't running
killall gpu-screen-recorder 2>/dev/null
# Give the hardware encoder a fraction of a second to free up
sleep 0.5 

# Check if the external monitor is actively connected
if hyprctl monitors | grep -q "Monitor $EXTERNAL"; then
    TARGET_MONITOR=$EXTERNAL
else
    TARGET_MONITOR=$LAPTOP
fi

# Launch the recorder on the chosen monitor in the background
gpu-screen-recorder -w "$TARGET_MONITOR" -f 60 -r 120 -c mp4 -a default_output -a "alsa_input.pci-0000_00_1f.3.analog-stereo" -o "$REPLAY_DIR" &
