From 75c203a1f05748ff877e986148f43e8e792edb16 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 11 Mar 2014 20:48:47 +0100
Subject: [PATCH 1/3] handle existing symlink but wrong source

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 cdist/conf/type/__link/explorer/state | 10 +++++++---
 cdist/conf/type/__link/gencode-remote |  9 +++++----
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/cdist/conf/type/__link/explorer/state b/cdist/conf/type/__link/explorer/state
index a9220a3c..48278d9e 100755
--- a/cdist/conf/type/__link/explorer/state
+++ b/cdist/conf/type/__link/explorer/state
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# 2012 Steven Armstrong (steven-cdist at armstrong.cc)
+# 2012-2014 Steven Armstrong (steven-cdist at armstrong.cc)
 #
 # This file is part of cdist.
 #
@@ -34,8 +34,12 @@ case "$type" in
    symbolic)
       cd "$destination_dir"
       source_is=$(ls -l "$destination" | sed 's/.*-> //g')
-      if [ -h "$destination" -a "$source_is" = "$source" ]; then
-         echo present
+      if [ -h "$destination" ]; then
+         if [ "$source_is" = "$source" ]; then
+            echo present
+         else
+            echo wrongsource
+         fi
       else
          echo absent
       fi
diff --git a/cdist/conf/type/__link/gencode-remote b/cdist/conf/type/__link/gencode-remote
index cbdfd30f..7582863f 100755
--- a/cdist/conf/type/__link/gencode-remote
+++ b/cdist/conf/type/__link/gencode-remote
@@ -1,7 +1,7 @@
 #!/bin/sh
 #
 # 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
-# 2013 Steven Armstrong (steven-cdist at armstrong.cc)
+# 2013-2014 Steven Armstrong (steven-cdist at armstrong.cc)
 #
 # This file is part of cdist.
 #
@@ -18,9 +18,6 @@
 # You should have received a copy of the GNU General Public License
 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
 #
-#
-# Mostly a wrapper for ln
-#
 
 destination="/$__object_id"
 
@@ -55,6 +52,10 @@ rm -rf "$destination"
 DONE
         fi
 
+        if [ "$state_is" = "wrongsource" ]; then
+            printf 'rm -f "%s"\n' "$destination"
+        fi
+
         # create our link
         cat << DONE
 ln ${lnopt} -f "$source" "$destination"

From 301b4e18ff8fc87f2a10d0c471c93df55fe0abc4 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 11 Mar 2014 21:07:20 +0100
Subject: [PATCH 2/3] either to it all, or fail; echo and cat suck, printf ftw!

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 cdist/conf/type/__link/gencode-remote | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/cdist/conf/type/__link/gencode-remote b/cdist/conf/type/__link/gencode-remote
index 7582863f..9e7831c7 100755
--- a/cdist/conf/type/__link/gencode-remote
+++ b/cdist/conf/type/__link/gencode-remote
@@ -47,24 +47,22 @@ case "$state_should" in
     present)
         if [ "$file_type" = "directory" ]; then
             # our destination is currently a directory, delete it
-            cat << DONE
-rm -rf "$destination"
-DONE
-        fi
-
-        if [ "$state_is" = "wrongsource" ]; then
-            printf 'rm -f "%s"\n' "$destination"
+            printf 'rm -rf "%s" &&\n' "$destination"
+        else
+           if [ "$state_is" = "wrongsource" ]; then
+               # our destination is a symlink but points to the wrong source,
+               # delete it
+               printf 'rm -f "%s" &&\n' "$destination"
+           fi
         fi
 
         # create our link
-        cat << DONE
-ln ${lnopt} -f "$source" "$destination"
-DONE
+        printf 'ln %s -f "%s" "%s"\n' "$lnopt" "$source" "$destination"
     ;;
     absent)
         # only delete if it is a sym/hard link
         if [ "$file_type" = "symlink" -o "$file_type" = "hardlink" ]; then
-            echo rm -f \"$destination\"
+            printf 'rm -f "%s"\n' "$destination"
         fi
     ;;
     *)

From 5ec617fa3e4dba53a85cad90d75590fea0abf591 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 11 Mar 2014 22:16:08 +0100
Subject: [PATCH 3/3] ignore trailing slashes for comparison of source

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 cdist/conf/type/__link/explorer/state | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/cdist/conf/type/__link/explorer/state b/cdist/conf/type/__link/explorer/state
index 48278d9e..b8d8fc2b 100755
--- a/cdist/conf/type/__link/explorer/state
+++ b/cdist/conf/type/__link/explorer/state
@@ -35,7 +35,8 @@ case "$type" in
       cd "$destination_dir"
       source_is=$(ls -l "$destination" | sed 's/.*-> //g')
       if [ -h "$destination" ]; then
-         if [ "$source_is" = "$source" ]; then
+         # ignore trailing slashes for comparison
+         if [ "${source_is%/}" = "${source%/}" ]; then
             echo present
          else
             echo wrongsource