#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] || {
	IPSET_DIR="/opt/zapret2/ipset"

	# Create empty user files if not exist
	[ -f "${IPSET_DIR}/zapret_hosts_user.txt" ] || touch "${IPSET_DIR}/zapret_hosts_user.txt"
	[ -f "${IPSET_DIR}/zapret_hosts_auto.txt" ] || touch "${IPSET_DIR}/zapret_hosts_auto.txt"

	# Migrate: add new UCI options with defaults if missing
	UCI_CHANGED=0
	_uci_default() {
		local key="$1" val="$2"
		uci -q get "zapret2.main.${key}" >/dev/null 2>&1 || {
			uci set "zapret2.main.${key}=${val}"
			UCI_CHANGED=1
		}
	}
	_uci_default enable_ipv6 '0'
	_uci_default openwrt_lan ''
	_uci_default openwrt_wan4 ''
	_uci_default openwrt_wan6 ''
	_uci_default filter_mark ''
	_uci_default ws_user 'daemon'
	_uci_default nfqws_ports_tcp_keepalive ''
	_uci_default nfqws_ports_udp_keepalive ''
	_uci_default nfqws_udp_pkt_out '5'
	_uci_default nfqws_udp_pkt_in '3'
	if [ -z "$(uci -q get zapret2.main.ctrack_timeouts)" ]; then
		uci set zapret2.main.ctrack_timeouts='60:300:60:60'
		UCI_CHANGED=1
	fi
	_uci_default set_maxelem '522288'
	_uci_default gzip_lists '1'
	_uci_default mdig_threads '30'
	_uci_default autohostlist_debuglog '0'

	# Sync files with UCI config
	# This prevents LuCI from showing "unsaved changes" after package update

	# Sync list files (.txt in ipset dir)
	LISTS_DIR="/opt/zapret2/ipset"
	for list_file in ${LISTS_DIR}/*.txt; do
		[ -f "${list_file}" ] || continue
		list_name=$(basename "${list_file}" .txt)
		list_name=$(echo "${list_name}" | sed 's/^zapret_//')
		section_name="list_$(echo "${list_name}" | sed 's/[^a-zA-Z0-9]/_/g')"
		if ! uci -q get zapret2.${section_name} >/dev/null 2>&1; then
			uci set zapret2.${section_name}=list
			uci set zapret2.${section_name}.path="${list_file}"
			UCI_CHANGED=1
		fi
	done

	# Remove stale list sections
	for section in $(uci -q show zapret2 | grep '=list$' | cut -d. -f2 | cut -d= -f1); do
		path=$(uci -q get zapret2.${section}.path)
		[ -n "${path}" ] && [ ! -f "${path}" ] && {
			uci delete zapret2.${section}
			UCI_CHANGED=1
		}
	done

	# Sync blob files (.bin in fake dir)
	BLOBS_DIR="/opt/zapret2/files/fake"
	for blob_file in ${BLOBS_DIR}/*.bin; do
		[ -f "${blob_file}" ] || continue
		blob_name=$(basename "${blob_file}" .bin)
		section_name="blob_$(echo "${blob_name}" | sed 's/[^a-zA-Z0-9]/_/g')"
		if ! uci -q get zapret2.${section_name} >/dev/null 2>&1; then
			uci set zapret2.${section_name}=blob
			uci set zapret2.${section_name}.path="${blob_file}"
			uci set zapret2.${section_name}.enabled='0'
			UCI_CHANGED=1
		fi
	done

	# Remove stale blob sections
	for section in $(uci -q show zapret2 | grep '=blob$' | cut -d. -f2 | cut -d= -f1); do
		path=$(uci -q get zapret2.${section}.path)
		[ -n "${path}" ] && [ ! -f "${path}" ] && {
			uci delete zapret2.${section}
			UCI_CHANGED=1
		}
	done

	# Sync lua scripts (.lua in lua dir, excluding base scripts)
	LUA_DIR="/opt/zapret2/lua"
	for lua_file in ${LUA_DIR}/*.lua; do
		[ -f "${lua_file}" ] || continue
		lua_name=$(basename "${lua_file}")
		case "${lua_name}" in
			zapret-lib.lua|zapret-antidpi.lua|zapret-auto.lua) continue ;;
		esac
		section_base=$(echo "${lua_name}" | sed 's/\.lua$//; s/[^a-zA-Z0-9]/_/g')
		section_name="lua_${section_base}"
		if ! uci -q get zapret2.${section_name} >/dev/null 2>&1; then
			uci set zapret2.${section_name}=luascript
			uci set zapret2.${section_name}.path="${lua_file}"
			uci set zapret2.${section_name}.enabled='0'
			UCI_CHANGED=1
		fi
	done

	# Remove stale luascript sections
	for section in $(uci -q show zapret2 | grep '=luascript$' | cut -d. -f2 | cut -d= -f1); do
		path=$(uci -q get zapret2.${section}.path)
		[ -n "${path}" ] && [ ! -f "${path}" ] && {
			uci delete zapret2.${section}
			UCI_CHANGED=1
		}
	done

	# Migrate strategy protocol: option -> list
	for section in $(uci -q show zapret2 | grep '=strategy$' | cut -d. -f2 | cut -d= -f1); do
		proto=$(uci -q get zapret2.${section}.protocol 2>/dev/null)
		[ -z "${proto}" ] && continue
		# Skip if already a list (uci show prints list items as [0], [1]...)
		uci -q show zapret2.${section}.protocol 2>/dev/null | grep -q '\[' && continue
		uci delete zapret2.${section}.protocol
		case "${proto}" in
			tcp|udp)
				uci add_list zapret2.${section}.protocol="${proto}"
				;;
			*)
				uci add_list zapret2.${section}.protocol='tcp'
				;;
		esac
		UCI_CHANGED=1
	done

	_pos=0
	for _type in zapret2 list blob luascript strategy; do
		for _sect in $(uci -q show zapret2 | grep "=$_type\$" | cut -d. -f2 | cut -d= -f1); do
			uci reorder "zapret2.$_sect=$_pos"
			_pos=$((_pos + 1))
		done
	done

	uci commit zapret2
	/usr/bin/zapret2 scripts list >/dev/null || true
}
exit 0
