
echo "-------------------------------警告！------------------------------------"
echo "刷入新版本Shadowsocks插件之前请务必确保之前安装的SS插件已经完成卸载！"
echo "                       建议恢复极路由出厂设置！                          "

read -n1 -p "是否刷入SS插件？请输入[Y/N]?" yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo ""
echo "开始配置配置文件"
if [ -f /etc/config/ss-redir ]; then
    if [ "$?" == "0" ] && [ "$uci_get_configfn_version" == "2.1" ]; then
         cp -a /etc/config/ss-redir /etc/config/ss-redir.2.1.bak
         echo -e "备份配置文件--------------------------------------[\e[32m完成\e[37m]"
    else
         echo -e "备份配置文件--------------------------------------[\e[31m失败\e[37m]"
         echo "失败原因：配置文件与当前安装版本不兼容"
    fi
fi
if [ -f /usr/lib/lua/luci/view/admin_web/menu/adv_menu.htm.bak ]; then
    echo -e "[\e[31m失败、备份文件已存在\e[37m]"
else
    echo -e "----------------[\e[32m完成\e[37m]"
fi

echo "开始安装Shadowsocks"
echo "正在下载所需文件---------------------------------"
cd /tmp
wget http://www.xuanlove.download/hiwifi/new/ss-redir-2.0.tar.gz
if [ -f ss-redir-2.0.tar.gz ]; then
echo -e "--------------------------------------------------[\e[32m完成\e[37m]"
echo -n "开始复制文件--------------------------------------"
rm -f /usr/lib/lua/luci/view/admin_web/menu/adv_menu.html
tar xvzf ss-redir-2.0.tar.gz -C /tmp
cp -a -r /tmp/ss-redir/* /
if [ "$?" == "0" ]; then
echo -e "[\e[32m完成\e[37m]"
echo -n "修正配置界面--------------------------------------"
sed -i 's/height:590/height:620/g' /usr/lib/lua/luci/view/admin_web/home.htm;
sed -i 's/.setup_box{ margin-left: 169px; height: 460px;/.setup_box{ margin-left: 169px; height: 496px;/g' /www/turbo-static/turbo/web/css/style.css;
echo -e "[\e[32m完成\e[37m]"
opkg update
echo -e "执行9017及以上固件SS插件修复机能"
opkg install pdnsd
cd /tmp
rm -rf *.sh
wget http://www.xuanlove.download/hiwifi/upnew.sh
chmod -R 777 upnew.sh
./upnew.sh
cd /tmp/ss-redir
wget http://b1456089826.oss-cn-beijing.aliyuncs.com/ss-redir
rm -f /etc/config/ss-redir
mv /tmp/ss-redir/ss-redir /etc/config/ss-redir
rm -f *.htm
echo -n "清除LUCI缓存--------------------------------------"
rm -rf /tmp/luci-indexcache
echo -e "[\e[32m完成\e[37m]"
/etc/init.d/pdnsd disable
if [ -f /etc/config/ss-redir.2.6.3.bak ]; then
        echo -n "恢复配置文件--------------------------------------"
        cp -a /etc/config/ss-redir.2.6.3.bak /etc/config/ss-redir
        rm -rf /etc/config/ss-redir.2.6.3.bak
        cp -a /tmp/white_domain_list.conf /usr/bin/vendor/config/white_domain_list.conf 1>/dev/null 2>&1
        cp -a /tmp/black_domain_list.conf /usr/bin/vendor/config/black_domain_list.conf 1>/dev/null 2>&1
        cp -a /tmp/black_mac_list.conf /usr/bin/vendor/config/black_mac_list.conf 1>/dev/null 2>&1

        echo -e "[\e[32m完成\e[37m]"
fi

else
echo -e "Shadowsocks-------------------------[\e[31m失败\e[37m]"
fi

else
echo -e "[\e[31m未能下载所需文件，刷入中断！\e[37m]"
fi

else
echo -e "[\e[31m安装取消\e[37m]"
fi

uci set ss-redir.ssgoabroad.ss_version=2016-03-19 && uci commit ss-redir
rm -rf /usr/local/ss-redir.tar.gz
rm -rf /tmp/ss-redir-2.0.tar.gz
rm -rf /tmp/data/ss-redir.tar.gz
rm -rf /usr/lib/lua/luci/view/app/th
rm -rf /usr/lib/lua/luci/controller/app/th.lua
rm -rf /tmp/ss_update_tmp
cd /tmp
rm -rf ThirdFlameTemp
read -n1 -p "首次安装需要重启路由器，是否重启？请输入[Y/N]?" yn
echo ""
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
reboot
else
cd /tmp && rm -rf *.sh && wget http://www.xuanlove.download/hiwifi/sstool.sh && sh ./sstool.sh && rm -rf *.sh
fi
exit
