#!/bin/bash
export LANG=C.UTF-8
echo "----------------下载节点优选------------"
# 定义要测试的域名列表
domains=("https://cf.mb6.top" "http://cf2.mb6.top" "https://act.jiawei.xin:10086")
# 初始化 domain 变量
domain=""
# 遍历域名列表并测试可访问性
for url in "${domains[@]}"; do
  response=$(curl --write-out "%{http_code}" --silent --output /dev/null "$url")
  if [ "$response" -eq 200 ]; then
    domain="$url"
    echo "------------可访问的下载节点是: $url"
    break
  else
    echo "------------下载节点: $url 不可用,继续测试"
  fi
done
# 检查是否找到可访问的域名
if [ -z "$domain" ]; then
  echo "------------没有可访问的域名"
  exit 1
fi
main_domain=$domain"/tmp/emby"

echo "----------------web播放器开始------------"
mv /var/packages/EmbyServer/target/system/Emby.Web.dll /var/packages/EmbyServer/target/system/Emby.Web.dll.bak
wget -P /var/packages/EmbyServer/target/system/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/Emby.Web.dll"
echo "----------------web核心完成------------"
mv /var/packages/EmbyServer/target/system/dashboard-ui/modules/emby-apiclient/connectionmanager.js /var/packages/EmbyServer/target/system/dashboard-ui/modules/emby-apiclient/connectionmanager.js.bak
wget -P /var/packages/EmbyServer/target/system/dashboard-ui/modules/emby-apiclient/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/dashboard-ui/modules/emby-apiclient/connectionmanager.js"
mv /var/packages/EmbyServer/target/system/dashboard-ui/embypremiere/embypremiere.js /var/packages/EmbyServer/target/system/dashboard-ui/embypremiere/embypremiere.js.bak
wget -P /var/packages/EmbyServer/target/system/dashboard-ui/embypremiere/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/dashboard-ui/embypremiere/embypremiere.js"
echo "----------------web播放器完成------------"
echo "----------------硬解部分开始----------------"
mv /var/packages/EmbyServer/target/system/Emby.Server.Implementations.dll /var/packages/EmbyServer/target/system/Emby.Server.Implementations.dll.bak
wget -P /var/packages/EmbyServer/target/system/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/Emby.Server.Implementations.dll"
echo "----------------硬解部分完成-----------"


# 插件安装询问
read -p "------------是否安装CustomCssJS-24.11.18插件？(y/n): " install_plugin
if [[ "$install_plugin" == "y" || "$install_plugin" == "Y" ]]; then
  echo "------------安装CustomCssJS-24.11.18插件开始..."
  rm -rf /var/packages/EmbyServer/target/system/plugins/Emby.CustomCssJS.*
  wget -P /var/packages/EmbyServer/target/system/plugins/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/plugins/Emby.CustomCssJS.dll"
  echo "------------安装CustomCssJS-24.11.18插件完成..."
else
  echo "------------已跳过安装CustomCssJS插件。"
fi


# read -p "------------是否安装StrmAssistant_less-1.0.57插件？(y/n): " install_plugin2
# if [[ "$install_plugin2" == "y" || "$install_plugin2" == "Y" ]]; then
#   echo "------------安装StrmAssistant_less-1.0.57插件开始..."
#   mv /var/packages/EmbyServer/target/system/plugins/StrmAssistant.dll /var/packages/EmbyServer/target/system/plugins/StrmAssistant.dll.bak
#   wget -P /var/packages/EmbyServer/target/system/plugins/ --no-check-certificate "$main_domain/4.9.1.23b/linux_32_64/plugins/StrmAssistant.dll"
#   echo "------------安装StrmAssistant_less-1.0.57插件完成..."
# else
#   echo "------------已跳过安装StrmAssistant_less-1.0.57插件。"
# fi

echo "----------------设置相关权限(DSM6版本会报错，无影响)"
chown -R emby:emby /var/packages/EmbyServer/target/system/
echo "----------------删除脚本，预防二次运行出错(可能会报错，无影响)"
rm -rf synology6-7crack_4.9.1.23b.sh
echo "----------------完成-------------------"
echo "运行后重启套件,填写任意激活码,清除浏览器缓存."
echo "运行后重启套件,填写任意激活码,清除浏览器缓存."
echo "运行后重启套件,填写任意激活码,清除浏览器缓存."
echo "运行后重启套件,填写任意激活码,清除浏览器缓存."
