#!/bin/sh

uci -q delete ucitrack.@openclash[-1]
uci -q add ucitrack openclash
uci -q set ucitrack.@openclash[-1].init=openclash
uci -q commit ucitrack
uci -q delete firewall.openclash
uci -q set firewall.openclash=include
uci -q set firewall.openclash.type=script
uci -q set firewall.openclash.path=/var/etc/openclash.include
uci -q set firewall.openclash.reload=1
uci -q commit firewall

mkdir -p /etc/openclash/config
mkdir -p /etc/openclash/proxy_provider
mkdir -p /etc/openclash/backup
mkdir -p /etc/openclash/core
mkdir -p /etc/openclash/history
mkdir -p /usr/share/openclash/backup

cp -f "/etc/config/openclash" "/usr/share/openclash/backup/openclash" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_rules.list" "/usr/share/openclash/backup/openclash_custom_rules.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_rules_2.list" "/usr/share/openclash/backup/openclash_custom_rules_2.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_hosts.list" "/usr/share/openclash/backup/openclash_custom_hosts.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_fake_filter.list" "/usr/share/openclash/backup/openclash_custom_fake_filter.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_domain_dns.list" "/usr/share/openclash/backup/openclash_custom_domain_dns.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_domain_dns_policy.list" "/usr/share/openclash/backup/openclash_custom_domain_dns_policy.list" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_fallback_filter.yaml" "/usr/share/openclash/backup/openclash_custom_fallback_filter.yaml" >/dev/null 2>&1
cp -f "/etc/openclash/custom/openclash_custom_netflix_domains.list" "/usr/share/openclash/backup/openclash_custom_netflix_domains.list" >/dev/null 2>&1

mkdir -p /lib/upgrade/keep.d
cat > "/lib/upgrade/keep.d/luci-app-openclash" <<-EOF
/etc/openclash/
EOF

if [ -f "/tmp/openclash.bak" ]; then
	mv -f "/tmp/openclash.bak" "/etc/config/openclash" >/dev/null 2>&1
	cp -rf "/tmp/openclash/." "/etc/openclash/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_config/." "/etc/openclash/config/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_core/." "/etc/openclash/core/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_history/." "/etc/openclash/history/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_proxy_provider/." "/etc/openclash/proxy_provider/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_rule_provider/." "/etc/openclash/rule_provider/" >/dev/null 2>&1
	cp -rf "/tmp/openclash_game_rules/." "/etc/openclash/game_rules/" >/dev/null 2>&1
	mv -f "/tmp/openclash_custom_rules.list.bak" "/etc/openclash/custom/openclash_custom_rules.list" >/dev/null 2>&1
	mv -f "/tmp/openclash_custom_rules_2.list.bak" "/etc/openclash/custom/openclash_custom_rules_2.list" >/dev/null 2>&1
	mv -f "/tmp/openclash_custom_hosts.list.bak" "/etc/openclash/custom/openclash_custom_hosts.list" >/dev/null 2>&1
	mv -f "/tmp/openclash_custom_fake_filter.list.bak" "/etc/openclash/custom/openclash_custom_fake_filter.list" >/dev/null 2>&1
	mv -f "/tmp/openclash_custom_domain_dns.list.bak" "/etc/openclash/custom/openclash_custom_domain_dns.list" >/dev/null 2>&1
	rm -rf "/etc/openclash/openclash"
	rm -rf "/etc/openclash/fake_filter.list"
	rm -rf "/etc/openclash/openclash_servers_fake_filter.conf"
	rm -rf /tmp/openclash*
fi

source "/etc/openwrt_release"
case "${DISTRIB_ARCH}" in
	aarch64_*)
		CORE_ARCH="linux-armv8"
		;;
	arm_*_neon-vfp*)
		CORE_ARCH="linux-armv7"
		;;
	arm_*_neon|arm_*_vfp*)
		CORE_ARCH="linux-armv6"
		;;
	arm*)
		CORE_ARCH="linux-armv5"
		;;
	i386_*)
		CORE_ARCH="linux-386"
		;;
	mips64_*)
		CORE_ARCH="linux-mips64"
		;;
	mips_*)
		CORE_ARCH="linux-mips-softfloat"
		;;
	mipsel_*)
		CORE_ARCH="linux-mipsle-softfloat"
		;;
	x86_64)
		CORE_ARCH="linux-amd64"
		;;
	*)
		CORE_ARCH="0"
		;;
esac
uci -q set openclash.config.core_version="${CORE_ARCH}"

if [ -n "$(uci -q get dhcp.@dnsmasq[0].resolvfile)" ]; then
   uci -q set openclash.config.default_resolvfile=$(uci -q get dhcp.@dnsmasq[0].resolvfile)
fi

uci -q set openclash.config.enable=0
uci -q commit openclash

rm -f /tmp/luci-indexcache
exit 0
