#!/sbin/openrc-run
# Copyright 1999-2005 Gentoo Foundation
# Copyright 2018 Hyperbola Project
# Distributed under the terms of the GNU General Public License v2

depend() {
	use dns ldap net slapd
}

start_pre() {
	checkpath --directory --mode 0755 /var/run/nscd
}

start() {
	ebegin "Starting Name Service Cache Daemon"
	local secure=`while read curline ; do
		table=${curline%:*}
		entries=${curline##$table:}
		table=${table%%[^a-z]*}
		case $table in
			passwd*|group*|hosts)
			for entry in $entries ; do
			case $entry in
				nisplus*)
					/usr/sbin/nscd_nischeck $table || \
					/echo "-S $table,yes"
				;;
			esac
			done
		;;
		esac
		done < /etc/nsswitch.conf`
	local pidfile="$(strings /usr/sbin/nscd | grep nscd.pid)"
	mkdir -p "$(dirname ${pidfile})"
	save_options pidfile "${pidfile}"
	start-stop-daemon --start --quiet \
		--exec /usr/sbin/nscd --pidfile "${pidfile}" \
		-- $secure
	eend $?
}

stop() {
	local pidfile="$(get_options pidfile)"
	[ -n "${pidfile}" ] && pidfile="--pidfile ${pidfile}"
	ebegin "Shutting down Name Service Cache Daemon"
	start-stop-daemon --stop --quiet --exec /usr/sbin/nscd ${pidfile}
	eend $?
}

# vim:ts=4
