#!/bin/sh
#
# $FreeBSD: ports/net/samba3/files/samba.sh.sample,v 1.10 2005/04/17 16:10:50 pav Exp $
#

# PROVIDE: nmbd smbd
# PROVIDE: winbindd
# REQUIRE: NETWORKING SERVERS named cups
# BEFORE: DAEMON
# KEYWORD: shutdown

#
# Add the following lines to /etc/rc.conf to enable samba:
#
#samba_enable="YES"
#
# or, for fine grain control
#
#nmbd_enable="YES"
#smbd_enable="YES"
# You need to enable winbindd separately, by adding:
#winbindd_enable="YES"
#

. /etc/rc.subr

name=samba
rcvar=`set_rcvar`

load_rc_config $name
# Set defaults
samba_config=${samba_config:-"/usr/local/etc/smb.conf"}
# Config file is required
if [ ! -r ${samba_config} ]; then
    warn "${samba_config} is not readable."
    case $1 in
	force*) : ;;
	*) exit 1 ;;
    esac
fi

if test -n ${samba_enable:-""} && checkyesno samba_enable; then
    nmbd_enable=${nmbd_enable:-"YES"}
    smbd_enable=${smbd_enable:-"YES"}
    # Check, that winbind is actally configured
    if [ "`egrep -i '(idmap.*uid|winbind.*uid)' ${samba_config} 2>/dev/null | egrep -v [\#\;]`" ]; then
	winbindd_enable="YES"
    fi
fi

# Hack until run_rc_command() get rid of exit()
samba_stop() {
    pid=$(check_pidfile ${pidfile} ${command})
    if [ -z ${pid} ]; then
	echo "${name} not running? (check ${pidfile})."
	return 1
    fi
    echo "Stopping ${command}."
    kill -${sig_stop:-TERM} ${pid}
    [ $? -ne 0 ] && [ -z "$rc_force" ] && return 1
    wait_for_pids ${pid}
}

nmbd_precmd() {
    # XXX: Never delete winbindd_idmap, winbindd_cache and group_mapping
    if [ -d "/var/db/samba" ]; then
	echo "Starting SAMBA: removing stale tdbs :"
	for file in connections.tdb locking.tdb messages.tdb \
		    sessionid.tdb unexpected.tdb brlock.tdb \
		    namelist.debug
	do
	    rm -vf "/var/db/samba/$file"
	done
    fi
}

# nmbd
name=nmbd
rcvar=`set_rcvar`
command="/usr/local/sbin/${name}"
required_dirs="/var/db/samba"
pidfile=/var/run/${name}.pid
start_precmd="nmbd_precmd"
stop_cmd="samba_stop"
# Defaults
nmbd_enable=${nmbd_enable:-"NO"}
nmbd_flags=${nmbd_flags:-"-D"}
command_args="-s ${samba_config}"

load_rc_config $name
run_rc_command "$1"

# smbd
name=smbd
rcvar=`set_rcvar`
command="/usr/local/sbin/${name}"
pidfile=/var/run/${name}.pid
start_precmd=":"
stop_cmd="samba_stop"
# Defaults
smbd_enable=${smbd_enable:-"NO"}
smbd_flags=${smbd_flags:-"-D"}
command_args="-s ${samba_config}"

load_rc_config $name
run_rc_command "$1"

# winbindd
name=winbindd
rcvar=`set_rcvar`
command="/usr/local/sbin/${name}"
required_dirs="/var/db/samba"
pidfile=/var/run/${name}.pid
start_precmd=":"
stop_cmd="samba_stop"
# Defaults
winbindd_enable=${winbindd_enable:-"NO"}
winbindd_flags=${winbindd_flags:-""}
command_args="-s ${samba_config}"

load_rc_config $name
run_rc_command "$1"
