Mac OS X fix, general *nix script improvements

After the initial `mktemp` Mac OS X fix, I added a few more things. Some are for general stability, but also extract the key and databases like the Windows version. Left the separate commits, hope you don't mind.