{ pkgs, inputs, config, lib, ... }:

let
  cfg = config.snowhawk.fish;
  macos = config.snowhawk.macos.enable;
  rust = config.snowhawk.rust.enable;
in
{
  options.snowhawk.fish = {
    enable = lib.mkEnableOption "fish";
  };

  config = lib.mkIf cfg.enable {
    xdg.configFile."fish/completions/rustup.fish" = {
      enable = rust;
      source = ../completions/rustup.fish;
    };

    programs.bash = lib.mkIf (!macos) {
      enable = true;
      initExtra = ''
        if [[ $(${pkgs.procps}/bin/ps --no-header --pid=$PPID --format=comm) != "fish" && -z ''${BASH_EXECUTION_STRING} ]]
        then
          shopt -q login_shell && LOGIN_OPTION='--login' || LOGIN_OPTION=""
          exec ${pkgs.fish}/bin/fish $LOGIN_OPTION
        fi
      '';
    };

    programs.zsh = lib.mkIf macos {
      enable = true;
      initExtra = ''
        if [[ $(${pkgs.procps}/bin/ps $(${pkgs.procps}/bin/ps -p $$ -co "ppid=") -co "comm=") != "fish" && -z ''${ZSH_EXECUTION_STRING} ]]
        then
          exec ${pkgs.fish}/bin/fish
        fi
      '';
    };

    programs.fish = {
      enable = true;
      shellAbbrs = lib.mkIf (!macos) {
        copy = "xclip -sel clip";
      };
      interactiveShellInit =
        builtins.readFile "${inputs.fish_theme}/fish_prompt.fish" + "\n" +
        builtins.readFile ../fish/config.fish;
    };

    home.packages = with pkgs; [
      (writeShellApplication {
        name = "lnmv";
        runtimeInputs = [ coreutils ];
        text = ''
          # This is crude i know, more shell guards and stuff that I'm bad at

          # Usage:
          # lnmv <source> <target>

          data=''${1%/}
          target=''${2%/}

          mv "$data" "$target"
          ln -s "$target" "$data"
        '';
      })
    ];
  };
}