#!/bin/sh

. /lib/functions.sh

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

# Convert URL filter to parental control format
urlfilter_config="/etc/config/urlfilter"
parentalcontrol_config="/etc/config/parentalcontrol"
schedules_config="/etc/config/schedules"

# this script only needs to work if urlfilter_config was found
if [ ! -s "$urlfilter_config" ]; then
	exit 0
fi

# reset parentalcontrol_config
# but schedules might have data other than schedules
# so append to it
rm -f "$parentalcontrol_config"
touch "$parentalcontrol_config"

schedules_enable="$(uci -q get schedules.global.enable)"
# if no schedules config, then add it
if [ ! -s "$schedules_config" ]; then
	touch "$schedules_config"
	schedules_enable=1
fi

# Parse globals
uci -q batch <<EOF
set parentalcontrol.globals=globals
set parentalcontrol.globals.enable="$(uci -q get urlfilter.globals.enable)"
set schedules.global=global
set schedules.global.enable="$schedules_enable"
EOF

# Function to handle filter sections
handle_filter() {
	local section="$1"
	local url_list="$2"
	local profile_name="$3"
	local access="$4"
	local profile_name enable start_time duration days filter_profile macaddr_list

	config_get filter_profile "$section" profile

	# if option profile value and profile name match, then
	if [ "$filter_profile" = "$profile_name" ]; then
		config_get enable "$section" enable
		config_get start_time "$section" start_time
		config_get duration "$section" duration
		config_get macaddr_list "$section" macaddr
		config_get days "$section" day

		# Add hosts based on MAC addresses in the filter
		if [ -n "$macaddr_list" ]; then
			for macaddr in $macaddr_list; do
				uci -q add_list parentalcontrol.${profile_name}.host="$macaddr"
			done
		fi

		uci -q set parentalcontrol.f_$filter_profile=profile_urlfilter
		uci -q set parentalcontrol.f_$filter_profile.enable="$enable"
		uci -q set parentalcontrol.f_$filter_profile.access="$access"
		uci -q set parentalcontrol.f_$filter_profile.dm_parent="$profile_name"

		# Add URLs one by one as filter_text
		for url in $url_list; do
			uci -q add_list parentalcontrol.f_$filter_profile.filter_text="$url"
		done

		# Add schedule if time restrictions exist
		if [ -n "$start_time" ] && [ -n "$duration" ] && [ -n "$days" ]; then
			local schedule_name
			# declare and assign separately to avoid masking return value
			schedule_name="$(uci -q add schedules schedule)"

			# if adding schedule was successful, then populate it
			if [ "$?" -eq 0 ] && [ -n "$schedule_name" ]; then
				uci -q set schedules.${schedule_name}=schedule
				uci -q set schedules.${schedule_name}.enable="$enable"
				uci -q set schedules.${schedule_name}.start="$start_time"
				uci -q set schedules.${schedule_name}.duration="$duration"

				for day in $days; do
					uci -q add_list schedules.${schedule_name}.day="$day"
				done

				# Link schedule to profile_urlfilter
				uci -q set parentalcontrol.f_$filter_profile.profile_urlfilter_schedule="$schedule_name"
			fi
		fi
	fi
}

# Function to handle profile sections
handle_profile() {
	local section="$1"
	local profile_name whitelist_urls blacklist_urls

	config_get profile_name "$section" name

	# if name was not set then continue
	if [ -z "$profile_name" ]; then
		return
	fi

	config_get whitelist_urls "$section" whitelist_url
	config_get blacklist_urls "$section" blacklist_url

	# Create the new profile in parentalcontrol
	uci -q set parentalcontrol.${profile_name}=profile
	uci -q set parentalcontrol.${profile_name}.name="$profile_name"

	# Add whitelist/blacklist URLs as filter_text
	if [ -n "$whitelist_urls" ]; then
		config_foreach handle_filter filter "$whitelist_urls" "$profile_name" 1  # Whitelist access
	fi
	if [ -n "$blacklist_urls" ]; then
		config_foreach handle_filter filter "$blacklist_urls" "$profile_name" 0  # Blacklist access
	fi
}

# Load urlfilter UCI config and iterate through profiles and filters
config_load "urlfilter"
config_foreach handle_profile profile
config_foreach handle_filter filter

rm -f "$urlfilter_config"
