fix build on OS X and merge other pull requests

This pull request applies the patch that MacPorts applies downstream as of https://trac.macports.org/changeset/116483 and also supersedes pull requests #17 and #29 and #30 and #31