it looks a bit strange that expand_envvar needs to behave differently if inside CMD_IF or not
If I read your sample test correctly, the issue in Wine stems from
'if !WINE_FOO!==' being expanded and then parsed again in the if-command leading to a syntax error because of the space introduced in WINE_FOO expansion
it rather looks like the delayed expansion is wrong as the !WINE_FOO! should rather be expanded **after** parsing
that's likely a way larger change