#!/bin/sh

[ ! -f "/etc/config/parentalcontrol" ] && exit 0

APPS_DIR="/apps"

check_mounted_app_partition() {
	local free

	if [ ! -d "${APPS_DIR}" ]; then
		return 1
	fi

	# Check free space in disk
	free="$(df -P "${APPS_DIR}"|tail -n 1|awk '{print $4}')"

	# disable if free storage is less then 300M
	if [ "${free}" -lt 307200 ]; then
		return 1
	fi

	return 0
}


if check_mounted_app_partition; then
	uci -q set parentalcontrol.globals.bundle_path="${APPS_DIR}/parentalcontrol"

	# configure the urlfilter if not configured
	urlfilter="$(uci -q get parentalcontrol.globals.urlfilter)"
	if [ -z "${urlfilter}" ]; then
		uci -q set parentalcontrol.globals.urlfilter='1'
	fi
else
	uci -q set parentalcontrol.globals.urlfilter='0'
fi

exit 0
