#!/bin/sh

MANAGER_STOP_D=/etc/manager.d/stop.d


# Runlevel 0 - Shutdown
manager_stop () {
    ! manager_lock_exists && return 1

	# Execute target-specific scripts in /etc/manager.d/stop.d/
    if [ "`ls -A ${MANAGER_STOP_D}/* 2>/dev/null`" != "" ]; then
        for each in ${MANAGER_STOP_D}/* ; do
            sh ${each}
        done
    fi

	# TODO: add some network monitor stop like for ReadyNAS R6
	/etc/init.d/S97access-patrol stop
	/etc/init.d/S95kwilt-index stop
	/etc/init.d/S90identity stop
	[ ${ENABLE_MINIDLNA} == 1 ] && /etc/init.d/S80minidlna stop
	/etc/init.d/S60openvpn stop
    [ -x /etc/init.d/S50dropbear ] && /etc/init.d/S50dropbear stop
	/etc/init.d/S50lighttpd stop
	/etc/init.d/S50crond stop
	/etc/init.d/S03databases stop
	/etc/init.d/S02tmp stop
	/etc/init.d/S01logging stop
	/etc/init.d/S00devices stop

	if grep -q "^[^ ]* /lib/modules " /proc/mounts; then
		umount "/lib/modules"
	fi

	manager_lock_remove

	echo "OK"
	return 0
}