#!/bin/sh
### BEGIN INIT INFO
# Provides:          winbind
# Required-Start:    $network $remote_fs $syslog smbd
# Required-Stop:     $network $remote_fs $syslog smbd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 
# Description:       
### END INIT INFO



PATH=/sbin:/bin:/usr/sbin:/usr/bin

[ -r /etc/default/winbind ] && . /etc/default/winbind

DAEMON=/usr/sbin/winbindd
PIDDIR=/var/run/samba
WINBINDPID=$PIDDIR/winbindd.pid

# clear conflicting settings from the environment
unset TMPDIR

# See if the daemon is there
test -x $DAEMON || exit 0

SERVER_ROLE=`samba-tool testparm --parameter-name="server role"  2>/dev/null | tail -1`
if [ "$SERVER_ROLE" = "active directory domain controller" ]; then
    exit 0
fi

. /etc/sysconfig/rc
. ${rc_functions}

case "$1" in
	start)
		boot_mesg "Starting the Winbind daemon: winbind" ${NORMAL}

		if ! start-stop-daemon --start --quiet --oknodo --exec $DAEMON --pidfile $WINBINDPID -- $WINBINDD_OPTS
        then
            boot_mesg "Failed to start winbind" ${FAILURE}
            echo_failure
            exit 1
        fi

        echo_ok

		;;

	stop)
		boot_mesg "Stopping the Winbind daemon: winbind"
		start-stop-daemon --stop --quiet --oknodo --exec $DAEMON --pidfile $WINBINDPID
		echo_ok
		;;

	restart|force-reload)
		$0 stop && sleep 2 && $0 start
		;;

	status)
		statusproc $DAEMON
        exit $?
		;;
	*)
		echo "Usage: /etc/init.d/winbind {start|stop|restart|force-reload|status}"
		exit 1
		;;
esac
