bin/rebuild: rework locking so it works on OS X

Try using flock, if exists, and fall back to lockfile otherwise.