#!/bin/env bash

for i in "pgrep" "pkill" "jq" "hyprctl" "wf-recorder" "notify-send" "hyprland-share-picker"; do
  if builtin type -P "$i" &> /dev/null; then continue; fi

  notify-send -h string:wf-recorder:record -t 1000 "Missing dependencies, can't record.";
  echo "Missing dependencies."
  exit 1
done

pgrep -x "wf-recorder" && \
  pkill -INT -x wf-recorder && \
  notify-send -h string:wf-recorder:record -t 1000 "Finished Recording" && \
  exit 0

display=$(hyprland-share-picker | sed -r 's/.+:([^@]+)(@.+)?/\1/')
if [ -z "$display" ]; then
  notify-send "Please select a display"
  exit 1
fi

for i in $(seq 1 3); do
  notify-send -h string:wf-recorder:record -t 1000 "Recording in:" "<span color='#90a4f4' font='26px'><i><b>$((4 - $i))</b></i></span>"
  sleep 1
done

dateTime=$(date +%m-%d-%Y-%H:%M:%S)
dir="$HOME/Videos/ScreenRecordings"

mkdir -p "$dir"
wf-recorder -o "$display" -f "$dir/$dateTime.mp4"
