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

let

  # why not just use functions normally? ctrl-alt-e is why!
  _tmpfn_name_ = "_tmpfn_ex4aiNgu_"; # pwgen ftw
  mkArgsAlias = alias: ''${_tmpfn_name_}() { unset -f ${_tmpfn_name_}; ${alias}; }; ${_tmpfn_name_}'';

in

{
  programs.bash.enable = true;
  #programs.bash.enableCompletion = true;
  programs.bash.shellOptions = [
    # Append to history file rather than replacing it.
    "histappend"
    # check the window size after each command and, if
    # necessary, update the values of LINES and COLUMNS.
    "checkwinsize"
    # Extended globbing.
    "extglob"
    "globstar"
    # Warn if closing shell with running jobs.
    "checkjobs"
  ];
  # programs.bash.historySize = 10000; # in ram
  # programs.bash.historyFileSize = 100000; # on file
  programs.bash.historyControl = [
    "erasedups"
    "ignoreboth"
  ];
  programs.bash.historyIgnore = [ "ls" "file" "cat" "type" "exit" ];
  programs.bash.initExtra = ''
    # tldr
    if command -v tldr >/dev/null; then
      complete -F _command tldr
    fi
  '';

  # ~/.inputrc
  programs.readline = {
    enable = true;
    bindings = {
      "\\e[Z" = "menu-complete"; # shift+tab
    };
    variables = {
      # https://man.archlinux.org/man/core/readline/readline.3.en#Variables
      completion-ignore-case = true;
      blink-matching-paren = true; # briefly move cursor to opening parenthesis when a closing parenthesis is inserted
      colored-completion-prefix = true; # color common prefix of possible completions ($LS_COLORS)
      colored-stats = true; # displays completions with different colors to indicate file type ($LS_COLORS)
      enable-bracketed-paste = true;
      #match-hidden-files = true;
      #show-all-if-ambiguous = true;
      mark-symlinked-directories = true; # completed names which are symbolic links to directories have a slash appended
    };
  };

  # TODO: move out of minimal?
  imports = [ ../../modules/jump.nix ];
  programs.jump.enableBash = true;

  #programs.zoxide.enable = true;
  #programs.zoxide.options = [ "--cmd cd --cmd dc" ];

  # # https://carapace-sh.github.io/carapace-bin/completers.html
  # programs.carapace.enable = true; # completions
  # programs.bash.sessionVariables.CARAPACE_EXCLUDES = "just"; # comma-separated

  # i can't type
  home.shellAliases.sl = "ls";
  #home.shellAliases.dc = "cd";

  home.shellAliases.cdtmp = ''cd "$(mktemp -d)"'';

  programs.direnv.enable = true;
  programs.direnv.nix-direnv.enable = true;

  home.shellAliases.kaomoji = "curl -s 'https://kaomoji.ru/en/' | htmlq tr td span --text | grep . | shuf | head -n1";

  # home.shellAliases.hman = "man -H "; # HTML opened in a browser
  home.shellAliases.tmux = "systemd-run --scope --user tmux"; # detach from ssh scope, surviving logout
  # home.shellAliases.shpool = "systemd-run --scope --user shpool"; # detach from ssh scope, surviving logout

  # TODO: ps1?
  home.shellAliases.pwd-fqdn = ''echo "$(whoami)@$(hostname -f):$(printf "%q" "$(realpath .)/")"'';

  home.shellAliases.killall = "killall -v";

  # bin = "python -c 'import sys; sys.stdout.write(sys.stdin.read().strip())' | curl -is -X POST https://i.kuklef.se/upload -F content=@- -F expiration=10d | grep '^location:' | cut -d' ' -f2-";
  home.shellAliases.termbin = "${lib.getExe pkgs.netcat} termbin.com 9999";

  # https://github.com/nwtgck/piping-server
  home.shellAliases.httppipe-in  = mkArgsAlias ''set -x; curl -T - https://ppng.io/"''${1:-ijoh1aeJiXail9gahlah}"''; # pwgen ftw
  home.shellAliases.httppipe-out = mkArgsAlias ''set -x; curl https://ppng.io/"''${1:-ijoh1aeJiXail9gahlah}"''; # pwgen ftw

  # old alias, just learn to use ncdu lol
  home.shellAliases.dush = "du -shc * | sort -h";
  home.shellAliases.dushd = "du -shc * .[!.]?* | sort -h";

  home.shellAliases.open = "xdg-open";
  home.shellAliases.rwhich   = mkArgsAlias ''which "$@" | xargs -d"\n" realpath --'';
  home.shellAliases.rwhichcd = mkArgsAlias ''
      local dir=$(which "$@" | xargs -d"\n" realpath -- | cut -d/ -f-4 | grep ^/nix/store/ | fzf --height=15 ||:)
      if [[ -n "$dir" && -d "$dir" ]]; then
        # history -s "$(printf "cd %q\n" "$dir")"
        # history -a
        cd "$dir"
      fi
  '';

  home.shellAliases.grep = "grep --color";
  home.shellAliases.diff = "diff -u --color"; # eyo
  home.shellAliases.ip = "ip -br -color";
  home.shellAliases.watch = "watch -c "; # colors
  home.shellAliases.sudo = "sudo "; # support aliases
  home.shellAliases.xargs = "xargs "; # support aliases

  # old -> new
  home.shellAliases.vimv = "edir";

  # Be conservative with files
  # --preserver-root is for GNU versions, preventing changing perms on /
  home.shellAliases.chown = "chown --preserve-root";
  home.shellAliases.chmod = "chmod --preserve-root";
  home.shellAliases.chgrp = "chgrp --preserve-root";
  home.shellAliases.rm = "rm --preserve-root";
  # prompt if deleting more than 3 files at a time
  #home.shellAliases.rm = "rm -i";
  #home.shellAliases.mv = "mv -i";
  #home.shellAliases.cp = "cp -i";

  home.shellAliases.fda = "fd --no-ignore --hidden";
  home.shellAliases.afd = "fd --no-ignore --hidden";

  home.shellAliases.drg = lib.mkIf (config.programs.git.enable && config.programs.delta.enable) (
    # mkArgsAlias ''rg "$@" --json | delta''
    mkArgsAlias ''rg "$@" --json | jq -s 'sort_by(.data.path)|.[]' -c | delta''
  );

  # via nix-index-with-full-db
  # home.shellAliases.nlocate = "nix-locate --top-level --regex";
  # home.shellAliases.nlocateo = mkArgsAlias ''nix-locate --top-level --regex "$@" | tr -s ' ' | cut -d' ' -f4'';
  home.shellAliases.nlocate = "nix-locate --regex";
  # home.shellAliases.nlocatea = "nix-locate --minimal --regex";
  # home.shellAliases.nlocateo = mkArgsAlias ''nix-locate --regex "$@" | tr -s ' ' | cut -d' ' -f4'';

  # home.shellAliases.swgl = "env LIBGL_ALWAYS_SOFTWARE=true GALLIUM_DRIVER=softpipe __GLX_VENDOR_LIBRARY_NAME=mesa ";
  home.shellAliases.swgl = "env LIBGL_ALWAYS_SOFTWARE=true GALLIUM_DRIVER=llvmpipe __GLX_VENDOR_LIBRARY_NAME=mesa ";

  home.packages = lib.filter (x: x != null) (with pkgs; [
    rsync
    bind.dnsutils # dig
    # dogdns # dog, better dig
    edir # better vimv
    curl
    wget

    entr # live reload
    btop # better htop
    faketty
    xe # better xargs
    sd # better sed
    fd # better find
    ripgrep # 'rg'
    jq # json query
    htmlq # html css queries
    ncdu # du tui
  ]);

  home.file.".config/ncdu/config".text = ''
    --one-file-system
    --exclude-kernfs
    --color dark
  '';

}
