fish_add_path /opt/homebrew/bin
fish_add_path /opt/homebrew/coreutils/libexec/gnubin
fish_add_path /usr/local/opt/coreutils/libexec/gnubin
fish_add_path /usr/local/bin
fish_add_path /snap/bin
fish_add_path $HOME/.local/bin
fish_add_path $HOME/.cargo/bin
fish_add_path $HOME/.go/bin
set -gx GOPATH $HOME/.go
set -gx EDITOR nvim
set fish_color_valid_path
set -g fish_greeting


function __tmux_environment_value --argument-names name
    # The session environment is refreshed on every attach (update-environment),
    # while the global one is frozen at server start. Prefer the session value,
    # treat its "-NAME" removal marker as unset, and only fall back to the
    # global environment when the session has no entry at all.
    set -l value (tmux show-environment "$name" 2>/dev/null)
    if test -z "$value"
        set value (tmux show-environment -g "$name" 2>/dev/null)
    end
    if string match -q -- "$name=*" "$value"
        string replace -r "^$name=" "" -- "$value"
    end
end

function __is_1password_ssh_auth_sock --argument-names sock
    test -n "$sock"; or return 1

    string match -q -- "$HOME/.1password/agent.sock" "$sock"
    or string match -q -- "$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" "$sock"
end

function __is_local_macos_ssh_auth_sock --argument-names sock
    test -n "$sock"; or return 1

    string match -q -- "/var/run/com.apple.launchd.*" "$sock"
    or string match -q -- "/private/tmp/com.apple.launchd.*" "$sock"
end

