#!/bin/bash

# omarchy:summary=Show or set window corners: theme, sharp, or rounded
# omarchy:args=[theme|sharp|rounded]
# omarchy:examples=omarchy hyprland corners | omarchy hyprland corners rounded

TOGGLES_DIR="${XDG_STATE_HOME:-$HOME/.local/state}/omarchy/toggles/hypr"

# Sharp and rounded are toggle files loaded after every theme, so they override
# the corners of whichever theme is current. Theme removes both.
if (( $# == 0 )); then
  if [[ -f $TOGGLES_DIR/corners-sharp.lua ]]; then
    echo "sharp"
  elif [[ -f $TOGGLES_DIR/corners-rounded.lua ]]; then
    echo "rounded"
  else
    echo "theme"
  fi
  exit 0
fi

case $1 in
  theme) LABEL="Corners follow the theme" ;;
  sharp) LABEL="Sharp corners for all themes" ;;
  rounded) LABEL="Rounded corners for all themes" ;;
  *)
    echo "Usage: omarchy-hyprland-corners [theme|sharp|rounded]" >&2
    exit 1
    ;;
esac

# Save the new choice before dropping the old one, so a failed save keeps it.
if [[ $1 != "theme" ]]; then
  mkdir -p "$TOGGLES_DIR" && cp "$OMARCHY_PATH/default/hypr/toggles/corners-$1.lua" "$TOGGLES_DIR/corners-$1.lua" || exit 1
fi
for mode in sharp rounded; do
  [[ $mode == "$1" ]] || rm -f "$TOGGLES_DIR/corners-$mode.lua" || exit 1
done

hyprctl reload >/dev/null || exit 1
omarchy-notification-send -g 󰘇 "$LABEL"
