Something should clean up $TMP, the most robust way is to add the following right after mktemp:

    trap "rm -f $TMP" EXIT