The -f flag doesn't work in BSD readlink. Replace it with a function.
BSD uniq doesn't accept long name options. Use short names.
sed in OSX doesn't accept unbounded repeated matches.
sed in OSX chokes on b command when not followed by newlines.

Also, cat is not needed.

As suggested by Scrutinizer @ http://stackoverflow.com/a/15470635/151465
Merge pull request #1 from Raimondi/support-osx

Add support for OSX