#!/bin/sh

# Initialize necessary directories
for d in certs provider ruleset resources templates; do
	mkdir -p "/etc/fchomo/$d/" 2>/dev/null
done

# Initialize default mixed port authentication
if ! uci -q get fchomo.global.authentication >/dev/null; then
	uci add_list fchomo.global.authentication="fchomodef:$(cat /proc/sys/kernel/random/uuid)"
	uci commit fchomo
fi

# Initialize the default direct list
if [ ! -s "/etc/fchomo/resources/direct_list.yaml" ]; then
	cat <<- EOF > "/etc/fchomo/resources/direct_list.yaml"
	DOMAIN:
	IPCIDR:
	- '223.0.0.0/12'
	IPCIDR6:
	- '2400:3200::/32'
	EOF
fi
# ruleset symlink
ln -sf /var/run/fchomo/direct_list.yaml /etc/fchomo/ruleset/rule_fchomo_direct_list
# ruleset config
if ! uci -q get fchomo.rule_fchomo_direct_list >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.rule_fchomo_direct_list=ruleset
		set fchomo.rule_fchomo_direct_list.label='Custom Direct List (Preset)'
		set fchomo.rule_fchomo_direct_list.type='file'
		set fchomo.rule_fchomo_direct_list.behavior='classical'
		set fchomo.rule_fchomo_direct_list.format='yaml'
	EOF
	uci commit fchomo
fi
# dns_policy config
if ! uci -q get fchomo.fchomo_direct_list_domain >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_direct_list_domain=dns_policy
		set fchomo.fchomo_direct_list_domain.label='Custom Direct List (Preset)'
		set fchomo.fchomo_direct_list_domain.type='rule_set'
		add_list fchomo.fchomo_direct_list_domain.server='default-dns'
		add_list fchomo.fchomo_direct_list_domain.rule_set='rule_fchomo_direct_list'
		set fchomo.fchomo_direct_list_domain.proxy='DIRECT'
	EOF
	uci commit fchomo
fi
# rules config
#if ! uci -q get fchomo.fchomo_direct_list_host >/dev/null; then
#	uci -q batch <<-EOF >/dev/null
#		set fchomo.fchomo_direct_list_host=rules
#		set fchomo.fchomo_direct_list_host.label='Custom Direct List (Debug)'
#		set fchomo.fchomo_direct_list_host.entry='{"type":"RULE-SET","payload":[{"type":"RULE-SET","factor":"rule_fchomo_direct_list"}],"detour":"DIRECT"}'
#	EOF
#	uci commit fchomo
#fi

# Initialize the default proxy list
if [ ! -s "/etc/fchomo/resources/proxy_list.yaml" ]; then
	cat <<- EOF > "/etc/fchomo/resources/proxy_list.yaml"
	DOMAIN:
	- www.google.com
	- '.googlevideo.com'
	- google.com
	IPCIDR:
	- '91.105.192.0/23'
	- '91.108.4.0/22'
	- '91.108.8.0/22'
	- '91.108.16.0/22'
	- '91.108.12.0/22'
	- '91.108.20.0/22'
	- '91.108.56.0/22'
	- '149.154.160.0/20'
	- '185.76.151.0/24'
	- '203.208.50.66/32'
	IPCIDR6:
	- '2001:67c:4e8::/48'
	- '2001:b28:f23c::/48'
	- '2001:b28:f23d::/48'
	- '2001:b28:f23f::/48'
	- '2a0a:f280::/32'
	EOF
fi
# ruleset symlink
ln -sf /var/run/fchomo/proxy_list.yaml /etc/fchomo/ruleset/rule_fchomo_proxy_list
# ruleset config
if ! uci -q get fchomo.rule_fchomo_proxy_list >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.rule_fchomo_proxy_list=ruleset
		set fchomo.rule_fchomo_proxy_list.label='Custom Proxy List (Preset)'
		set fchomo.rule_fchomo_proxy_list.type='file'
		set fchomo.rule_fchomo_proxy_list.behavior='classical'
		set fchomo.rule_fchomo_proxy_list.format='yaml'
	EOF
	uci commit fchomo
fi
# dns_policy config
if ! uci -q get fchomo.fchomo_proxy_list_domain >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_proxy_list_domain=dns_policy
		set fchomo.fchomo_proxy_list_domain.label='Custom Proxy List (Preset)'
		set fchomo.fchomo_proxy_list_domain.type='rule_set'
		add_list fchomo.fchomo_proxy_list_domain.server='default-dns'
		add_list fchomo.fchomo_proxy_list_domain.rule_set='rule_fchomo_proxy_list'
		set fchomo.fchomo_proxy_list_domain.proxy='GLOBAL'
	EOF
	uci commit fchomo
fi
# rules config
if ! uci -q get fchomo.fchomo_proxy_list_host >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_proxy_list_host=rules
		set fchomo.fchomo_proxy_list_host.label='Custom Proxy List (Preset)'
		set fchomo.fchomo_proxy_list_host.entry='{"type":"RULE-SET","payload":[{"type":"RULE-SET","factor":"rule_fchomo_proxy_list"}],"detour":"GLOBAL"}'
	EOF
	uci commit fchomo
