From dd9e51a7d0e62d03820d04c54bdc9400c49b1d92 Mon Sep 17 00:00:00 2001
From: Thierry Parmentelat <thierry.parmentelat@inria.fr>
Date: Mon, 10 Jun 2013 12:53:40 +0200
Subject: [PATCH] try to properly handle symlinks; relative symlinks are
 untouched, absolute are changed using canonical dest (and canonical
 buildroot)

---
 rpm/rvm-ruby.spec | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/rpm/rvm-ruby.spec b/rpm/rvm-ruby.spec
index 63189a7..6a40fba 100644
--- a/rpm/rvm-ruby.spec
+++ b/rpm/rvm-ruby.spec
@@ -152,8 +152,22 @@ slashes=$(printf "%s" "${ch// //}")
 find $br -type f -print0 | xargs -0 sed -i "s,$br,$slashes,g"
 
 # Fix symlinks with bad path
-for f in $(find $br -type l |grep "$br"); do
-    ln -sfn $(echo $f | sed "s,^$br,,") $f
+# the canonical path of the build root
+brc=$(readlink -f $br)
+for f in $(find $br -type l); do
+    # some symlinks are relative, in which case we want to preserve them
+    # do *not* mention -f here as it would always return an absolute path
+    dest=$(readlink $f)
+    # relative symlinks have $dest not starting with a /
+    first_step=$(echo $dest | cut -d / -f1)
+    # absolute paths have a void first_step 
+    if [ -z "$first_step" ] ; then
+	# destination is an absolute path, let's fix it
+	# call readlink with -f so all symlinmks are solved
+	# and so we can reliably substitute $brc that is also canonicalized
+	destc=$(readlink -f $f | sed -e "s,^$brc,,")
+	ls -sfn $destc $f
+    fi
 done
 
 find $br -maxdepth 1 -name '.*' -exec rm -rf {} \;
-- 
2.47.0