Subject: Bug#145: Makefile uses broken substitution

From: Jan Engelhardt <[email-masked]>
Date: Sun, 17 Mar 2013 13:21:38 +0100 (CET)

Package: nx-libs
Version: 3.5.0.17

Can't use ${x//...}
From: Jan Engelhardt <[email-masked]>
Date: 2013-03-06 23:10:19.962925458 +0100

build: avoid bash4-specific substitution

The ${a/b/c} substitution is not POSIX compatible. Additionally, in
bash 3.x, quotes do not escape slashes. This causes screwed up
installation paths.

	SLES 11, bash-3.2-147.9.13
	$ dirname="foo/bar"
	$ echo ${dirname//"foo/bar"/"omg/nei"}
	bar/omg/nei/bar

	openSUSE 12.2, bash-4.2-51.6.1
	$ dirname="foo/bar"
	$ echo ${dirname//"foo/bar"/"omg/nei"}
	omg/nei

	openSUSE 12.2, dash-0.5.7-5.1.2.x86_64
	$ dirname="foo/bar"
	$ echo ${dirname//"foo/bar"/"omg/nei"}
	dash: 2: Bad substitution
---
 Makefile   |   12 ++++++------
 replace.sh |   27 +++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 6 deletions(-)

Index: nx-libs-3.5.0.17/Makefile
===================================================================
--- nx-libs-3.5.0.17.orig/Makefile
+++ nx-libs-3.5.0.17/Makefile
@@ -95,23 +95,23 @@ install-full:
 	cp -aL nx-X11/exports/include/* nx-X11/.build-exports/include
 
 	# copy libs (for libnx-x11), we want the targets of the links
-	find nx-X11/exports/lib/ | grep -F ".so" | while read libpath; do \
+	. replace.sh; set -x; find nx-X11/exports/lib/ | grep -F ".so" | while read libpath; do \
 	    libfile=$$(basename $$libpath); \
 	    libdir=$$(dirname $$libpath); \
 	    link=$$(readlink $$libpath); \
 	\
-	    mkdir -p $${libdir//exports/.build-exports}; \
-	    cp -a $${libpath/$$libfile/$$link} $${libdir//exports/.build-exports}; \
+	    mkdir -p "$$(string_rep "$$libdir" exports .build-exports)"; \
+	    cp -a "$$(string_rep "$$libpath" "$$libfile" "$$link")" "$$(string_rep "$$libdir" exports .build-exports)"; \
 	done;
 
 	$(INSTALL_DIR) $(DESTDIR)$(NXLIBDIR)/X11
 	$(COPY_SYMLINK) nx-X11/.build-exports/lib/*.so* $(DESTDIR)$(NXLIBDIR)/X11/
 
-	find nx-X11/.build-exports/include/ -type d | grep -v "include/X11/bitmaps" | \
+	. replace.sh; set -x; find nx-X11/.build-exports/include/ -type d | grep -v "include/X11/bitmaps" | \
 	    while read dirname; do \
-	        $(INSTALL_DIR) $${dirname//"nx-X11/.build-exports/include"/"$(DESTDIR)$(PREFIX)/include/nx"}; \
+	        $(INSTALL_DIR) "$$(string_rep "$$dirname" nx-X11/.build-exports/include "$(DESTDIR)$(PREFIX)/include/nx")"; \
 	        $(INSTALL_FILE) $${dirname}/*.h \
-	                        $${dirname//"nx-X11/.build-exports/include"/"$(DESTDIR)$(PREFIX)/include/nx"}/ || true; \
+	                        "$$(string_rep "$$dirname" nx-X11/.build-exports/include "$(DESTDIR)$(PREFIX)/include/nx")"/ || true; \
 	    done; \
 
 	$(INSTALL_DIR) $(DESTDIR)$(PREFIX)/include/nx/X11/bitmaps
Index: nx-libs-3.5.0.17/replace.sh
===================================================================
--- /dev/null
+++ nx-libs-3.5.0.17/replace.sh
@@ -0,0 +1,27 @@
+# from http://mywiki.wooledge.org/BashFAQ/021
+
+string_rep()
+{
+	# initialize vars
+	in=$1
+	unset out
+
+	# SEARCH must not be empty
+	test -n "$2" || return
+
+	while true; do
+		# break loop if SEARCH is no longer in "$in"
+		case "$in" in
+			*"$2"*) : ;;
+			*) break;;
+		esac
+
+		# append everything in "$in", up to the first instance of SEARCH, and REP, to "$out"
+		out=$out${in%%"$2"*}$3
+		# remove everything up to and including the first instance of SEARCH from "$in"
+		in=${in#*"$2"}
+	done
+
+	# append whatever is left in "$in" after the last instance of SEARCH to out, and print
+	printf '%s%s\n' "$out" "$in"
+}