From 493c8d61f400653ce235cbd27d717e51656a0e9b Mon Sep 17 00:00:00 2001
From: Andres Erbsen <andreser@mit.edu>
Date: Sun, 11 Sep 2016 22:23:06 -0400
Subject: [PATCH 1/2] __user_groups: refactor, support FreeBSD

---
 .../type/__user_groups/explorer/oldusermod    |  7 ++--
 cdist/conf/type/__user_groups/gencode-remote  | 37 ++++++++++---------
 2 files changed, 23 insertions(+), 21 deletions(-)

diff --git a/cdist/conf/type/__user_groups/explorer/oldusermod b/cdist/conf/type/__user_groups/explorer/oldusermod
index bf43fcec..248d3922 100644
--- a/cdist/conf/type/__user_groups/explorer/oldusermod
+++ b/cdist/conf/type/__user_groups/explorer/oldusermod
@@ -22,7 +22,8 @@ os="$($__explorer/os)"
 
 if [ "$os" = "netbsd" ]; then
     echo netbsd
-    exit
+elif [ "$os" = "freebsd" ]; then
+    echo freebsd
+else
+    usermod --help | grep -q -- '-A group' && echo true || echo false
 fi
-
-usermod --help | grep -q -- '-A group' && echo true || echo false
diff --git a/cdist/conf/type/__user_groups/gencode-remote b/cdist/conf/type/__user_groups/gencode-remote
index 8b13f32c..6728228c 100755
--- a/cdist/conf/type/__user_groups/gencode-remote
+++ b/cdist/conf/type/__user_groups/gencode-remote
@@ -23,19 +23,6 @@ state_should="$(cat "$__object/parameter/state")"
 oldusermod="$(cat "$__object/explorer/oldusermod")"
 os=$(cat "$__global/explorer/os")
 
-if [ "$os" = "netbsd" ]; then
-	# NetBSD does not have a command to remove a user from a group
-	oldusermod="true"
-	addparam="-G"
-	delparam=";;#"
-elif [ "$oldusermod" = "true" ]; then
-        addparam="-A"
-        delparam="-R"
-else
-        addparam="-a"
-        delparam="-d"
-fi
-
 mkdir "$__object/files"
 # file has to be sorted for comparison with `comm`
 sort "$__object/parameter/group" > "$__object/files/group.sorted"
@@ -43,11 +30,9 @@ sort "$__object/parameter/group" > "$__object/files/group.sorted"
 case "$state_should" in
    present)
       changed_groups="$(comm -13 "$__object/explorer/group" "$__object/files/group.sorted")"
-      action="$addparam"
    ;;
    absent)
       changed_groups="$(comm -12 "$__object/explorer/group" "$__object/files/group.sorted")"
-      action="$delparam"
    ;;
 esac
 
@@ -57,9 +42,25 @@ if [ -z "$changed_groups" ]; then
 fi
 
 for group in $changed_groups; do
-   if [ "$oldusermod" = "true" ]; then
-     echo "usermod $action \"$group\" \"$user\""
+   if [ "$os" = "netbsd" ]; then
+      case "$state_should" in
+         present) echo "usermod -G \"$group\" \"$user\"" ;;
+         absent) echo 'NetBSD does not have a command to remove a user from a group' >&2 ; exit 1 ;;
+      esac
+   elif [ "$os" = "freebsd" ]; then
+      case "$state_should" in
+         present) echo "pw groupmod \"$group\" -m \"$user\"" ;;
+         absent) echo "pw groupmod \"$group\" -d \"$user\"" ;;
+      esac
+   elif [ "$oldusermod" = "true" ]; then
+      case "$state_should" in
+         present) echo "usermod -A \"$group\" \"$user\"" ;;
+         absent) echo "usermod -R \"$group\" \"$user\"" ;;
+      esac
    else
-     echo "gpasswd $action \"$user\" \"$group\""
+      case "$state_should" in
+         present) echo "gpasswd -a \"$group\" \"$user\"" ;;
+         absent) echo "gpasswd -d \"$group\" \"$user\"" ;;
+      esac
    fi
 done

From 86678b5beb0a292273947c250796035de0814758 Mon Sep 17 00:00:00 2001
From: Darko Poljak <darko.poljak@gmail.com>
Date: Wed, 11 Jan 2017 21:32:52 +0100
Subject: [PATCH 2/2] Cleanup __user_groups oldusermod explorer.

---
 cdist/conf/type/__user_groups/explorer/oldusermod | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/cdist/conf/type/__user_groups/explorer/oldusermod b/cdist/conf/type/__user_groups/explorer/oldusermod
index 248d3922..6ef25b13 100644
--- a/cdist/conf/type/__user_groups/explorer/oldusermod
+++ b/cdist/conf/type/__user_groups/explorer/oldusermod
@@ -18,12 +18,4 @@
 # along with cdist. If not, see <http://www.gnu.org/licenses/>.
 #
 
-os="$($__explorer/os)"
-
-if [ "$os" = "netbsd" ]; then
-    echo netbsd
-elif [ "$os" = "freebsd" ]; then
-    echo freebsd
-else
-    usermod --help | grep -q -- '-A group' && echo true || echo false
-fi
+usermod --help | grep -q -- '-A group' && echo true || echo false