#!/bin/sh
# Try to bring nmbd up when an interface comes up, if smbd is already running.

# Don't bother to do anything for lo.
if [ "$IFACE" = lo ]; then
	exit 0
fi

# Only run from ifup.
if [ "$MODE" != start ]; then
	exit 0
fi

# Samba only cares about inet and inet6. Get thee gone, strange people
# still using ipx.
case $ADDRFAM in
	inet|inet6|NetworkManager)
		;;
	*)
		exit 0
		;;
esac

if test -e /sys/fs/cgroup/systemd; then
	echo 'systemd detected' | mail root
	# systemd hijacked the output of '/etc/init.d/samba status'
	if systemctl status samba.service > /dev/null; then
		echo 'samba running' | mail root
		if ! systemd-cgls /system/samba.service | grep -q nmbd; then
			echo 'nmbd not running' | mail root
			systemctl restart samba.service
		fi
	fi
else
	status=$(/etc/init.d/samba status)

	# Really only necessary to do anything if nmbd is not already running
	if echo "$status" | grep -q 'smbd is running' \
	   && ! echo "$status" | grep -q 'nmbd is running'
	then
		/etc/init.d/samba start
	fi
fi

exit 0
