#! /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
#
### BEGIN INIT INFO
# Provides:       winbind
# Required-Start: $network $remote_fs syslog
# X-UnitedLinux-Should-Start: nmb
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    NSS daemon for resolving names from NT servers
### END INIT INFO

DAEMON_DIR="/usr/lib/samba/"
WINBINDD_BIN="winbindd"
SMB_CONF="/etc/samba/smb.conf"
SYSCONFIG_FILE="/etc/sysconfig/samba"
PID_FILE="/var/run/samba/winbindd.pid"
SAM_STATE_FILE="/var/run/samba/samwinbindd.state"

# Status shell functions
. /etc/rc.status
# Reset status of this service
rc_reset

# Check for existence of needed config files.
for configfile in ${SMB_CONF} ${SYSCONFIG_FILE}; do
	if [ ! -f ${configfile} ]; then
		echo -n "Samba configuration file ${configfile} does not exist. "
		# Tell the user this has skipped
		rc_status -s
		exit 6
	fi
done

# Check for configured winbind.
grep '^[[:space:]]*winbind uid' ${SMB_CONF} >/dev/null
rc=$?
if [ "$1" != "stop" -a ${rc:-0} -ne 0 ]; then
	echo -n "Samba WINBIND daemon not configured in ${SMB_CONF}. "
	# Tell the user this has skipped
	rc_status -s
	exit 6
fi

# Source Samba settings
. ${SYSCONFIG_FILE}

BIN_SUFFIX=$( echo ${SAMBA_SAM} | tr '[:upper:]' '[:lower:]')
# If a wrong or no BIN_SUFFIX is set, warn the user and set a default.
if [ x"${BIN_SUFFIX}" != x"classic" -a x"${BIN_SUFFIX}" != x"ldap" ]; then
	echo "Unknown Samba Security Authentication Mechanism, ${SAMBA_SAM}."
	echo "Please fix the sysconfig setting. Using default, classic for now."
	BIN_SUFFIX="classic"
fi

# Get current sam if a state file exists.
test -f ${SAM_STATE_FILE} && \
	curr_sam=$( cat ${SAM_STATE_FILE}) || \
	curr_sam=${BIN_SUFFIX}

# Check for missing binary
if [ ! -x ${DAEMON_DIR}${BIN_SUFFIX}/${WINBINDD_BIN} ]; then
	echo -n "Samba daemon, ${DAEMON_DIR}${BIN_SUFFIX}/${WINBINDD_BIN} is not installed. "
	# Tell the user this has skipped
	rc_status -s
	exit 5
fi

case "$1" in
	start)
		if [ ${BIN_SUFFIX} != ${curr_sam} ]; then
			echo -n "Samba ${curr_sam} WINBIND daemon might run; I will try to stop it first. "
			killproc -p ${PID_FILE} ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN}
			rc_status -v
		fi
		echo -n "Starting Samba ${BIN_SUFFIX} WINBIND daemon "
		checkproc -p ${PID_FILE} ${DAEMON_DIR}${BIN_SUFFIX}/${WINBINDD_BIN} && \
			echo -n " Warning: daemon already running. "
		startproc -p ${PID_FILE} ${DAEMON_DIR}${BIN_SUFFIX}/${WINBINDD_BIN} -s ${SMB_CONF}
		rc_status -v
		echo ${BIN_SUFFIX} >${SAM_STATE_FILE}
		;;
	stop)
		echo -n "Shutting down Samba ${curr_sam} WINBIND daemon "
		checkproc -p ${PID_FILE} ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN} || \
			echo -n " Warning: daemon not running. "
		killproc -p ${PID_FILE} ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN}
		rc_status -v
		rm -f ${SAM_STATE_FILE}
		;;
	try-restart)
		$0 status >/dev/null && $0 restart
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	force-reload)
		$0 reload
		rc_status
		;;
	reload)
		echo -n "Reloading Samba ${curr_sam} WINBIND daemon "
		checkproc -p ${PID_FILE} ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN} || \
			echo -n " Warning: daemon not running. "
		killproc -p ${PID_FILE} -HUP ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN}
		rc_status -v
		;;
	status)
		echo -n "Checking for Samba ${curr_sam} WINDIND daemon "
		checkproc -p ${PID_FILE} ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN}
		rc_status -v
		;;
	write-status)
		echo -n "Sending SAMBA windbind signal SIGUSR1 to write status log. "
		killproc -p ${PID_FILE} -USR1 ${DAEMON_DIR}${curr_sam}/${WINBINDD_BIN}
		rc_status -v
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|write-status}"
		exit 1
		;;
esac
rc_exit
