# Only execute this file once per shell.
set -q __fish_home_manager_config_sourced; and exit
set -g __fish_home_manager_config_sourced 1


# Add ROCm bin directory to PATH
set -gx PATH "/opt/rocm/bin" $PATH


set --export VOLTA_HOME "$HOME/.volta"
set --export PATH "$VOLTA_HOME/bin" $PATH

set --export EDITOR "nvim"


status is-login; and begin

    # Login shell initialisation


end

status is-interactive; and begin

    # Abbreviations


    # Aliases
    alias che chezmoi
    alias gb 'git branch'
    alias gca 'git commit --amend'
    alias gchd 'git checkout develop'
    alias gchm 'git checkout main'
    alias gcho 'git checkout'
    alias gcnv 'git commit --no-verify'
    alias gd 'git diff'
    alias gdc 'git diff --cached'
    alias gdt 'git diff-tree --no-commit-id --name-only -r'
    alias gf 'git fetch'
    alias gl 'git pull'
    alias gm 'git merge'
    alias gp 'git push'
    alias gpuoh 'git push --set-upstream origin HEAD'
    alias gr 'git remote'
    alias gra 'git remote add'
    alias grr 'git remote remove'
    alias grv 'git remote -v'
    alias gs 'git status'
    alias nv nvim
    # alias yay paru
		alias wo 'pomodoro work'
		alias br 'pomodoro break'

    # Interactive shell initialisation
    fish_config theme choose rose-pine-moon
    set fish_greeting # Disable greeting
    test -f /run/current-system/sw/share/autojump/autojump.fish; and source /run/current-system/sw/share/autojump/autojump.fish
		[ -f /opt/homebrew/share/autojump/autojump.fish ]; and source /opt/homebrew/share/autojump/autojump.fish

    test -f ~/.env; and source ~/.env
		test -f ~/.config/myvars; and source ~/.config/myvars

    nerdfetch

    if test "$TERM" != dumb
		eval (starship init fish)
		fzf --fish | source
    end

    # add completions generated by Home Manager to $fish_complete_path
    begin
        set -l joined (string join " " $fish_complete_path)
        set -l prev_joined (string replace --regex "[^\s]*generated_completions.*" "" $joined)
        set -l post_joined (string replace $prev_joined "" $joined)
        set -l prev (string split " " (string trim $prev_joined))
        set -l post (string split " " (string trim $post_joined))
        set fish_complete_path $prev "/home/thomasgl/.local/share/fish/home-manager_generated_completions" $post
    end


end

# Add user local bin to PATH
if test -d "$HOME/.local/bin"
    fish_add_path "$HOME/.local/bin"
end

# pnpm
switch (uname)
    case Linux
        set -gx PNPM_HOME "/home/thomasgl/.local/share/pnpm"
    case Darwin
        set -gx PNPM_HOME "/Users/thomasglopes/.local/share/pnpm"
end

if not string match -q -- $PNPM_HOME $PATH
    set -gx PATH "$PNPM_HOME" $PATH
end
# pnpm end

set -Ux ENV ~/.config/fish/config.fish

if not set -q SSH_AUTH_SOCK
	# Check if ssh-agent is already running and accessible
	set -l agent_pid (pgrep ssh-agent | head -n 1)

	if test -n "$agent_pid"
		# Try to find existing socket
		set -l sock_path (find /tmp -name "ssh-*" -type d 2>/dev/null | \
		xargs -I {} find {} -name "agent.*" 2>/dev/null | head -n 1)

		if test -n "$sock_path"
			set -gx SSH_AUTH_SOCK "$sock_path"
			set -gx SSH_AGENT_PID "$agent_pid"

			# Test if agent is responsive
			if ssh-add -l >/dev/null 2>&1
				# echo "Connected to existing ssh-agent (PID: $agent_pid)"
				return 0
			else
				# Clean up stale variables
				set -e SSH_AUTH_SOCK SSH_AGENT_PID
			end
		end
	end

	# Start new ssh-agent
	# echo "Starting new ssh-agent..."
	set -l agent_output (ssh-agent -c)

	# Parse output and set variables
	for line in $agent_output
		if string match -qr '^setenv SSH_AUTH_SOCK' "$line"
			set -gx SSH_AUTH_SOCK (string replace -r '^setenv SSH_AUTH_SOCK (.+);$' '$1' "$line" | string trim -c '"')
		else if string match -qr '^setenv SSH_AGENT_PID' "$line"
			set -gx SSH_AGENT_PID (string replace -r '^setenv SSH_AGENT_PID (.+);$' '$1' "$line" | string trim -c '"')
		end
	end

	# Verify agent started successfully
	if test -z "$SSH_AUTH_SOCK" -o -z "$SSH_AGENT_PID"
		# echo "Error: Failed to start ssh-agent" >&2
		return 1
	end

	# echo "ssh-agent started (PID: $SSH_AGENT_PID)"
end


# bun
set --export BUN_INSTALL "$HOME/.bun"
set --export PATH $BUN_INSTALL/bin $PATH

# opencode
fish_add_path ~/.opencode/bin

if test (uname) = Darwin
	if test -f ~/.safe-chain/scripts/init-fish.fish
	 source ~/.safe-chain/scripts/init-fish.fish
	end

# Google Cloud SDK
	if test -f /Users/thomasglopes/google-cloud-sdk/path.fish.inc
	 source /Users/thomasglopes/google-cloud-sdk/path.fish.inc
	end

	set -gx PATH /Users/thomasglopes/.local/bin /Users/thomasglopes/google-cloud-sdk/bin $PATH

	# opencode
	fish_add_path /Users/thomasglopes/.opencode/bin
end

