#! /bin/sh
# Copyright (c) 1999-2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Lars Mueller <lmuelle@suse.de>
#
# /etc/init.d/smb
#   and its symbolic link
# /usr/sbin/rcsmb
#
#	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:       smb
# Required-Start: $network $remote_fs $syslog
# Should-Start:   cupsd winbind nmb ldap
# Required-Stop:  $network $remote_fs $syslog
# Should-Stop:    cupsd winbind nmb ldap
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Short-Description: Samba SMB/CIFS file and print server
# Description:    Samba SMB/CIFS file and print server
### END INIT INFO

SMBD_BIN="/usr/sbin/smbd"
SMB_CONF="/etc/samba/smb.conf"
PID_FILE="/var/run/samba/smbd.pid"

SMB_APPARMOR_UPDATE="/usr/share/samba/update-apparmor-samba-profile"

. /etc/rc.status
rc_reset

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

# be extra carefull cause connection fail if TMPDIR is not writeable
export TMPDIR="/var/tmp"

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

for setting in $SAMBA_SMBD_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 SMB 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} ${SMBD_BIN}
		case $? in
			0) echo -n "- Warning: daemon already running. " ;;
			1) echo -n "- Warning: ${PID_FILE} exists. " ;;
		esac
		test -d "${PID_FILE%/smbd.pid}" || \
			mkdir -m 0755 -p "${PID_FILE%/smbd.pid}"
		test -f /etc/sysconfig/language && \
			. /etc/sysconfig/language
		"${SMB_APPARMOR_UPDATE}"
		export LC_ALL="$RC_LC_ALL"
		export LC_CTYPE="$RC_LC_CTYPE"
		export LANG="$RC_LANG"
		startproc -p ${PID_FILE} -W ${PID_FILE} ${SMBD_BIN} -D -s ${SMB_CONF}
		rc_status -v
		unset LC_ALL LC_CTYPE LANG
		;;
	stop)
		echo -n "Shutting down Samba SMB daemon "
		checkproc -p ${PID_FILE} ${SMBD_BIN} || \
			echo -n " Warning: daemon not running. "
		killproc -p ${PID_FILE} -t 10 ${SMBD_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 SMB daemon "
		checkproc -p ${PID_FILE} ${SMBD_BIN} && \
			touch ${PID_FILE} || \
			echo -n >&2 " Warning: daemon not running. "
		"${SMB_APPARMOR_UPDATE}"
		killproc -p ${PID_FILE} -HUP ${SMBD_BIN}
		rc_status -v
		;;
	status)
		echo -n "Checking for Samba SMB daemon "
		checkproc -p ${PID_FILE} ${SMBD_BIN}
		rc_status -v
		;;
	probe)
		test ${SMB_CONF} -nt ${PID_FILE} && echo reload
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
		exit 1
		;;
esac
rc_exit
