$OpenBSD: patch-util_kdev_format_source,v 1.1.1.1 2014/07/09 11:29:15 zhuk Exp $
Make it more portable and error-prone.
REVIEW: https://git.reviewboard.kde.org/r/119192/
--- util/kdev_format_source.orig	Sat Jul 27 18:14:47 2013
+++ util/kdev_format_source	Sat Jul 27 18:29:16 2013
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 
 # Author: David Nolden <david.nolden.kdevelop@art-master.de>
 # This script is made available under the GPLv2 licence.
@@ -19,11 +19,11 @@
 #
 # The contents is processed in linear order, and the first matching command is used.
 
-ORIGFILE=$1
-TMPFILE=$2
+ORIGFILE="$1"
+TMPFILE="$2"
 
-if ! [ "$ORIGFILE" ]; then
-    echo "Usage: $(basename $0) [FILE] [TEMPFILE]"
+if [ -z "$ORIGFILE" ]; then
+    echo "Usage: $(basename $0) FILE [TEMPFILE]"
     echo ""
     echo "Where FILE represents the original location of the formatted contents,"
     echo "and TEMPFILE is used as the actual, potentially different,"
@@ -31,34 +31,34 @@ if ! [ "$ORIGFILE" ]; then
     exit
 fi
 
-ORIGFILE=$(readlink -f $ORIGFILE)
+ORIGFILE="$(readlink -f \"$ORIGFILE\")"
 
-if ! [ $TMPFILE ]; then
+if [ -z "$TMPFILE" ]; then
     echo "No tempfile given, formatting the original file"
-    TMPFILE=$ORIGFILE
+    TMPFILE="$ORIGFILE"
 else
-    TMPFILE=$(readlink -f $TMPFILE)
+    TMPFILE="$(readlink -f \"$TMPFILE\")"
 fi
 
 # Helper: Returns the relative path from a given source directory to a target path
 function relativePath {
-    source=$1
-    target=$2
+    source="$1"
+    target="$2"
 
-    common_part=$source
+    common_part="$source"
     back=
     while [ "${target#$common_part}" = "${target}" ]; do
-    common_part=$(dirname $common_part)
-    back="../${back}"
+        common_part="$(dirname \"$common_part\")"
+        back="../${back}"
     done
 
-    echo ${back}${target#$common_part/}
+    echo "${back}${target#$common_part/}"
 }
 
 # Go to the directory of the original file, and start searching for "format_sources" files upwards
-cd $(dirname $ORIGFILE)
+cd -- "$(dirname $ORIGFILE)"
 
-while ! [ "$(pwd)" == "/" ]; do
+while [ "$(pwd)" != "/" ]; do
 
     if [ -e "format_sources" ]; then
         echo "found $(readlink -f format_sources)"
@@ -69,29 +69,23 @@ while ! [ "$(pwd)" == "/" ]; do
 #             echo "Line: $line"
             # Split by the ":" which is the delimiter between wildcards
             IFS="\:"
-            array=
-            pos=0
             
             # remove leading whitespace
             line="${line#"${line%%[![:space:]]*}"}"
 
-            if [[ "$line" == \#* ]] || ! [ "$line" ]; then 
+            if [[ "X$line" = X\#* ]] || [ -z "$line" ]; then 
                 # Ignore lines starting with #
                 # Those can be used for comments.
                 # Also ignore empty lines
                 continue
             fi
             
-            for item in $line;
-            do
-                array[$pos]=$item
-                pos=$(($pos+1))
-            done
+	    set -- $line
             
-            if [ $pos == "2" ]; then
+            if [ $# -eq 2 ]; then
                 # We found the correct syntax with "wildcards : command"
-                WILDCARDS=${array[0]}
-                COMMAND=${array[1]}
+                WILDCARDS="$1"
+                COMMAND="$2"
                 
                 MATCHED=0
                 
@@ -106,7 +100,7 @@ while ! [ "$(pwd)" == "/" ]; do
                     set +f
                     # This if-command does wildcard matching
 #                     echo "matching $RELATIVE_ORIGFILE and $WILDCARD"
-                    if [[ "$RELATIVE_ORIGFILE" == $WILDCARD ]]; then
+                    if [[ "X$RELATIVE_ORIGFILE" = X$WILDCARD ]]; then
                         echo "matched $RELATIVE_ORIGFILE with wildcard $WILDCARD, using command \"$COMMAND\""
                         eval $COMMAND
                         exit
@@ -116,9 +110,9 @@ while ! [ "$(pwd)" == "/" ]; do
                 set +f
             fi
             
-            if [ $pos == "1" ]; then
+            if [ $# -eq 1 ]; then
                 # We found the simple syntax without wildcards, and only with the command
-                COMMAND=${array[0]}
+                COMMAND="$1"
                 echo "matched without wildcard, using command $COMMAND"
                 eval $COMMAND
                 exit
