# POSIX sh with mktemp -d available

tempdir=""
cleanup() { test "${tempdir}" && rm -rf -- "${tempdir}"; }
trap cleanup EXIT HUP INT TERM # in Bash you can just trap cleanup EXIT
tempdir=$(mktemp -d -- "${TMPDIR:-/tmp}/XXXXXX") ||
  { echo 'Error creating a temporary directory' >&2; exit 1; }

# don't forget to call "cleanup" at the end of your script, if you aren't in bash.
