--with-statedir compile time option

Mostly interesting to package mantainers but there is some cleanup here as well.

This is the continuation of #1400 and it's ready for review.
Debugging. I intentionally did this so that there were symbols available on which to set a breakpoint or print. Just now read about building with `-ggdb3` flag (didn't know about this one) so not sure (yet) if I can do what you suggest and use this flag to preserve my original intention...