function __find_forwarded_ssh_auth_sock
    for sock in /tmp/ma-*/* /tmp/ssh-*/* /private/tmp/ssh-*/*
        if test -S "$sock"
            echo "$sock"
            return
        end
    end
end

function __refresh_ssh_auth_sock
    set -l tmux_sock
    set -l in_ssh_session 0

    if set -q TMUX
        set tmux_sock (__tmux_environment_value SSH_AUTH_SOCK)
        set -l tmux_ssh_connection (__tmux_environment_value SSH_CONNECTION)
        if test -n "$tmux_ssh_connection"
            set -gx SSH_CONNECTION "$tmux_ssh_connection"
            set in_ssh_session 1
        else
            set -e SSH_CONNECTION SSH_CLIENT SSH_TTY
        end
    else if set -q SSH_CONNECTION
        set in_ssh_session 1
    end

    if test "$in_ssh_session" = 1
        if test -n "$tmux_sock"; and test -S "$tmux_sock"; and not __is_1password_ssh_auth_sock "$tmux_sock"; and not __is_local_macos_ssh_auth_sock "$tmux_sock"
            set -gx SSH_AUTH_SOCK "$tmux_sock"
            return
        end

        if set -q SSH_AUTH_SOCK; and test -S "$SSH_AUTH_SOCK"; and not __is_1password_ssh_auth_sock "$SSH_AUTH_SOCK"; and not __is_local_macos_ssh_auth_sock "$SSH_AUTH_SOCK"
            return
        end

        set -l forwarded_sock (__find_forwarded_ssh_auth_sock)
        if test -n "$forwarded_sock"
            set -gx SSH_AUTH_SOCK "$forwarded_sock"
            return
        end

        set -e SSH_AUTH_SOCK
        return
    end

    if test -S "$HOME/.1password/agent.sock"
        set -gx SSH_AUTH_SOCK "$HOME/.1password/agent.sock"
        return
    end

    if test -n "$tmux_sock"; and test -S "$tmux_sock"
        set -gx SSH_AUTH_SOCK "$tmux_sock"
    end
end

__refresh_ssh_auth_sock

function __refresh_ssh_auth_sock_before_command --on-event fish_preexec
    __refresh_ssh_auth_sock
end

# Make Tide show context for orb/SSH sessions that set SSH_CONNECTION but not SSH_TTY
if set -q SSH_CONNECTION; and not set -q SSH_TTY
    set -gx SSH_TTY (tty)
end

# Commands to run in interactive sessions can go here
if status is-interactive
		if type -q atuin; and test -s ~/.local/share/atuin/key
			atuin init fish --disable-up-arrow | source
		end

	if type -q jump
		# initialize jump cd
		jump shell fish --bind=z | source
	end
end
# aliases
alias b "bat -p"
alias l "ls -la"
alias lll "ls -latr"
alias lg "lazygit"
alias claude "env -u TMUX claude"

# abbreviations
abbr lsn littlesnitch
abbr nv nvim
abbr vim nvim
abbr vi nvim

# functions
function sss
    python3 ~/.config/scripts/ss.py --model gpt-5.2 $argv
end

function ss
	#python3 ~/.config/scripts/ss.py $argv
	python3 ~/.config/scripts/llm.py cmd $argv
end

if type -q ip
    function ip
        command ip --color=auto $argv
    end
end

function s
    __kitty_tab_set (__kitty_ssh_host $argv)
    # auto-tmux when called as: s <host> <session>
    if test (count $argv) -eq 2; and not string match -q -- '-*' $argv[1]; and not string match -q -- '-*' $argv[2]
        set -l host $argv[1]
        set -l session $argv[2]
        if test "$TERM" = xterm-kitty
            kitten ssh $host -t "tmux new-session -A -s $session"
        else
            command ssh $host -t "tmux new-session -A -s $session"
        end
        __kitty_tab_reset
        return
    end

    if test "$TERM" = xterm-kitty
        kitten ssh $argv
    else
        command ssh $argv
    end
    __kitty_tab_reset
end

function m
    __kitty_tab_set (__kitty_ssh_host $argv)
    # auto-tmux when called as: m <host> <session>
    if test (count $argv) -eq 2; and not string match -q -- '-*' $argv[1]; and not string match -q -- '-*' $argv[2]
        set -l host $argv[1]
        set -l session $argv[2]
        command mosh -A $host -- env KITTY_MOSH=1 tmux new-session -A -s $session
        __kitty_tab_reset
        return
    end

    if contains -- -- $argv
        command mosh -A $argv
    else
        command mosh -A $argv -- env KITTY_MOSH=1 sh -lc 'exec "${SHELL:?}" -l'
    end
    __kitty_tab_reset
end


function icat
    if test "$TERM_PROGRAM" = "WezTerm"
        wezterm imgcat $argv
	else if test "$TERM_PROGRAM" = "ghostty"
		env TERM=xterm-kitty viu $argv
    else
        kitty +kitten icat --align=left $argv
    end
end

function ils
	if test (count $argv) -eq 0
        # 1. Find "Fast" images (JPG, PNG, etc.) - Case Insensitive
        set -l fast_imgs (find . -maxdepth 1 -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" -o -iname "*.gif" -o -iname "*.webp" -o -iname "*.svg" \) | sort)

        # 2. Find "Slow" RAW images (ARW, DNG, etc.)
        # We search for these separately so we can append them to the end of the list

        set -l slow_imgs (find . -maxdepth 1 -type f \( -iname "*.arw" -o -iname "*.raw" -o -iname "*.cr2" -o -iname "*.nef" \) | sort)

        # Combine them: Fast first, Slow last
        set argv $fast_imgs $slow_imgs
    end

    if test (count $argv) -gt 0
        # Move cursor to bottom to prevent scrolling glitches
        tput cup (tput lines) 0
    # -g 5      : 5 images per row (The Grid!)
    # -p k      : Force Kitty protocol (Cleanest for Ghostty)
    # --title   : Show filename headers
    # --upscale=0 : Don't stretch small icons
    # --center  : Center the whole grid in the terminal
    timg -p k --grid 5 --frames=1 --title --center $argv 2>/dev/null
	else
        echo "No images found."
    end
end

function winbox
	~/dev/winbox/run.sh&
end


if test -e $HOME/.nix-profile/etc/profile.d/nix.fish
    source $HOME/.nix-profile/etc/profile.d/nix.fish
end

# The next line updates PATH for the Google Cloud SDK.
if [ -f ~/dev/google-cloud-sdk/path.fish.inc ]
	source ~/dev/google-cloud-sdk/path.fish.inc
end


if type -q direnv
    direnv hook fish | source
end
if [ -f ~/.config/op/plugins.sh ]
	source ~/.config/op/plugins.sh
end

#fish_vi_key_bindings

# bun
set -gx BUN_INSTALL "$HOME/.bun"
fish_add_path $HOME/.bun/bin
if test -f "$HOME/.cargo/env.fish"
    source "$HOME/.cargo/env.fish"
end

fish_add_path $HOME/dev/screenpipe/target/debug

# Added by LM Studio CLI (lms)
fish_add_path $HOME/.cache/lm-studio/bin

# Added by OrbStack: command-line tools and integration
# This won't be added again if you remove it.
source ~/.orbstack/shell/init.fish 2>/dev/null || :

fish_add_path $HOME/.antigravity/antigravity/bin

# Added by OrbStack: command-line tools and integration
# This won't be added again if you remove it.
source ~/.orbstack/shell/init2.fish 2>/dev/null || :

# GitHub MCP plugin auth — pull PAT from keychain (avoids plaintext in config files)
set -gx GITHUB_PERSONAL_ACCESS_TOKEN (security find-generic-password -s codex-github-mcp-pat -w 2>/dev/null)


# >>> grok installer >>>
fish_add_path $HOME/.grok/bin
# <<< grok installer <<<

# mimocode
fish_add_path /Users/anton/.mimocode/bin

# kimi-code
fish_add_path -g "/Users/anton/.kimi-code/bin"
