#!/usr/bin/env bash

set -euo pipefail
repo_name="$(basename "$GITHUB_REPOSITORY")"

download_single() {
    local output_path="$1"
    local url="$2"
    echo "Downloading_Single: $url -> $output_path"
    curl -fsSL --retry 3 --retry-delay 2 -o "$output_path" "$url" || {
        echo "Download failed: $url"
        exit 1
    }
}

download_merges() {
    local output_path="$1"; shift; : > "$output_path"
    for url in "$@"; do
        echo "Downloading_Merges: $url -> $output_path"
        curl -fsSL --retry 3 --retry-delay 2 "$url" >> "$output_path" || {
            echo "Download failed: $url"
            exit 1
        }
        echo >> "$output_path"
    done
}

if [[ "$repo_name" == "Scripts" ]]; then
    echo "Running in Scripts Repository"
    scripts_dirs=("Ruleset" "QuantumultX/Ruleset" "Stash/Ruleset" "Surge/Ruleset")
    for dir in "${scripts_dirs[@]}"; do
        mkdir -p "Scripts-repo/$dir"
    done
    declare -A ruleA_download=(
        ["Scripts-repo/Ruleset/AdBlockLite.list"]="
            https://raw.githubusercontent.com/ConnersHua/RuleGo/master/Surge/Ruleset/Extra/Reject/Advertising.list
            https://raw.githubusercontent.com/ConnersHua/RuleGo/master/Surge/Ruleset/Extra/Reject/Malicious.list
            https://raw.githubusercontent.com/ConnersHua/RuleGo/master/Surge/Ruleset/Extra/Reject/Tracking.list
            https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/BanAD.list
            https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/BanProgramAD.list
            https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/BanEasyListChina.list"
        ["Scripts-repo/Ruleset/Global.list"]="
            https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/ProxyGFWlist.list
            https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/gfw.txt"
    )
    declare -A ruleB_download=(
        ["Scripts-repo/Ruleset/AdBlock.list"]="https://raw.githubusercontent.com/privacy-protection-tools/anti-AD/master/anti-ad-surge.txt"
        ["Scripts-repo/Ruleset/AdGuardDNS.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/AdGuardSDNSFilter/AdGuardSDNSFilter.list"
        ["Scripts-repo/Ruleset/Advertising.list"]="https://raw.githubusercontent.com/Cats-Team/AdRules/main/adrules.list"
        ["Scripts-repo/Ruleset/Alibaba.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Alibaba.list"
        ["Scripts-repo/Ruleset/AmazonIP.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/AmazonIp.list"
        ["Scripts-repo/Ruleset/Apple.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Apple.list"
        ["Scripts-repo/Ruleset/Baidu.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Baidu.list"
        ["Scripts-repo/Ruleset/BiliBili.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Bilibili.list"
        ["Scripts-repo/Ruleset/CNCIDR.list"]="https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/cn.txt"
        ["Scripts-repo/Ruleset/CNCIDR4.list"]="https://raw.githubusercontent.com/Hackl0us/GeoIP2-CN/release/CN-ip-cidr.txt"
        ["Scripts-repo/Ruleset/CNCIDR6.list"]="https://raw.githubusercontent.com/Masaiki/GeoIP2-CN/release/CN-ip-cidr.txt"
        ["Scripts-repo/Ruleset/China.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/ChinaDomain.list"
        ["Scripts-repo/Ruleset/ChinaASN.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Surge/ChinaASN/ChinaASN_Resolve.list"
        ["Scripts-repo/Ruleset/ChinaIPBGP.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Surge/ChinaIPsBGP/ChinaIPsBGP.list"
        ["Scripts-repo/Ruleset/ChinaIPMax.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Surge/ChinaIPs/ChinaIPs.list"
        ["Scripts-repo/Ruleset/ChinaIPv4.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/ChinaIp.list"
        ["Scripts-repo/Ruleset/ChinaIPv6.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/ChinaIpV6.list"
        ["Scripts-repo/Ruleset/ChinaMax.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/ChinaMax/ChinaMax.list"
        ["Scripts-repo/Ruleset/ChinaMediaMax.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/ChinaMedia/ChinaMedia.list"
        ["Scripts-repo/Ruleset/DIRECT.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/direct.txt"
        ["Scripts-repo/Ruleset/Easylist.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/BanEasyList.list"
        ["Scripts-repo/Ruleset/Game.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Surge/Game/Game.list"
        ["Scripts-repo/Ruleset/GlobalMax.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/Global/Global.list"
        ["Scripts-repo/Ruleset/GlobalMediaMax.list"]="https://raw.githubusercontent.com/blackmatrix7/ios_rule_script/master/rule/Clash/GlobalMedia/GlobalMedia.list"
        ["Scripts-repo/Ruleset/Google.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Google.list"
        ["Scripts-repo/Ruleset/GreatFire.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/greatfire.txt"
        ["Scripts-repo/Ruleset/GreatFireWall.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/gfw.txt"
        ["Scripts-repo/Ruleset/Microsoft.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Microsoft.list"
        ["Scripts-repo/Ruleset/Netflix.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Netflix.list"
        ["Scripts-repo/Ruleset/PROXY.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/proxy.txt"
        ["Scripts-repo/Ruleset/Privacy.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/BanEasyPrivacy.list"
        ["Scripts-repo/Ruleset/Private.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/private.txt"
        ["Scripts-repo/Ruleset/PrivateTracker.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/PrivateTracker.list"
        ["Scripts-repo/Ruleset/REJECT.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/reject.txt"
        ["Scripts-repo/Ruleset/Tld-Not-CN.list"]="https://raw.githubusercontent.com/Loyalsoldier/surge-rules/release/ruleset/tld-not-cn.txt"
        ["Scripts-repo/Ruleset/USCIDR.list"]="https://raw.githubusercontent.com/Loyalsoldier/geoip/release/text/us.txt"
        ["Scripts-repo/Ruleset/WeChat.list"]="https://raw.githubusercontent.com/ACL4SSR/ACL4SSR/master/Clash/Ruleset/Wechat.list"
    )
    for out_file in "${!ruleA_download[@]}"; do
        mapfile -t urls < <(echo "${ruleA_download[$out_file]}" | sed -E 's/^[[:space:]]+//; /^[[:space:]]*$/d')
        download_merges "$out_file" "${urls[@]}"
    done
    for out_file in "${!ruleB_download[@]}"; do
        download_single "$out_file" "${ruleB_download[$out_file]}"
    done
    declare -A ruleC_download=(
        ["AdBlock"]="AdBlock.list"
        ["Advertising"]="Advertising.list"
        ["AppStore"]="AppStore.list"
        ["Apple"]="Apple.list"
        ["BiliBili"]="BiliBili.list"
        ["ChinaASN"]="ChinaASN.list"
        ["ChinaMedia"]="ChinaMedia.list"
        ["DouYin"]="DouYin.list"
        ["GEOIPCN"]="GEOIPCN.list"
        ["Global"]="Global.list"
        ["GlobalMedia"]="GlobalMedia.list"
        ["HMTMedia"]="HMTMedia.list"
        ["LAN"]="LAN.list"
        ["TikTok"]="TikTok.list"
        ["Unbreak"]="Unbreak.list"
    )
    declare -A formats=(
        # ["Egern"]="yaml"
        ["QuantumultX"]="list"
        # ["Sing-box"]="json"
        ["Stash"]="yaml"
        ["Surge"]="list"
    )
    for dir in "${!formats[@]}"; do
        mkdir -p "Scripts-repo/$dir/Ruleset"
    done
    for key in "${!ruleC_download[@]}"; do
        file="Scripts-repo/Ruleset/${ruleC_download[$key]}"
        base="${ruleC_download[$key]%.*}"
        [[ -f "$file" ]] || { echo "Skipped: $file"; continue; }
        for dir in "${!formats[@]}"; do
            cp "$file" "Scripts-repo/$dir/Ruleset/$base.${formats[$dir]}"
        done
        echo "Processed: $file"
    done
    echo "Scripts Repository: All Rules Downloaded!"

elif [[ "$repo_name" == "Matrix-io" ]]; then
    echo "Running in Matrix-io Repository"
    matrix_io_dirs=("Clash" "Egern" "Loon" "QuantumultX" "Shadowrocket" "Sing-box" "Stash" "Surge")
    for dir in "${matrix_io_dirs[@]}"; do
        mkdir -p "Matrix-io-repo/$dir/Ruleset"
    done
    declare -A ruleset=(
        # ["0x0"]="0x0.list"
        # ["18comic"]="18comic.list"
        # ["360"]="360.list"
        # ["4399"]="4399.list"
        # ["51Job"]="51Job.list"
        # ["58TC"]="58TC.list"
        ["ABC"]="ABC.list"
        ["AMAP"]="AMAP.list"
        # ["ATT"]="ATT.list"
        # ["ATTWatchTV"]="ATTWatchTV.list"
        # ["Abema"]="Abema.list"
        # ["AbemaTV"]="AbemaTV.list"
        ["AcFun"]="AcFun.list"
        ["AdBlock"]="AdBlock.list"
        # ["AdBlockLite"]="AdBlockLite.list"
        # ["AdGuard"]="AdGuard.list"
        # ["AdGuardDNS"]="AdGuardDNS.list"
        # ["Adobe"]="Adobe.list"
        ["Advertising"]="Advertising.list"
        # ["Aixcoder"]="Aixcoder.list"
        # ["Akamai"]="Akamai.list"
        # ["AliCloud"]="AliCloud.list"
        ["AliPay"]="AliPay.list"
        ["Alibaba"]="Alibaba.list"
        ["All4"]="All4.list"
        ["Amazon"]="Amazon.list"
        ["AmazonCN"]="AmazonCN.list"
        # ["AmazonIP"]="AmazonIP.list"
        # ["Anaconda"]="Anaconda.list"
        ["Android"]="Android.list"
        # ["Aparat"]="Aparat.list"
        ["AppStore"]="AppStore.list"
        ["Apple"]="Apple.list"
        # ["AppleDve"]="AppleDve.list"
        # ["AppleMail"]="AppleMail.list"
        # ["AppleMedia"]="AppleMedia.list"
        # ["AppleMusic"]="AppleMusic.list"
        ["AppleTV"]="AppleTV.list"
        # ["Avmoo"]="Avmoo.list"
        # ["Azure"]="Azure.list"
        # ["BBC"]="BBC.list"
        # ["BOC"]="BOC.list"
        # ["BOCOM"]="BOCOM.list"
        ["Baidu"]="Baidu.list"
        # ["BaiduCloud"]="BaiduCloud.list"
        # ["BaiduTieba"]="BaiduTieba.list"
        # ["BeStore"]="BeStore.list"
        ["BiliBili"]="BiliBili.list"
        # ["BiliBiliIntl"]="BiliBiliIntl.list"
        # ["Binance"]="Binance.list"
        # ["Bing"]="Bing.list"
        # ["Bitauto"]="Bitauto.list"
        # ["Bitly"]="Bitly.list"
        ["Blizzard"]="Blizzard.list"
        # ["Blued"]="Blued.list"
        # ["Boylove"]="Boylove.list"
        # ["Bybit"]="Bybit.list"
        ["ByteDance"]="ByteDance.list"
        # ["CCB"]="CCB.list"
        # ["CCTV"]="CCTV.list"
        # ["CDN"]="CDN.list"
        # ["CDN77"]="CDN77.list"
        ["CNCIDR"]="CNCIDR.list"
        ["CNCIDR4"]="CNCIDR4.list"
        ["CNCIDR6"]="CNCIDR6.list"
        # ["CNKI"]="CNKI.list"
        # ["CSDN"]="CSDN.list"
        # ["CaiNiao"]="CaiNiao.list"
        # ["Canon"]="Canon.list"
        # ["Canonical"]="Canonical.list"
        # ["Cavporn"]="Cavporn.list"
        # ["Cern"]="Cern.list"
        # ["China"]="China.list"
        ["ChinaASN"]="ChinaASN.list"
        # ["ChinaDNS"]="ChinaDNS.list"
        ["ChinaIPBGP"]="ChinaIPBGP.list"
        ["ChinaIPMax"]="ChinaIPMax.list"
        ["ChinaIPv4"]="ChinaIPv4.list"
        ["ChinaIPv6"]="ChinaIPv6.list"
        ["ChinaMax"]="ChinaMax.list"
        ["ChinaMedia"]="ChinaMedia.list"
        # ["ChinaMediaMax"]="ChinaMediaMax.list"
        # ["ChinaMobile"]="ChinaMobile.list"
        # ["ChinaTelecom"]="ChinaTelecom.list"
        # ["ChinaUnicom"]="ChinaUnicom.list"
        # ["ChunYou"]="ChunYou.list"
        ["Claude"]="Claude.list"
        # ["Cloudcone"]="Cloudcone.list"
        # ["Cloudflare"]="Cloudflare.list"
        # ["Coinone"]="Coinone.list"
        # ["Collabora"]="Collabora.list"
        # ["Comodo"]="Comodo.list"
        ["Coolapk"]="Coolapk.list"
        ["Copilot"]="Copilot.list"
        # ["CosplayTele"]="CosplayTele.list"
        # ["Ctexcel"]="Ctexcel.list"
        # ["DAZN"]="DAZN.list"
        ["DIRECT"]="DIRECT.list"
        # ["DMM"]="DMM.list"
        # ["DNSPod"]="DNSPod.list"
        # ["Dart"]="Dart.list"
        # ["Dcard"]="Dcard.list"
        # ["DeepSeek"]="DeepSeek.list"
        # ["Deepin"]="Deepin.list"
        ["Deezer"]="Deezer.list"
        # ["Dettol"]="Dettol.list"
        # ["Developer"]="Developer.list"
        # ["DingTalk"]="DingTalk.list"
        # ["DirectCDN"]="DirectCDN.list"
        ["Discord"]="Discord.list"
        # ["Discovery"]="Discovery.list"
        # ["Discuz"]="Discuz.list"
        # ["Disney"]="Disney.list"
        ["Docker"]="Docker.list"
        # ["Domob"]="Domob.list"
        # ["Dood"]="Dood.list"
        ["DouBan"]="DouBan.list"
        ["DouYin"]="DouYin.list"
        ["DouYu"]="DouYu.list"
        ["Dropbox"]="Dropbox.list"
        # ["Drweb"]="Drweb.list"
        # ["Dubox"]="Dubox.list"
        # ["Durex"]="Durex.list"
        # ["EBAY"]="EBAY.list"
        # ["EHGallery"]="EHGallery.list"
        # ["ESPN"]="ESPN.list"
        # ["Easylist"]="Easylist.list"
        # ["Eleme"]="Eleme.list"
        # ["Embl"]="Embl.list"
        # ["EncoreTVB"]="EncoreTVB.list"
        # ["Epic"]="Epic.list"
        # ["F-droid"]="F-droid.list"
        # ["FT"]="FT.list"
        ["Facebook"]="Facebook.list"
        # ["FacebookDev"]="FacebookDev.list"
        # ["FanQienovel"]="FanQienovel.list"
        # ["Fandom"]="Fandom.list"
        # ["Fedora"]="Fedora.list"
        # ["Feizhu"]="Feizhu.list"
        # ["FireFox"]="FireFox.list"
        # ["Flickr"]="Flickr.list"
        # ["Fox"]="Fox.list"
        # ["FoxNow"]="FoxNow.list"
        # ["FoxPlus"]="FoxPlus.list"
        # ["FuboTV"]="FuboTV.list"
        ["GEOIPCN"]="GEOIPCN.list"
        # ["GEOIPUS"]="GEOIPUS.list"
        # ["Game"]="Game.list"
        # ["Gannett"]="Gannett.list"
        # ["Garena"]="Garena.list"
        # ["Gateio"]="Gateio.list"
        # ["Gemini"]="Gemini.list"
        # ["GitBook"]="GitBook.list"
        ["GitHub"]="GitHub.list"
        ["GitLab"]="GitLab.list"
        # ["Gitee"]="Gitee.list"
        ["Global"]="Global.list"
        ["GlobalMax"]="GlobalMax.list"
        ["GlobalMedia"]="GlobalMedia.list"
        # ["GlobalMediaMax"]="GlobalMediaMax.list"
        ["Google"]="Google.list"
        ["GoogleCN"]="GoogleCN.list"
        # ["GoogleDrive"]="GoogleDrive.list"
        # ["GoogleEarth"]="GoogleEarth.list"
        # ["GoogleVoice"]="GoogleVoice.list"
        # ["GovernmentCN"]="GovernmentCN.list"
        # ["GreatFire"]="GreatFire.list"
        ["GreatFireWall"]="GreatFireWall.list"
        # ["Gucci"]="Gucci.list"
        ["HBO"]="HBO.list"
        ["HBOAsia"]="HBOAsia.list"
        # ["HKBN"]="HKBN.list"
        # ["HKT"]="HKT.list"
        ["HMTMedia"]="HMTMedia.list"
        # ["HTTPDNS"]="HTTPDNS.list"
        # ["HWTV"]="HWTV.list"
        # ["HamiVideo"]="HamiVideo.list"
        # ["Hanime1"]="Hanime1.list"
        # ["Hema"]="Hema.list"
        ["Heroku"]="Heroku.list"
        # ["Hetzner"]="Hetzner.list"
        # ["Hinet"]="Hinet.list"
        ["HuYa"]="HuYa.list"
        # ["Huawei"]="Huawei.list"
        ["Hulu"]="Hulu.list"
        ["HuluJP"]="HuluJP.list"
        # ["Huobi"]="Huobi.list"
        # ["Hupu"]="Hupu.list"
        # ["IBM"]="IBM.list"
        # ["ICBC"]="ICBC.list"
        ["ICloud"]="ICloud.list"
        # ["IEEE"]="IEEE.list"
        # ["IFLYTEK"]="IFLYTEK.list"
        # ["IMDB"]="IMDB.list"
        ["IQiYi"]="IQiYi.list"
        # ["IQiYiIntl"]="IQiYiIntl.list"
        # ["ITV"]="ITV.list"
        # ["Instagram"]="Instagram.list"
        # ["Intel"]="Intel.list"
        # ["IntelDev"]="IntelDev.list"
        # ["Intercom"]="Intercom.list"
        # ["JD"]="JD.list"
        ["JOOX"]="JOOX.list"
        # ["Jable"]="Jable.list"
        # ["Japonx"]="Japonx.list"
        # ["Javdb"]="Javdb.list"
        # ["JianGuoYun"]="JianGuoYun.list"
        # ["KKBOX"]="KKBOX.list"
        # ["KKTV"]="KKTV.list"
        # ["KWKG"]="KWKG.list"
        # ["KakaoTalk"]="KakaoTalk.list"
        # ["Keep"]="Keep.list"
        # ["Kemono"]="Kemono.list"
        # ["Kindle"]="Kindle.list"
        # ["Koolearn"]="Koolearn.list"
        # ["Kugou"]="Kugou.list"
        # ["Kuwo"]="Kuwo.list"
        ["LAN"]="LAN.list"
        # ["LANCIDR"]="LANCIDR.list"
        # ["LZY"]="LZY.list"
        # ["Landian"]="Landian.list"
        # ["Lanzou"]="Lanzou.list"
        # ["Lark"]="Lark.list"
        # ["LeTV"]="LeTV.list"
        # ["Lenovo"]="Lenovo.list"
        # ["LiTV"]="LiTV.list"
        # ["Line"]="Line.list"
        # ["LineTV"]="LineTV.list"
        # ["Linkedin"]="Linkedin.list"
        # ["MGTV"]="MGTV.list"
        # ["MI"]="MI.list"
        # ["MIUI"]="MIUI.list"
        # ["Mailru"]="Mailru.list"
        # ["Manorama"]="Manorama.list"
        # ["Maocloud"]="Maocloud.list"
        # ["MapBox"]="MapBox.list"
        # ["Marvel"]="Marvel.list"
        # ["Mastercard"]="Mastercard.list"
        # ["Matrix"]="Matrix.list"
        # ["MeiTuan"]="MeiTuan.list"
        # ["Meizu"]="Meizu.list"
        # ["Metart"]="Metart.list"
        # ["MiGu"]="MiGu.list"
        # ["MiHoYo"]="MiHoYo.list"
        ["Microsoft"]="Microsoft.list"
        # ["MicrosoftDev"]="MicrosoftDev.list"
        # ["Mozilla"]="Mozilla.list"
        # ["My5"]="My5.list"
        ["MyTVSuper"]="MyTVSuper.list"
        # ["NGAA"]="NGAA.list"
        # ["NVIDIA"]="NVIDIA.list"
        # ["NaSDDNS"]="NaSDDNS.list"
        # ["Naver"]="Naver.list"
        # ["NaverTV"]="NaverTV.list"
        ["NetEase"]="NetEase.list"
        ["NetEaseMusic"]="NetEaseMusic.list"
        ["Netflix"]="Netflix.list"
        # ["NetflixIP"]="NetflixIP.list"
        # ["Nexon"]="Nexon.list"
        # ["Niconico"]="Niconico.list"
        # ["Nintendo"]="Nintendo.list"
        ["NivodTV"]="NivodTV.list"
        # ["Notion"]="Notion.list"
        # ["NowE"]="NowE.list"
        # ["OKX"]="OKX.list"
        # ["OPPO"]="OPPO.list"
        # ["OUP"]="OUP.list"
        # ["Olevod"]="Olevod.list"
        ["Onedrive"]="Onedrive.list"
        ["OpenAI"]="OpenAI.list"
        # ["Opera"]="Opera.list"
        ["Oracle"]="Oracle.list"
        # ["Oreilly"]="Oreilly.list"
        # ["PDD"]="PDD.list"
        # ["PPTV"]="PPTV.list"
        # ["PPX"]="PPX.list"
        ["PROXY"]="PROXY.list"
        # ["Pandora"]="Pandora.list"
        # ["PandoraTV"]="PandoraTV.list"
        # ["Paramount"]="Paramount.list"
        # ["Patreon"]="Patreon.list"
        ["PayPal"]="PayPal.list"
        # ["Pccw"]="Pccw.list"
        # ["Peacock"]="Peacock.list"
        # ["Perl"]="Perl.list"
        # ["Picsee"]="Picsee.list"
        ["PikPak"]="PikPak.list"
        # ["Pinterest"]="Pinterest.list"
        # ["Pixiv"]="Pixiv.list"
        # ["Pixnet"]="Pixnet.list"
        # ["Porn"]="Porn.list"
        ["PornHub"]="PornHub.list"
        # ["PornMax"]="PornMax.list"
        # ["PrimeVideo"]="PrimeVideo.list"
        # ["Privacy"]="Privacy.list"
        # ["Private"]="Private.list"
        # ["PrivateTracker"]="PrivateTracker.list"
        # ["Protonmail"]="Protonmail.list"
        # ["Python"]="Python.list"
        # ["QQ"]="QQ.list"
        # ["QQMusic"]="QQMusic.list"
        # ["QQSpace"]="QQSpace.list"
        # ["Qcloud"]="Qcloud.list"
        ["Qobuz"]="Qobuz.list"
        # ["Qualcomm"]="Qualcomm.list"
        ["Quark"]="Quark.list"
        ["Quora"]="Quora.list"
        # ["Qwant"]="Qwant.list"
        # ["RB"]="RB.list"
        ["REJECT"]="REJECT.list"
        # ["RTHK"]="RTHK.list"
        # ["Rakuten"]="Rakuten.list"
        # ["Razer"]="Razer.list"
        # ["Reabble"]="Reabble.list"
        # ["Realclear"]="Realclear.list"
        # ["RedBook"]="RedBook.list"
        # ["RedHat"]="RedHat.list"
        # ["Reddit"]="Reddit.list"
        # ["Redtube"]="Redtube.list"
        # ["Riot"]="Riot.list"
        # ["Rouman5"]="Rouman5.list"
        # ["Ruanmei"]="Ruanmei.list"
        # ["SFExpress"]="SFExpress.list"
        # ["SINA"]="SINA.list"
        # ["SMZDM"]="SMZDM.list"
        # ["STUN"]="STUN.list"
        # ["Scholar"]="Scholar.list"
        # ["Seasun"]="Seasun.list"
        # ["Secom"]="Secom.list"
        # ["Shopee"]="Shopee.list"
        # ["Shopify"]="Shopify.list"
        # ["Slack"]="Slack.list"
        # ["Snapp"]="Snapp.list"
        # ["Sogou"]="Sogou.list"
        # ["SohuVideo"]="SohuVideo.list"
        # ["Soul"]="Soul.list"
        ["SoundCloud"]="SoundCloud.list"
        # ["Spark"]="Spark.list"
        ["Speedtest"]="Speedtest.list"
        ["Spotify"]="Spotify.list"
        ["Steam"]="Steam.list"
        ["SteamCN"]="SteamCN.list"
        # ["SteamCheck"]="SteamCheck.list"
        # ["Streaming"]="Streaming.list"
        # ["StreamingCN"]="StreamingCN.list"
        # ["StreamingHK"]="StreamingHK.list"
        # ["StreamingJP"]="StreamingJP.list"
        # ["StreamingKR"]="StreamingKR.list"
        # ["StreamingSE"]="StreamingSE.list"
        # ["StreamingTW"]="StreamingTW.list"
        # ["StreamingUK"]="StreamingUK.list"
        # ["StreamingUS"]="StreamingUS.list"
        # ["Sumkoo"]="Sumkoo.list"
        # ["Supercell"]="Supercell.list"
        # ["TCL"]="TCL.list"
        ["TIDAL"]="TIDAL.list"
        # ["TVB"]="TVB.list"
        # ["TVer"]="TVer.list"
        # ["TaiwanGood"]="TaiwanGood.list"
        # ["TaoBao"]="TaoBao.list"
        ["TapTap"]="TapTap.list"
        # ["TeamViewer"]="TeamViewer.list"
        ["Telegram"]="Telegram.list"
        # ["Telekom"]="Telekom.list"
        ["Tencent"]="Tencent.list"
        # ["TencentDev"]="TencentDev.list"
        ["TencentVideo"]="TencentVideo.list"
        # ["Tendcloud"]="Tendcloud.list"
        # ["TeraBox"]="TeraBox.list"
        # ["Termux"]="Termux.list"
        # ["TestFlight"]="TestFlight.list"
        ["TikTok"]="TikTok.list"
        # ["Tld-CN"]="Tld-CN.list"
        # ["Tld-Not-CN"]="Tld-Not-CN.list"
        # ["Tonec"]="Tonec.list"
        # ["TouTiao"]="TouTiao.list"
        # ["Tumblr"]="Tumblr.list"
        ["Twitch"]="Twitch.list"
        ["Twitter"]="Twitter.list"
        # ["UCBrowser"]="UCBrowser.list"
        # ["USCIDR"]="USCIDR.list"
        # ["UUChat"]="UUChat.list"
        # ["Ubuntu"]="Ubuntu.list"
        # ["Ucloud"]="Ucloud.list"
        ["Unbreak"]="Unbreak.list"
        # ["Upai"]="Upai.list"
        # ["V2ray"]="V2ray.list"
        # ["VISA"]="VISA.list"
        # ["VIVO"]="VIVO.list"
        # ["VK"]="VK.list"
        # ["Vancl"]="Vancl.list"
        # ["Veet"]="Veet.list"
        ["Vercel"]="Vercel.list"
        # ["Verizon"]="Verizon.list"
        # ["Vimeo"]="Vimeo.list"
        # ["ViuTV"]="ViuTV.list"
        # ["Volvo"]="Volvo.list"
        # ["Voxmedia"]="Voxmedia.list"
        # ["WPS"]="WPS.list"
        # ["Walmart"]="Walmart.list"
        # ["Wantmedia"]="Wantmedia.list"
        ["WeChat"]="WeChat.list"
        # ["WeTV"]="WeTV.list"
        # ["Webex"]="Webex.list"
        ["WeiBo"]="WeiBo.list"
        ["WhatsApp"]="WhatsApp.list"
        # ["Wikimedia"]="Wikimedia.list"
        ["Wikipedia"]="Wikipedia.list"
        # ["Woolite"]="Woolite.list"
        # ["X-AI"]="X-AI.list"
        # ["XNXX"]="XNXX.list"
        # ["XianYu"]="XianYu.list"
        # ["Xiaomi"]="Xiaomi.list"
        ["Ximalaya"]="Ximalaya.list"
        # ["Xunlei"]="Xunlei.list"
        # ["YY"]="YY.list"
        # ["YYeTs"]="YYeTs.list"
        # ["Yahoo"]="Yahoo.list"
        ["Yandex"]="Yandex.list"
        # ["Ynet"]="Ynet.list"
        ["YouTube"]="YouTube.list"
        ["Youku"]="Youku.list"
        # ["Youporn"]="Youporn.list"
        # ["Youquan"]="Youquan.list"
        # ["Youzan"]="Youzan.list"
        # ["Yuewen"]="Yuewen.list"
        # ["Yundaex"]="Yundaex.list"
        # ["Z-library"]="Z-library.list"
        # ["Zee"]="Zee.list"
        # ["ZeeTV"]="ZeeTV.list"
        # ["Zhihu"]="Zhihu.list"
        # ["Zoom"]="Zoom.list"
    )
    declare -A formats=(
        ["Clash"]="yaml"
        ["Egern"]="yaml"
        ["Loon"]="list"
        ["QuantumultX"]="list"
        ["Shadowrocket"]="list"
        ["Sing-box"]="json"
        ["Stash"]="yaml"
        ["Surge"]="list"
    )
    skip_rule=("ChinaASN" "GEOIPCN") # 排除不需要加载的规则
    should_skip_rule() {
        local platform="$1" rule="$2"; [[ "$platform" != "Sing-box" ]] && return 1
        for r in "${skip_rule[@]}"; do [[ "$rule" == "$r" ]] && return 0; done; return 1
    }
    for rule in "${!ruleset[@]}"; do
        for platform in "${!formats[@]}"; do
            should_skip_rule "$platform" "$rule" && { echo "Skipped $rule for $platform"; continue; }
            extension="${formats[$platform]}"
            output_dir="Matrix-io-repo/$platform/Ruleset"
            output_file="$output_dir/$rule.$extension"
            url="https://raw.githubusercontent.com/Centralmatrix3/Scripts/master/Ruleset/${ruleset[$rule]}"
            download_single "$output_file" "$url"
        done
    done
    echo "Matrix-io Repository: All Rules Downloaded!"
else
    echo "Unknown Repository: $repo_name"
    echo "Please Execute Scripts in Scripts or Matrix-io Repository."
    exit 1
fi