From ae6ae1f507ea76e6f9fa4e1ae1dba4aa14493aaa Mon Sep 17 00:00:00 2001
From: Antoine Jacoutot <ajacoutot@cvs.openbsd.org>
Date: Wed, 20 Aug 2014 13:23:57 +0000
Subject: Drop default values from rc.conf.local when we run "enable".

---
 usr.sbin/rcctl/rcctl.sh | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

(limited to 'usr.sbin/rcctl')

diff --git a/usr.sbin/rcctl/rcctl.sh b/usr.sbin/rcctl/rcctl.sh
index 41d325f75e0..9dca58ab92d 100755
--- a/usr.sbin/rcctl/rcctl.sh
+++ b/usr.sbin/rcctl/rcctl.sh
@@ -61,11 +61,31 @@ svc_default_enabled()
 	svc_is_enabled ${_svc} && _ret=0
 
 	# reparse _all_ values
-	svc_is_base ${_svc} && _rc_parse_conf
+	_rc_parse_conf
 
 	return ${_ret}
 }
 
+svc_default_flags()
+{
+	local daemon_flags
+	local _svc=$1
+	[ -n "${_svc}" ] || return
+
+	# get _defaults_ values only
+	_rc_parse_conf /etc/rc.conf
+
+	if svc_is_base ${_svc} || svc_is_special ${_svc}; then
+		svc_get_flags ${_svc}
+	else
+		eval $(grep '^daemon_flags=' /etc/rc.d/${_svc})
+		echo ${daemon_flags}
+	fi
+
+	# reparse _all_ values
+	_rc_parse_conf
+}
+
 svc_get_all()
 {
 	local _i
@@ -224,8 +244,10 @@ add_flags()
 			set -A _flags -- ${_flags}
 		fi
 	elif svc_is_enabled ${_svc}; then
-		# svc is already enabled and we did not (re)set the flags
-		return
+		# we did not (re)set the flags: stop here if svc is already
+		# enabled and our current flags do not match the default ones;
+		# if they do, we drop the default "svc_flags=" further down
+		[[ "$(svc_default_flags ${_svc})" != "$(svc_get_flags ${_svc})" ]] && return
 	fi
 
 	# special var
@@ -254,7 +276,8 @@ add_flags()
 	rcconf_edit_begin
 	grep -v "^${_svc}_flags.*=" /etc/rc.conf.local >${_TMP_RCCONF}
 	if [ "${#_flags[*]}" -gt 0 ]; then
-		echo ${_svc}_flags=${_flags[@]} >>${_TMP_RCCONF}
+		[[ "${_flags[@]}" != "$(svc_default_flags ${_svc})" ]] && \
+			echo ${_svc}_flags=${_flags[@]} >>${_TMP_RCCONF}
 	fi
 	rcconf_edit_end
 }
-- 
cgit v1.2.3