#! /bin/sh
# Copyright (c) 1999-2003 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Thomas Fehr, 1999-2001
#         Lars Mueller <lmuelle@suse.de>, 2002-2003
#
# /etc/init.d/smbfs
#   and its symbolic link
# /usr/sbin/rcsmbfs
#
### BEGIN INIT INFO
# Provides:       smbfs
# Required-Start: $network syslog
# X-UnitedLinux-Should-Start: nmb
# Required-Stop:
# Default-Start:  3 5
# Default-Stop:
# Description:    Import remote SMB/ CIFS (MS Windows) file systems
### END INIT INFO

# To access SMB/ CIFS servers beyond the network broadcast domain it may be
# necessary to also activate the nmb service. Also see section 'smbfs and nmb
# service' in /usr/share/doc/packages/samba/README.SuSE

# Status shell functions
. /etc/rc.status
# Reset status of this service
rc_reset

SMBFSTAB="/etc/samba/smbfstab"

grep -q smbfs /proc/mounts
test $? -eq 0 && smbfs_used=yes || smbfs_used=no

case "$1" in
	start)
		grep -Eq '^[^#].*?smbfs*' /etc/fstab
		rc=$?
		if [ ! -f ${SMBFSTAB} -a ${rc} -ne 0 ]; then
			echo -n "No ${SMBFSTAB} found and no type smbfs active in /etc/fstab. "
			rc_status -s
			exit 6
		fi
		echo -n "Mount SMB File System "
		service_used="no"
		if [ ${rc} -eq 0 ]; then
			timer=30
			rc=0
			printdot=""
			while [ ${rc} -eq 0 ] && [ ${timer} -gt 0 ]; do
				echo
				echo -n "from /etc/fstab "
				mount -at smbfs >/dev/null &
				jobs | grep -q Running
				rc=$?
				if [ ${rc} -ne 0 ]; then
					timer=$[${timer}-1]
					echo -n "."
					sleep 1
					printdot="yes"
				fi
				test $printdot && echo -n " "
			done
			if [ ${rc} -eq 0 -a ${timer} -eq 0 ]; then
				echo -n " Error: timeout while mount. "
				rc_failed
			fi
			rc_status -v
		fi
		timer=-1
		while read service mountpoint options; do
			case "${service}" in
				""|\#*|\;*) continue ;;
			esac
			test ${timer} -eq -1 && echo -n "from ${SMBFSTAB} "
			test ${service_used} = "no" && service_used="yes"
			echo
			echo -n "${service} on ${mountpoint} "
			rc_reset
			timer=30
			rc=0
			printdot=""
			while [ ${rc} -eq 0 ] && [ ${timer} -gt 0 ]; do
				mount -t smbfs -o ${options} ${service} ${mountpoint} &
				jobs | grep -q Running
				rc=$?
				if [ ${rc} -ne 0 ]; then
					timer=$[${timer}-1]
					echo -n "."
					sleep 1
					printdot="yes"
				fi
				test $printdot && echo -n " "
			done
			if [ ${rc} -eq 0 -a ${timer} -eq 0 ]; then
				echo -n " Error: timeout while mount. "
				rc_failed
			fi
			rc_status -v
		done < ${SMBFSTAB}
		test ${service_used} = "no" && rc_status -u
		;;
	stop)
		echo -n "Umount SMB File System "
		if [ "${smbfs_used}" = "yes" ]; then
			#
			# Unmount in background due to possible long timeouts
			#
			timer=30
			rc=0
			printdot=""
			while [ ${rc} -eq 0 ] && [ ${timer} -gt 0 ]; do
				umount -at smbfs &
				jobs | grep -q Running
				rc=$?
				if [ ${rc} -ne 0 ]; then
					timer=$[${timer}-1]
					echo -n "."
					sleep 1
					printdot="yes"
				fi
				test $printdot && echo -n " "
			done
			if [ ${rc} -eq 0 -a ${timer} -eq 0 ]; then
				echo -n " Error: timeout while umount. "
				rc_failed
			fi
			rc_status -v
		else
			rc_status -u
		fi
		;;
	try-restart)
		$0 status >/dev/null && $0 restart
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	reload)
		echo -n "Reload mounted SMB File System "
		rc_failed 3
		rc_status -v
		;;
	status)
		echo -n "Checking for mounted SMB File System "
		if [ "${smbfs_used}" = "yes" ]; then
			mount -t smbfs | while read service on mountpoint rest; do
				echo
				echo -n "${service} on ${mountpoint} "
			done
		else
			rc_failed 3
		fi
		rc_status -v
		;;
	*)
		echo "Usage: $0 {start|stop|status|reload|restart}"
		exit 1
		;;
esac
rc_exit
