#! /bin/sh
# Copyright (c) 2002,2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Lars Mueller <lmuelle@suse.de>
#
# /etc/init.d/winbind
#   and its symbolic link
# /usr/sbin/rcwinbind
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#
#	This program is distributed 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 General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
### BEGIN INIT INFO
# Provides:       winbind
# Required-Start: $network $remote_fs $syslog
# Should-Start:   nmb network-remotefs ntp
# Required-Stop:  $network $remote_fs $syslog
# Should-Stop:    nmb
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: NSS daemon for resolving names from Microsoft Windows servers
# Description:    NSS daemon for resolving names from Microsoft Windows servers
### END INIT INFO

WINBINDD_BIN="/usr/sbin/winbindd"
SMB_CONF="/etc/samba/smb.conf"
TESTPARM_BIN="/usr/bin/testparm"
PID_FILE="/var/run/samba/winbindd.pid"

. /etc/rc.status
rc_reset

# Check for missing binary
if [ ! -x ${WINBINDD_BIN} ]; then
	echo -n >&2 "Samba WINBIND daemon, ${WINBINDD_BIN} is not installed. "
	rc_status -s
	exit 5
fi

test -f /etc/sysconfig/samba && \
	. /etc/sysconfig/samba

for setting in $SAMBA_WINBINDD_ENV; do
	pathcheck="${setting%%:*}"
	variable="${setting##*:}"
	test "${pathcheck}" != "${variable}" -a ! -e "${pathcheck}" && \
		continue
	export eval ${variable}
done

case "$1" in
	start)
		echo -n "Starting Samba WINBIND daemon "
		if [ ! -f ${SMB_CONF} ]; then
			echo -n >&2 "Samba configuration file, ${SMB_CONF} does not exist. "
			rc_status -s
			exit 6
		fi
		checkproc -p ${PID_FILE} ${WINBINDD_BIN}
		case $? in
			0) echo -n "- Warning: daemon already running. " ;;
			1) echo -n "- Warning: ${PID_FILE} exists. " ;;
		esac
		test -d "${PID_FILE%/winbindd.pid}" || \
			mkdir -m 0755 -p "${PID_FILE%/winbindd.pid}"
		startproc -p ${PID_FILE} -W ${PID_FILE} ${WINBINDD_BIN} -s ${SMB_CONF}
		rc_status -v
		;;
	stop)
		echo -n "Shutting down Samba ${curr_sam} WINBIND daemon "
		checkproc -p ${PID_FILE} ${WINBINDD_BIN} || \
			echo -n " Warning: daemon not running. "
		killproc -p ${PID_FILE} -t 10 ${WINBINDD_BIN}
		rc_status -v
		;;
	try-restart|condrestart)
		if test "$1" = "condrestart"; then
			echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		fi
		$0 status
		if test $? = 0; then
			$0 restart
		else 
			rc_reset
		fi
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	force-reload|reload)
		echo -n "Reloading Samba ${curr_sam} WINBIND daemon "
		checkproc -p ${PID_FILE} ${WINBINDD_BIN} || \
			echo -n >&2 " Warning: daemon not running. "
		killproc -p ${PID_FILE} -g -HUP ${WINBINDD_BIN}
		rc_status -v
		;;
	status)
		echo -n "Checking for Samba ${curr_sam} WINBIND daemon "
		checkproc -p ${PID_FILE} ${WINBINDD_BIN}
		rc_status -v
		;;
	probe)
		test ${SMB_CONF} -nt ${PID_FILE} && echo reload
		;;
	write-status)
		echo -n "Sending SAMBA windbind signal SIGUSR2 to write status log. "
		killproc -p ${PID_FILE} -USR2 ${WINBINDD_BIN}
		rc_status -v
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe|write-status}"
		exit 1
		;;
esac
rc_exit
