#!/bin/sh
@%@UCRWARNING=# @%@
#
# Copyright 2001-2014 Univention GmbH
#
# http://www.univention.de/
#
# All rights reserved.
#
# The source code of this program is made available
# under the terms of the GNU Affero General Public License version 3
# (GNU AGPL V3) as published by the Free Software Foundation.
#
# Binary versions of this program provided by Univention to you as
# well as other copyrighted, protected or trademarked materials like
# Logos, graphics, fonts, specific documentations and configurations,
# cryptographic keys etc. are subject to a license agreement between
# you and Univention and not subject to the GNU AGPL V3.
#
# In the case you use this program under the terms of the GNU AGPL V3,
# the program is provided 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License with the Debian GNU/Linux or Univention distribution in file
# /usr/share/common-licenses/AGPL-3; if not, see
# <http://www.gnu.org/licenses/>.
#

eval "$(univention-config-registry shell dns/backend)"

. /lib/lsb/init-functions

sv_action () { # Silently toggle runit state: $action $service_name
	if [ -e "/etc/runit/univention/$2" ]; then
		 sv "$1" "$2" >>/var/log/daemon.log
	fi
}

sv_running () { # Check wanted state: $service name
	local status
	status="$(LC_ALL=C sv status "$1")"
	case "$status" in
	run:*) return 0 ;; # requested running
	down:) return 1 ;; # requested shutdown
	finish:*normally\ down,\ want\ down) return 1 ;; # shutting down
	finish:*normally\ down) return 0 ;; # down due to error
	*) logger -p daemon.notice "Unknown bind9 status: $status" ; return 1 ;;
	esac
}

case "$1" in
	start)
		# check ucr autostart setting
		if [ -f "/usr/share/univention-config-registry/init-autostart.lib" ]; then
			. "/usr/share/univention-config-registry/init-autostart.lib"
			check_autostart univention-bind bind/autostart
		fi
		log_action_msg "Starting bind9 daemon:"
		if [ "$dns_backend" = "samba4" ]; then
			sv_action up univention-bind-samba4
		elif [ "$dns_backend" = "ldap" ]; then
			sv_action up univention-bind
			sv_action up univention-bind-proxy
		fi
		log_action_end_msg 0
		;;
	stop)
		log_action_msg "Stopping bind9 daemon: "
		sv_action down univention-bind-proxy
		sv_action down univention-bind
		sv_action down univention-bind-samba4
		log_action_end_msg 0
		;;
	restart|force-reload)
		# check ucr autostart setting
		if [ -f "/usr/share/univention-config-registry/init-autostart.lib" ]; then
			. "/usr/share/univention-config-registry/init-autostart.lib"
			check_autostart univention-bind bind/autostart
		fi

		log_action_msg "Restarting bind9 daemon: "
		if [ "$dns_backend" = "samba4" ]; then
			sv_action stop univention-bind
			sv_action stop univention-bind-proxy
			sv_action restart univention-bind-samba4
		elif [ "$dns_backend" = "ldap" ]; then
			sv_action stop univention-bind-samba4
			sv_action restart univention-bind
			sv_action restart univention-bind-proxy
		fi
		log_action_end_msg 0
		;;
	crestart)
		# check ucr autostart setting
		if [ -f "/usr/share/univention-config-registry/init-autostart.lib" ]; then
			. "/usr/share/univention-config-registry/init-autostart.lib"
			check_autostart univention-bind bind/autostart
		fi
		if [ "$dns_backend" = "samba4" ]; then
			if sv_running univention-bind-samba4
			then
				"$0" restart
			fi
		elif [ "$dns_backend" = "ldap" ]; then
			if sv_running univention-bind
			then
				"$0" restart
			fi
			if sv_running univention-bind-proxy
			then
				"$0" restart
			fi
		fi
		;;
	status)
		if	sv status univention-bind-samba4 | grep -qs ^run: ||
			sv status univention-bind | grep -qs ^run: ||
			sv status univention-bind-proxy | grep -qs ^run:
		then
			log_success_msg "bind9 is running"
			exit 0
		else
 			log_success_msg "bind9 is not running"
			exit 3
		fi
		;;
	*)
		log_action_msg "Usage: $0 {start|stop|restart|crestart|force-reload|status}"
		exit 1
		;;
esac
