# fnva environment setup (fnva env --shell fish | source)

# --- Auto-restore on startup ---
set -g _fnva_autoload_done false
function fnva_autoload_default
    if test $_fnva_autoload_done = true; return; end
    set -g _fnva_autoload_done true

    set envs_file "$HOME/.fnva/current_envs.toml"
    if test -f "$envs_file"; and command -v fnva >/dev/null 2>&1
        for line in (cat "$envs_file")
            set -l match (string match -r '^\s*(\w+)\s*=\s*"([^"]*)"' -- $line)
            test (count $match) -ge 3; or continue
            set key $match[2]
            set value $match[3]
            test -n "$value"; or continue
            set env_script (fnva $key use $value 2>/dev/null)
            if test -n "$env_script"; eval "$env_script"; end
        end
    end
end

fnva_autoload_default

# --- Shell wrapper (auto-source on use) ---
function fnva
    if test (count $argv) -ge 2; and string match -q -r "^(java|llm|cc)$" $argv[1]; and test $argv[2] = "use"
        set temp_file (mktemp)
        command fnva $argv > $temp_file
        source $temp_file
        rm -f $temp_file
    else
        command fnva $argv
    end
end
