buildenv.sh: clean up each tmpdir once

...instead of cleaning up the last one once for each tmpdir.

Commit 63e8d6321 changed the command registered atexit from
an explict "rm -rf '${DIR}'" to a call to a cleanup_tempdir
function, with the function containing the remove command.
As, however, the function name is not unique (in fact, it is
a constant), that function will get overridden at each invocation
of the tempdir function. The fact that the function is registered
several times with atexit doesn't help, as it will always remove
the last created temporary directory. Fix this, by creating function
names that encode the directory to be removed in the name. Fixes #1466.

--
Change-Id: I833aef8ee5423412f058e74c8c9e2f4bb53a0cba
Reviewed-on: https://bazel-review.googlesource.com/#/c/3955
MOS_MIGRATED_REVID=126406196
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index 1ca2c71..6efa355 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -99,8 +99,9 @@
   local tmp=${TMPDIR:-/tmp}
   local DIR="$(mktemp -d ${tmp%%/}/bazel.XXXXXXXX)"
   mkdir -p "${DIR}"
-  eval "cleanup_tempdir() { rm -rf '${DIR}'; }"
-  atexit cleanup_tempdir
+  local DIRBASE=$(basename "${DIR}")
+  eval "cleanup_tempdir_${DIRBASE}() { rm -rf '${DIR}'; }"
+  atexit cleanup_tempdir_${DIRBASE}
   NEW_TMPDIR="${DIR}"
 }
 tempdir