From 37612ff102f3b87dcce38af667bfb0c8d1a80a51 Mon Sep 17 00:00:00 2001
From: Steven Armstrong <steven@icarus.ethz.ch>
Date: Thu, 21 Jul 2011 23:06:13 +0200
Subject: [PATCH] add --state parameter to __directory type which allows
 removal of directories

Signed-off-by: Steven Armstrong <steven@icarus.ethz.ch>
---
 conf/type/__directory/gencode-remote     | 76 ++++++++++++++----------
 conf/type/__directory/man.text           | 10 +++-
 conf/type/__directory/manifest           | 25 ++++++++
 conf/type/__directory/parameter/optional |  1 +
 4 files changed, 80 insertions(+), 32 deletions(-)
 create mode 100755 conf/type/__directory/manifest

diff --git a/conf/type/__directory/gencode-remote b/conf/type/__directory/gencode-remote
index a5458b4b..7fd589be 100755
--- a/conf/type/__directory/gencode-remote
+++ b/conf/type/__directory/gencode-remote
@@ -25,39 +25,55 @@
 #
 
 destination="/$__object_id"
+state_should="$(cat "$__object/parameter/state")"
 
-# Include parent directories?
-if [ -f "$__object/parameter/parents" ]; then
-   parents="$(cat "$__object/parameter/parents")"
-   if [ yes = "$parents" ]; then
-      mkdiropt="-p"
-   else
-      mkdiropt=""
-   fi
-fi
+case "$state_should" in
+   present)
+      # Include parent directories?
+      if [ -f "$__object/parameter/parents" ]; then
+         parents="$(cat "$__object/parameter/parents")"
+         if [ yes = "$parents" ]; then
+            mkdiropt="-p"
+         else
+            mkdiropt=""
+         fi
+      fi
 
-if [ -f "$__object/parameter/recursive" ]; then
-   if [ yes = "$(cat "$__object/parameter/recursive")" ]; then
-      recursive="-R"
-   fi
-fi
+      if [ -f "$__object/parameter/recursive" ]; then
+         if [ yes = "$(cat "$__object/parameter/recursive")" ]; then
+            recursive="-R"
+         fi
+      fi
 
-# Only create if not already existent
-if [ no = "$(cat "$__object/explorer/exists")" ]; then
-   echo mkdir $mkdiropt \"$destination\"
-fi
+      # Only create if not already existent
+      if [ no = "$(cat "$__object/explorer/exists")" ]; then
+         echo mkdir $mkdiropt \"$destination\"
+      fi
 
-# Mode settings
-if [ -f "$__object/parameter/mode" ]; then
-   echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\"
-fi
+      # Mode settings
+      if [ -f "$__object/parameter/mode" ]; then
+         echo chmod \"$(cat "$__object/parameter/mode")\" \"$destination\"
+      fi
 
-# Group
-if [ -f "$__object/parameter/group" ]; then
-   echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\"
-fi
+      # Group
+      if [ -f "$__object/parameter/group" ]; then
+         echo chgrp $recursive \"$(cat "$__object/parameter/group")\" \"$destination\"
+      fi
 
-# Owner
-if [ -f "$__object/parameter/owner" ]; then
-   echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\"
-fi
+      # Owner
+      if [ -f "$__object/parameter/owner" ]; then
+         echo chown $recursive \"$(cat "$__object/parameter/owner")\" \"$destination\"
+      fi
+   ;;
+   absent)
+      # Only delete if it exists
+      if [ yes = "$(cat "$__object/explorer/exists")" ]; then
+         echo rm -r \"$destination\"
+      fi
+
+   ;; 
+   *) 
+      echo "Unknown state" >&2
+      exit 1
+   ;;
+esac
diff --git a/conf/type/__directory/man.text b/conf/type/__directory/man.text
index e2201172..315d1fb9 100644
--- a/conf/type/__directory/man.text
+++ b/conf/type/__directory/man.text
@@ -5,12 +5,12 @@ Nico Schottelius <nico-cdist--@--schottelius.org>
 
 NAME
 ----
-cdist-type__directory - Create a directory
+cdist-type__directory - Manage a directory
 
 
 DESCRIPTION
 -----------
-This cdist type allows you to create directories on the target.
+This cdist type allows you to create or remove directories on the target.
 
 
 REQUIRED PARAMETERS
@@ -20,6 +20,9 @@ None.
 
 OPTIONAL PARAMETERS
 -------------------
+state::
+   'present' or 'absent', defaults to 'present'
+
 group::
    Group to chgrp to.
 
@@ -45,6 +48,9 @@ EXAMPLES
 # A silly example
 __directory /tmp/foobar
 
+# Remove a directory
+__directory /tmp/foobar --ensure absent
+
 # Ensure /etc exists correctly
 __directory /etc --owner root --group root --mode 0755
 
diff --git a/conf/type/__directory/manifest b/conf/type/__directory/manifest
new file mode 100755
index 00000000..2fcc085d
--- /dev/null
+++ b/conf/type/__directory/manifest
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# 2011 Steven Armstrong (steven-cdist at 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/>.
+#
+
+if [ -f "$__object/parameter/state" ]; then
+   state="$(cat "$__object/parameter/state")"
+else
+   echo "present" > "$__object/parameter/state"
+fi
diff --git a/conf/type/__directory/parameter/optional b/conf/type/__directory/parameter/optional
index 247e2f64..27f9d76a 100644
--- a/conf/type/__directory/parameter/optional
+++ b/conf/type/__directory/parameter/optional
@@ -1,3 +1,4 @@
+state
 group
 mode
 owner