Correct export.sh failure exit code and cleanup
Correct export.sh failure exit code and cleanup

In failure cases, when idf_export_main() returns 1 (failure),
the export.sh script returns 0 (success). This makes it very
difficult to detect failure when writing scripts that use
export.sh. Furthermore, idf_export_main() does not clean up all
internal variables and functions in failure cases.

Move all cleanup steps into a cleanup function and pass
the return value from idf_export_main() to the cleanup
function so it can return with that same return value.

Signed-off-by: Marek Fiala <marek.fiala@espressif.com>

Closes https://github.com/espressif/esp-idf/pull/5744

espressif/esp-idf,espressif/esp-idf,espressif/esp-idf,espressif/esp-idf