make compilable on Mac OS X

This change fixes the compilation problem on Mac OS X, where standard sed isn't working
with given commands.  Code first tries to detect OS, and after that, do we have GNU
sed (as gsed).

If code is too verbose, feel free to remove not necessary parts