#!/bin/bash

# Helper function to find which file contains a config option
findConfigFileForOption()
{
    local config_option="$1"

    # Global configs first. Dotfiles (reconcile backups, markers) are excluded —
    # finding a key in a .bak before its real file would write the user's
    # config update into the backup and silently lose it.
    for config_file in $(find "$configs_dir" -type f ! -name ".category" ! -name ".*" ! -name "*.bak"); do
        if [ -f "$config_file" ] && grep -q "^$config_option=" "$config_file"; then
            echo "$config_file"
            return 0
        fi
    done

    # Fall back to per-app configs (CFG_<APP>_*). Without this, tools that
    # mutate per-app CFGs (password reset, shortcut manager) silently no-op.
    if [[ -n "$containers_dir" && -d "$containers_dir" ]]; then
        for config_file in "$containers_dir"*/*.config; do
            if [ -f "$config_file" ] && grep -q "^$config_option=" "$config_file"; then
                echo "$config_file"
                return 0
            fi
        done
    fi
    return 1
}
