#!/bin/sh
[ ! -f "/usr/share/ucitrack/luci-app-shadowsocksr.json" ] && {
    cat > /usr/share/ucitrack/luci-app-shadowsocksr.json << EEOF
{
    "config": "shadowsocksr",
    "init": "shadowsocksr"
}
EEOF
}

if [ -e "/etc/config/ucitrack" ]; then
    uci -q batch <<-EOF
        delete ucitrack.@shadowsocksr[-1]
        add ucitrack shadowsocksr
        set ucitrack.@shadowsocksr[-1].init=shadowsocksr
        commit ucitrack
EOF
fi

uci -q batch <<-EOF
	delete firewall.shadowsocksr
	set firewall.shadowsocksr=include
	set firewall.shadowsocksr.type=script
	set firewall.shadowsocksr.path=/var/etc/shadowsocksr.include
	$(command -v fw4 >/dev/null 2>&1 || echo "set firewall.shadowsocksr.reload=1")
	commit firewall
EOF

rm -rf /etc/config/shadowsocksr-opkg /etc/ssrplus/*opkg
touch /etc/ssrplus/china_ssr.txt
touch /etc/ssrplus/deny.list
touch /etc/ssrplus/white.list
touch /etc/ssrplus/black.list
touch /etc/ssrplus/gfw_base.conf
touch /etc/ssrplus/gfw_list.conf
touch /etc/ssrplus/ad.conf
touch /etc/config/shadowsocksr

if [ -s "/etc/config/shadowsocksr" ]; then
    if uci -q get shadowsocksr.@server_subscribe[0].auto_update_time > /dev/null; then
        uci -q delete shadowsocksr.@server_subscribe[0].auto_update_time
    fi

    if ! uci -q get shadowsocksr.@server_subscribe[0].auto_update_week_time > /dev/null; then
        uci -q set shadowsocksr.@server_subscribe[0].auto_update_week_time='*'
        uci -q set shadowsocksr.@server_subscribe[0].auto_update_day_time='2'
        uci -q set shadowsocksr.@server_subscribe[0].auto_update_min_time='0'
    fi

    if ! uci -q get shadowsocksr.@server_subscribe[0].config_auto_update_mode > /dev/null; then
        uci -q set shadowsocksr.@server_subscribe[0].config_auto_update_mode='0'
    fi

    if ! uci -q get shadowsocksr.@server_subscribe[0].config_update_interval > /dev/null; then
        uci -q set shadowsocksr.@server_subscribe[0].config_update_interval='60'
    fi

    if ! uci -q get shadowsocksr.@server_subscribe[0].user_agent > /dev/null; then
        uci -q set shadowsocksr.@server_subscribe[0].user_agent='v2rayN/9.99'
    fi

    if ! uci -q get shadowsocksr.@server_subscribe[0].proxy > /dev/null; then
        uci -q set shadowsocksr.@server_subscribe[0].proxy='1'
    fi

    if ! uci -q get shadowsocksr.@global_xray_fragment[0] > /dev/null; then
        uci -q add shadowsocksr global_xray_fragment
        uci -q set shadowsocksr.@global_xray_fragment[0].fragment='0'
        uci -q set shadowsocksr.@global_xray_fragment[0].noise='0'
    fi

    if ! uci -q get shadowsocksr.@global[0].component_mirror > /dev/null; then
        uci -q set shadowsocksr.@global[0].component_mirror='direct'
    fi

    if ! uci -q get shadowsocksr.@global[0].filter_aaaa > /dev/null; then
        legacy_filter_aaaa="$(uci -q get shadowsocksr.@global[0].mosdns_ipv6)"
        uci -q set shadowsocksr.@global[0].filter_aaaa="${legacy_filter_aaaa:-1}"
    fi

    legacy_dns_mode="$(uci -q get shadowsocksr.@global[0].pdnsd_enable)"
    if [ "$legacy_dns_mode" = "2" ] || [ "$legacy_dns_mode" = "3" ]; then
        uci -q set shadowsocksr.@global[0].pdnsd_enable='1'
    fi

    if uci -q get shadowsocksr.@global[0].mosdns_ipv6 > /dev/null; then
        uci -q delete shadowsocksr.@global[0].mosdns_ipv6
    fi

	if ! uci -q get shadowsocksr.@http_proxy[0] > /dev/null; then
	    uci -q add shadowsocksr http_proxy
	    uci -q set shadowsocksr.@http_proxy[0].server='nil'
	    uci -q set shadowsocksr.@http_proxy[0].local_port='3128'
	    uci -q set shadowsocksr.@http_proxy[0].http_auth='none'
	fi

	ss_rust_bin="$(command -v ssserver 2>/dev/null)"
	[ -n "$ss_rust_bin" ] || [ ! -x /usr/libexec/ssserver ] || ss_rust_bin="/usr/libexec/ssserver"
	mihomo_bin="$(command -v mihomo 2>/dev/null)"
	[ -n "$mihomo_bin" ] || [ ! -x /usr/libexec/mihomo ] || mihomo_bin="/usr/libexec/mihomo"
	xray_bin="$(command -v xray 2>/dev/null)"
	[ -n "$xray_bin" ] || [ ! -x /usr/libexec/xray ] || xray_bin="/usr/libexec/xray"
	for section in $(uci -q show shadowsocksr | sed -n "s/^shadowsocksr\\.\\([^.=][^.=]*\\)=servers$/\\1/p"); do
	    node_type="$(uci -q get shadowsocksr.${section}.type)"
	    case "$node_type" in
	        ss|ss-libev)
	            if [ -n "$mihomo_bin" ]; then
	                uci -q set shadowsocksr.${section}.type='ss'
	            elif [ -n "$ss_rust_bin" ]; then
	                uci -q set shadowsocksr.${section}.type='ss-rust'
	            elif [ -n "$xray_bin" ]; then
	                uci -q set shadowsocksr.${section}.type='v2ray'
	                uci -q set shadowsocksr.${section}.v2ray_protocol='shadowsocks'
	            fi
	            ;;
	        v2ray)
	            if [ "$(uci -q get shadowsocksr.${section}.v2ray_protocol)" = "shadowsocks" ] && [ -n "$mihomo_bin" ]; then
	                uci -q set shadowsocksr.${section}.type='ss'
	                uci -q delete shadowsocksr.${section}.v2ray_protocol
	            fi
	            ;;
	    esac
	done

	uci -q commit shadowsocksr
	fi

[ -s "/etc/config/shadowsocksr" ] || /etc/init.d/shadowsocksr reset

sed -i "s/option type 'vmess'/option type 'v2ray'\n\toption v2ray_protocol 'vmess'/g" /etc/config/shadowsocksr
sed -i "s/option type 'vless'/option type 'v2ray'\n\toption v2ray_protocol 'vless'/g" /etc/config/shadowsocksr
sed -i "s/option encrypt_method_v2ray_ss/option encrypt_method_ss/g" /etc/config/shadowsocksr
sed -i "s/option xtls/option tls/g" /etc/config/shadowsocksr
sed -i "/option vless_flow/d" /etc/config/shadowsocksr
sed -i "/option fingerprint 'disable'/d" /etc/config/shadowsocksr

if [ -s "/etc/uwsgi/vassals/luci-webui.ini" ];then
	limit=$(cat /etc/uwsgi/vassals/luci-webui.ini  | grep -Eo "limit-as.*"|grep -Eo "[0-9]+")
	[ $limit -lt 5000 ] && sed -i '/limit-as/c\limit-as = 5000' /etc/uwsgi/vassals/luci-webui.ini && \
	/etc/init.d/uwsgi restart
fi

grep -q ip-api.com /etc/ssrplus/black.list ||
	sed -i '$a ip-api.com' /etc/ssrplus/black.list

rm -f /tmp/luci-indexcache /tmp/luci-indexcache.*
rm -rf /tmp/luci-modulecache/
exit 0
