Fixes #2151 - start CouchDB from symlinks (#2152)

diff --git a/rel/files/couchdb.in b/rel/files/couchdb.in
index b3c7e98..f64c0f8 100755
--- a/rel/files/couchdb.in
+++ b/rel/files/couchdb.in
@@ -12,7 +12,17 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-COUCHDB_BIN_DIR=$(cd "${0%/*}" && pwd)
+canonical_readlink ()
+  {
+  cd $(dirname $1);
+  FILE=$(basename $1);
+  if [ -h "$FILE" ]; then
+    canonical_readlink $(readlink $FILE);
+  else
+    echo "$(pwd -P)";
+  fi
+}
+COUCHDB_BIN_DIR=$(canonical_readlink $0)
 ERTS_BIN_DIR=$COUCHDB_BIN_DIR/../
 cd "$COUCHDB_BIN_DIR/../"
 
diff --git a/rel/overlay/bin/remsh b/rel/overlay/bin/remsh
index 2ac421b..b8946ac 100755
--- a/rel/overlay/bin/remsh
+++ b/rel/overlay/bin/remsh
@@ -12,7 +12,17 @@
 # License for the specific language governing permissions and limitations under
 # the License.
 
-COUCHDB_BIN_DIR=$(cd "${0%/*}" && pwd)
+canonical_readlink ()
+  {
+  cd $(dirname $1);
+  FILE=$(basename $1);
+  if [ -h "$FILE" ]; then
+    canonical_readlink $(readlink $FILE);
+  else
+    echo "$(pwd -P)";
+  fi
+}
+COUCHDB_BIN_DIR=$(canonical_readlink $0)
 ERTS_BIN_DIR=$COUCHDB_BIN_DIR/../
 ROOTDIR=${ERTS_BIN_DIR%/*}
 START_ERL=$(cat "$ROOTDIR/releases/start_erl.data")