fi

# Initialize the default china list
# ruleset symlink
ln -sf /var/run/fchomo/china_list.mrs /etc/fchomo/ruleset/rule_fchomo_china_list
# ruleset config
if ! uci -q get fchomo.rule_fchomo_china_list >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.rule_fchomo_china_list=ruleset
		set fchomo.rule_fchomo_china_list.label='China List (Preset)'
		set fchomo.rule_fchomo_china_list.type='file'
		set fchomo.rule_fchomo_china_list.behavior='domain'
		set fchomo.rule_fchomo_china_list.format='mrs'
	EOF
	uci commit fchomo
fi
# dns_policy config
if ! uci -q get fchomo.fchomo_china_list_domain >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_china_list_domain=dns_policy
		set fchomo.fchomo_china_list_domain.label='China List (Preset)'
		set fchomo.fchomo_china_list_domain.type='rule_set'
		add_list fchomo.fchomo_china_list_domain.server='default-dns'
		add_list fchomo.fchomo_china_list_domain.rule_set='rule_fchomo_china_list'
		set fchomo.fchomo_china_list_domain.proxy='DIRECT'
	EOF
	uci commit fchomo
fi
# rules config
if ! uci -q get fchomo.fchomo_china_list_host >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_china_list_host=rules
		set fchomo.fchomo_china_list_host.label='China List (Preset)'
		set fchomo.fchomo_china_list_host.entry='{"type":"RULE-SET","payload":[{"type":"RULE-SET","factor":"rule_fchomo_china_list"}],"detour":"DIRECT"}'
	EOF
	uci commit fchomo
fi

# Initialize the default gfw list
# ruleset symlink
ln -sf /var/run/fchomo/gfw_list.mrs /etc/fchomo/ruleset/rule_fchomo_gfw_list
# ruleset config
if ! uci -q get fchomo.rule_fchomo_gfw_list >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.rule_fchomo_gfw_list=ruleset
		set fchomo.rule_fchomo_gfw_list.label='GFW List (Preset)'
		set fchomo.rule_fchomo_gfw_list.type='file'
		set fchomo.rule_fchomo_gfw_list.behavior='domain'
		set fchomo.rule_fchomo_gfw_list.format='mrs'
	EOF
	uci commit fchomo
fi
# dns_policy config
if ! uci -q get fchomo.fchomo_gfw_list_domain >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_gfw_list_domain=dns_policy
		set fchomo.fchomo_gfw_list_domain.label='GFW List (Preset)'
		set fchomo.fchomo_gfw_list_domain.type='rule_set'
		add_list fchomo.fchomo_gfw_list_domain.server='default-dns'
		add_list fchomo.fchomo_gfw_list_domain.rule_set='rule_fchomo_gfw_list'
		set fchomo.fchomo_gfw_list_domain.proxy='GLOBAL'
	EOF
	uci commit fchomo
fi
# rules config
if ! uci -q get fchomo.fchomo_gfw_list_host >/dev/null; then
	uci -q batch <<-EOF >/dev/null
		set fchomo.fchomo_gfw_list_host=rules
		set fchomo.fchomo_gfw_list_host.label='GFW List (Preset)'
		set fchomo.fchomo_gfw_list_host.entry='{"type":"RULE-SET","payload":[{"type":"RULE-SET","factor":"rule_fchomo_gfw_list"}],"detour":"GLOBAL"}'
	EOF
	uci commit fchomo
fi

# Initialize the default hosts
if [ ! -s "/etc/fchomo/templates/hosts.yaml" ]; then
	cat <<- EOF > "/etc/fchomo/templates/hosts.yaml"
	hosts:
	  # '*.clash.dev': 127.0.0.1
	  # 'alpha.clash.dev': '::1'
	  # test.com: [1.1.1.1, 2.2.2.2]
	  # baidu.com: google.com
	  dns.alidns.com: [223.6.6.6, '2400:3200:baba::1', 223.5.5.5, '2400:3200::1']
	  dns.google: [8.8.8.8, '2001:4860:4860::8888', 8.8.4.4, '2001:4860:4860::8844']
	EOF
fi

# Initialize default firewall table
uci -q batch <<-EOF >"/dev/null"
	delete firewall.fchomo_pre
	set firewall.fchomo_pre=include
	set firewall.fchomo_pre.type=nftables
	set firewall.fchomo_pre.path="/var/run/fchomo/fchomo_pre.nft"
	set firewall.fchomo_pre.position="table-pre"

	delete firewall.fchomo_post
	set firewall.fchomo_post=include
	set firewall.fchomo_post.type=nftables
	set firewall.fchomo_post.path="/var/run/fchomo/fchomo_post.nft"
	set firewall.fchomo_post.position="ruleset-post"
	commit firewall
EOF

# fix dnsmasq
if ! uci -q get dhcp.@dnsmasq[0].cache_rr|grep -q '\bANY\b'; then
	uci add_list dhcp.@dnsmasq[0].cache_rr="ANY"
	uci commit dhcp
fi

exit 0
