From 6d5686229fd6da683ef692301ab356251992acff Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Wed, 18 Sep 2013 10:40:29 +0200
Subject: [PATCH 1/3] only delete links; delete existing destination before
 creating links

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 cdist/conf/type/__link/explorer/type          | 26 ++++++++++++++++
 cdist/conf/type/__link/gencode-remote         | 30 ++++++++++++++++---
 .../conf/type/__link/parameter/default/state  |  1 +
 3 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100755 cdist/conf/type/__link/explorer/type
 create mode 100644 cdist/conf/type/__link/parameter/default/state

diff --git a/cdist/conf/type/__link/explorer/type b/cdist/conf/type/__link/explorer/type
new file mode 100755
index 00000000..c02b3af8
--- /dev/null
+++ b/cdist/conf/type/__link/explorer/type
@@ -0,0 +1,26 @@
+#!/bin/sh
+# 2013 Steven Armstrong (steven-cdist armstrong.cc)
+
+destination="/$__object_id"
+
+if [ ! -e "$destination" ]; then
+   echo none
+elif [ -h "$destination" ]; then
+   echo symlink
+elif [ -f "$destination" ]; then
+   type="$(cat "$__object/parameter/type")"
+   case "$type" in
+      hard)
+         link_count=$(ls -l "$destination" | awk '{ print $2 }')
+         if [ $link_count -gt 1 ]; then
+            echo hardlink
+            exit 0
+         fi
+      ;;
+   esac
+   echo file
+elif [ -d "$destination" ]; then
+   echo directory
+else
+   echo unknown
+fi
diff --git a/cdist/conf/type/__link/gencode-remote b/cdist/conf/type/__link/gencode-remote
index 2975ef69..2e41b7d9 100755
--- a/cdist/conf/type/__link/gencode-remote
+++ b/cdist/conf/type/__link/gencode-remote
@@ -1,6 +1,7 @@
 #!/bin/sh
 #
 # 2011-2012 Nico Schottelius (nico-cdist at schottelius.org)
+# 2013 Steven Armstrong (steven-cdist at armstrong.cc)
 #
 # This file is part of cdist.
 #
@@ -40,17 +41,38 @@ case "$type" in
 esac
 
 state_is="$(cat "$__object/explorer/state")"
-state_should=present
-[ -f "$__object/parameter/state" ] && state_should="$(cat "$__object/parameter/state")"
+state_should="$(cat "$__object/parameter/state")"
 
 [ "$state_should" = "$state_is" ] && exit 0
 
+file_type="$(cat "$__object/explorer/type")"
 case "$state_should" in
     present)
-        echo ln ${lnopt} -f \"$source\" \"$destination\"
+        if [ "$file_type" = "directory" ]; then
+            # our destination is currently a directory, move it out of the way
+            cat << DONE
+destination_old="\$(mktemp "${destination}.cdist.XXXXXXXXXX")"
+mv "$destination" "\$destination_old"
+DONE
+        fi
+
+        # create our link
+        cat << DONE
+ln ${lnopt} -f "$source" "$destination"
+DONE
+
+        if [ "$file_type" = "directory" ]; then
+            # delete the legacy directory
+            cat << DONE
+rm -rf "\$destination_old"
+DONE
+        fi
     ;;
     absent)
-        echo rm -f \"$destination\"
+        # only delete if it is a sym/hard link
+        if [ "$file_type" = "symlink" -o "$file_type" = "hardlink" ]; then
+            echo rm -f \"$destination\"
+        fi
     ;;
     *)
         echo "Unknown state: $state_should" >&2
diff --git a/cdist/conf/type/__link/parameter/default/state b/cdist/conf/type/__link/parameter/default/state
new file mode 100644
index 00000000..e7f6134f
--- /dev/null
+++ b/cdist/conf/type/__link/parameter/default/state
@@ -0,0 +1 @@
+present

From 71b41df73324fa2f423b21c7d81b9baa4d6b131b Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Tue, 15 Oct 2013 14:55:35 +0200
Subject: [PATCH 2/3] no late delete

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

diff --git a/cdist/conf/type/__link/gencode-remote b/cdist/conf/type/__link/gencode-remote
index 2e41b7d9..cbdfd30f 100755
--- a/cdist/conf/type/__link/gencode-remote
+++ b/cdist/conf/type/__link/gencode-remote
@@ -49,10 +49,9 @@ file_type="$(cat "$__object/explorer/type")"
 case "$state_should" in
     present)
         if [ "$file_type" = "directory" ]; then
-            # our destination is currently a directory, move it out of the way
+            # our destination is currently a directory, delete it
             cat << DONE
-destination_old="\$(mktemp "${destination}.cdist.XXXXXXXXXX")"
-mv "$destination" "\$destination_old"
+rm -rf "$destination"
 DONE
         fi
 
@@ -60,13 +59,6 @@ DONE
         cat << DONE
 ln ${lnopt} -f "$source" "$destination"
 DONE
-
-        if [ "$file_type" = "directory" ]; then
-            # delete the legacy directory
-            cat << DONE
-rm -rf "\$destination_old"
-DONE
-        fi
     ;;
     absent)
         # only delete if it is a sym/hard link

From abf291cb20106dcfb7c50587e2cb3408a296cf9a Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Wed, 27 Nov 2013 16:08:12 +0100
Subject: [PATCH 3/3] add gpl header

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

diff --git a/cdist/conf/type/__link/explorer/type b/cdist/conf/type/__link/explorer/type
index c02b3af8..579fd081 100755
--- a/cdist/conf/type/__link/explorer/type
+++ b/cdist/conf/type/__link/explorer/type
@@ -1,5 +1,25 @@
 #!/bin/sh
+#
 # 2013 Steven Armstrong (steven-cdist armstrong.cc)
+#
+# This file is part of cdist.
+#
+# cdist is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# cdist is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# 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"