apply_options()
{
    # Write all the options out to replace the bashburnrc.
    local bbtempfile=$(mktemp bashburn_apply_opts.XXXXXX)
    local k
    
    # Make sure BashBurn knows it is configured.
    for k in "${BB_KEYWORDS[@]}"; do
        printf "%s: %s\n" "$k" "${!k}"
    done > "${bbtempfile}"
    
    cp "${bbtempfile}" "$BBCONFFILE"
    rm "${bbtempfile}"
}

conf_set_aval()
{
    local -a desc=( "$@" )
    local line varname nvar nval k
    local -i found didsomething=0
    local tmpfile=$(mktemp 'bbcfg.XXXXXX')

    while IFS=: read -r varname rest; do
        found=0
        for (( i=0; i < ${#desc[@]}; i++ )); do
            IFS='|' read -r nvar nval <<< "${desc[i]}"
            if [[ "$nvar" == "$varname" ]]; then
                declare -g "$nvar"="$nval"
                found=1
                didsomething=1
                
                # Handle special side effects
                if [[ "$nvar" == "BBLANG" ]]; then
                    source_language_modules
                    define_global_menus
                fi
                break
            fi
        done
        
        if (( found )); then
            printf "%s: %s\n" "$nvar" "${!nvar}"
        else
            # Print original line (including comments if any)
            # but we need to reconstruct it if we used IFS
            if [[ -z "$varname" && -z "$rest" ]]; then
                echo ""
            elif [[ "$varname" =~ ^# ]]; then
                echo "$varname$rest"
            else
                printf "%s: %s\n" "$varname" "$rest"
            fi
        fi
    done < "$BBCONFFILE" > "$tmpfile"
    
    (( didsomething )) && cp "$tmpfile" "$BBCONFFILE"
    rm "$tmpfile"
}
