Patch provided by Hrvoje Habjanic in http://mail-index.netbsd.org/pkgsrc-users/2006/11/02/0003.html
patches provided by Tobias Nygren <tnn at netilium dot org> with minor changes by me.