#!/bin/sh
#
# chkconfig: 35 91 9
# description: Starts and stops the Samba smbd and nmbd daemons \
#	       used to provide SMB network services.

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 0
fi

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Mount tmpfs directories
umount /var/cache/samba 2>/dev/null
mount -t tmpfs none /var/cache/samba
umount /etc/samba 2>/dev/null
mount -t tmpfs none /etc/samba

#Get the IP that was configured automatically by the kernel
MYIP=`/sbin/ip route show proto kernel scope link|xargs -n1 echo|tail -n1`

#Make a link so both remote and local users have the same hierarchy
mkdir -p /tmp/drives
ln -s /mnt /tmp/drives/$MYIP

# Check that smb.conf exists.
if [ ! -f /etc/samba/smb.conf ];then
	cp -f /home/xterminals/etc/smb-local/* /etc/samba
fi

if [ ! -f /var/run/samba ];then
	mkdir -p /var/run/samba
fi

[ -f /etc/samba/smb.conf ] || exit 0

RETVAL=0


start() {
       # If CUPS is used as printing system, reload smb after a 1 minute delay
       # to allow the printers to appear properly as samba shares.
        if killall -0 cupsd 2>/dev/null; then
         ( sleep 60 && killproc smbd -HUP 1>/dev/null ) &
       fi
       export TMPDIR="/var/tmp"
	gprintf "Starting SMB services: "
	daemon smbd -D 	
	RETVAL=$?
	echo
	gprintf "Starting NMB services: "
	daemon nmbd -D 
	RETVAL2=$?
	echo
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && touch /var/lock/subsys/smb || \
	   RETVAL=1
	return $RETVAL
}	
stop() {
	gprintf "Shutting down SMB services: "
	killproc smbd
	RETVAL=$?
	echo
	gprintf "Shutting down NMB services: "
	killproc nmbd
	RETVAL2=$?
	[ $RETVAL -eq 0 -a $RETVAL2 -eq 0 ] && rm -f /var/lock/subsys/smb
	echo ""
	return $RETVAL
}	
restart() {
	stop
	start
}	
reload() {
       export TMPDIR="/var/tmp"
        gprintf "Reloading smb.conf file: "
	killproc smbd -HUP
	RETVAL=$?
	echo
	return $RETVAL
}	
mdkstatus() {
	status smbd
	status nmbd
}	

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	restart
	;;
  reload)
  	reload
	;;
  status)
  	mdkstatus
	;;
  condrestart)
  	[ -f /var/lock/subsys/smb ] && restart || :
	;;
  *)
	gprintf "Usage: %s {start|stop|restart|status|condrestart}\n" "$0"
	exit 1
esac

exit $?
