#!/bin/sh -e
@%@UCRWARNING=# @%@
#
# Univention Samba4
#  init script
#
# Like what you see? Join us!
# https://www.univention.com/about-us/careers/vacancies/
#
# Copyright 2017-2022 Univention GmbH
#
# https://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
# <https://www.gnu.org/licenses/>.
#
#
# samba         start/stop samba services
#
### BEGIN INIT INFO
# Provides:          samba
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: ensure Samba daemons are started (nmbd and smbd)
### END INIT INFO

## This is a convenience script, not a real init script,
## so don't load /lib/lsb/init-functions.d/40-systemd
# . /lib/lsb/init-functions

# start nmbd, smbd and samba-ad-dc unconditionally
# the init scripts themselves check if they are needed or not
case $1 in
	start)
		/etc/init.d/nmbd start
		/etc/init.d/smbd start
		/etc/init.d/samba-ad-dc start
		;;
	stop)
		/etc/init.d/samba-ad-dc stop
		/etc/init.d/smbd stop
		/etc/init.d/nmbd stop
		;;
	reload)
		echo "Reloading smbd."
		/etc/init.d/smbd reload
		;;
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
		;;
	status)
		status=0
		NMBD_DISABLED=`testparm -s --parameter-name='disable netbios' 2>/dev/null || true`
		SERVER_ROLE=`samba-tool testparm --parameter-name="server role"  2>/dev/null | tail -1 || true`
		if [ "$SERVER_ROLE" != "active directory domain controller" ]; then
			if [ "$NMBD_DISABLED" != "Yes" ]; then
				/etc/init.d/nmbd status || status=$?
			fi
			/etc/init.d/smbd status || status=$?
		else
			/etc/init.d/samba-ad-dc status || status=$?
		fi
		exit $status
		;;
	*)
		echo "Usage: /etc/init.d/samba {start|stop|reload|restart|force-reload|status}"
		exit 1
		;;
esac
