# Copyright (C) 2020-2021 Bob Hepple <bob.hepple@gmail.com>
# Copyright (C) 2021,2023,2024 Sefa Eyeoglu <contact@scrumplex.net>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
{
  lib,
  writeShellApplication,
  backend ? "sway",
  sway,
  hyprland,
  jq,
}:
assert builtins.elem backend ["sway" "hyprland"];
  writeShellApplication {
    name = "run-or-raise";

    runtimeInputs =
      lib.optional (backend == "sway") sway
      ++ lib.optionals (backend == "hyprland") [hyprland jq];

    text = ''

      [ $# -lt 2 ] && exit 1

      class="$1"

      ${lib.optionalString (backend == "sway") ''
        swaymsg "[app_id=$class] focus" &>/dev/null || {
          # could be Xwayland app:
          swaymsg "[class=$class] focus" &>/dev/null
        } || exec "''${@:2}"
      ''}
      ${lib.optionalString (backend == "hyprland") ''
        address=$(hyprctl clients -j | jq --arg "class" "$class" -r '.[] | select(.class == $class) | .address')
        if [ -n "$address" ]; then
          hyprctl dispatch focuswindow "address:$address"
        else
          exec "''${@:2}"
        fi
      ''}
      exit 0
    '';

    meta = with lib; {
      platforms = platforms.linux;
    };
  }
