#! /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 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

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

function checkWinbindConfig
{
	test -d /dev/shm && \
		TMP_PATH="/dev/shm" || \
		TMP_PATH="/var/tmp"
	TMP_FILE=$( mktemp -p ${TMP_PATH} testparm.out.XXXXX)

	${TESTPARM_BIN} -s ${SMB_CONF} 2>/dev/null | \
		grep -i "\(idmap uid\|idmap gid\)" | \
		sed -ne "s/^[[:space:]]*\(.*\)[[:space:]]=[[:space:]]*\([0-9]*\)-\([0-9]*\)/\1/Ip" \
		>${TMP_FILE}

	idmapUidConf="No"
	idmapGidConf="No"
	while read param; do
		case "${param}" in
			'idmap uid')
				idmapUidConf="Yes"
				;;
			'idmap gid')
				idmapGidConf="Yes"
				;;
		esac
	done <${TMP_FILE}
	rm -f ${TMP_FILE}

	if [ "${idmapUidConf}" = "No" -o "${idmapGidConf}" = "No" ]; then
		if [ "${idmapUidConf}" = "No" -a "${idmapGidConf}" = "No" ]; then
			message="You have to set parameters for 'idmap uid' and 'idmap gid'."
		elif [ "${idmapUidConf}" = "No" ]; then
			message="You have to set parameters for 'idmap uid'."
		else
			message="You have to set parameters for 'idmap gid'."
		fi
		echo >&2 "Samba WINBIND daemon not configured in ${SMB_CONF}. "
		echo -n >&2 "${message} "
		rc_status -s
		exit 6
	fi
}

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
		checkWinbindConfig
		checkproc -p ${PID_FILE} ${WINBINDD_BIN}
		case $? in
			0) echo -n "- Warning: daemon already running. " ;;
			1) echo -n "- Warning: ${PID_FILE} exists. " ;;
		esac
		startproc -p ${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} ${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} -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
