revised mainly to make compatible with freebsd, since openacs no longer in ports.

Logic and variable dependencies required moving some code segments around. I reformatted for legiblility then realized reformatting may complicate acceptance of this pull. You're welcome to incorporate changes without accepting the pull. Cheers.