name: Update Clash Rules
on:
  schedule:
    - cron: '0 23 * * *'  # 每天北京时间早上7点执行
  workflow_dispatch:  # 允许手动触发
  push:
    branches: [ main, master ]

jobs:
  update-rules:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        
      - name: 创建目录结构
        run: |
          mkdir -p ruleset/yaml
      
      - name: 下载并转换规则文件
        run: |
          # 定义规则文件列表
          rules=(
            "icloud"
            "apple"
            "google"
            "proxy"
            "direct"
            "private"
            "gfw"
            "tld-not-cn"
            "telegramcidr"
            "cncidr"
            "lancidr"
            "applications"
          )
          
          # 下载所有规则文件
          for rule in "${rules[@]}"; do
            echo "Processing $rule..."
            
            # 下载 txt 格式
            curl -L "https://cdn.jsdelivr.net/gh/Loyalsoldier/clash-rules@release/${rule}.txt" \
                 -o "ruleset/txt/${rule}.txt" \
                 --fail --silent --show-error
            
            # 转换为 yaml 格式
            echo "payload:" > "ruleset/yaml/${rule}.yaml"
            sed 's/^/  - /' "ruleset/txt/${rule}.txt" >> "ruleset/yaml/${rule}.yaml"
          done
          
      - name: 下载并转换adti-ad规则文件
        run: |
          # 下载原始规则文件
          curl -L "https://anti-ad.net/clash.yaml" \
                 -o "ruleset/yaml/reject.yaml" \
                 --fail --silent --show-error && \

          # 转换为 txt 格式
          sed 's/^/  - /' "ruleset/yaml/reject.yaml" > "ruleset/txt/reject.txt"

      - name: 生成 README
        run: |
          cat > README.md << EOF
          # Clash Rules
          
          自动同步 [clash-rules](https://github.com/Loyalsoldier/clash-rules) 规则文件。</br>
          其中广告过滤规则替换为更好的[anti-AD](https://github.com/privacy-protection-tools/anti-AD?tab=readme-ov-file) : anti-ad-clash.yaml。
          
          ## 文件说明
          
          | 规则 | 说明 | TXT | YAML |
          |------|------|-----|------|
          | reject | 广告拦截 | [链接](./ruleset/txt/reject.txt) | [链接](./ruleset/yaml/reject.yaml) |
          | apple | 苹果服务 | [链接](./ruleset/txt/apple.txt) | [链接](./ruleset/yaml/apple.yaml) |
          | google | 谷歌服务 | [链接](./ruleset/txt/google.txt) | [链接](./ruleset/yaml/google.yaml) |
          | proxy | 代理列表 | [链接](./ruleset/txt/proxy.txt) | [链接](./ruleset/yaml/proxy.yaml) |
          | direct | 直连列表 | [链接](./ruleset/txt/direct.txt) | [链接](./ruleset/yaml/direct.yaml) |
          | gfw | GFW列表 | [链接](./ruleset/txt/gfw.txt) | [链接](./ruleset/yaml/gfw.yaml) |
          | cncidr | 中国IP段 | [链接](./ruleset/txt/cncidr.txt) | [链接](./ruleset/yaml/cncidr.yaml) |
          | telegramcidr | Telegram IP | [链接](./ruleset/txt/telegramcidr.txt) | [链接](./ruleset/yaml/telegramcidr.yaml) |
          
          ## 使用方法
          
          在 Clash 配置文件中添加：
          
          \`\`\`yaml
          rule-providers:
          EOF
          
          # 定义规则类型
          declare -A rule_types=(
            ["reject"]="domain"
            ["icloud"]="domain"
            ["apple"]="domain"
            ["google"]="domain"
            ["proxy"]="domain"
            ["direct"]="domain"
            ["private"]="domain"
            ["telegramcidr"]="ipcidr"
            ["cncidr"]="ipcidr"
            ["lancidr"]="ipcidr"
            ["applications"]="classical"
          )
          
          # 生成 rule-providers
          for rule in reject icloud apple google proxy direct private telegramcidr cncidr lancidr applications; do
            cat >> README.md << EOF
            ${rule}:
              type: http
              behavior: ${rule_types[$rule]}
              url: "https://git.nvme.cc/${{ github.repository }}/raw/branch/main/ruleset/yaml/${rule}.yaml"
              path: ./ruleset/${rule}.yaml
              interval: 86400
              
          EOF
          done
          
          # 添加 rules 部分
          cat >> README.md << EOF
          rules:
            - RULE-SET,applications,DIRECT
            - RULE-SET,private,DIRECT
            - RULE-SET,reject,REJECT
            - RULE-SET,icloud,DIRECT
            - RULE-SET,apple,DIRECT
            - RULE-SET,google,PROXY
            - RULE-SET,proxy,PROXY
            - RULE-SET,direct,DIRECT
            - RULE-SET,telegramcidr,PROXY
            - RULE-SET,lancidr,DIRECT
            - RULE-SET,cncidr,DIRECT
            - GEOIP,LAN,DIRECT
            - GEOIP,CN,DIRECT
            - MATCH,PROXY
          \`\`\`
          
          最后更新: $(TZ='Asia/Shanghai' date +'%Y-%m-%d %H:%M')
          EOF
          
      - name: 提交更改
        run: |
          git config --local user.email "gitea-actions[bot]@users.noreply.nvme.cc"
          git config --local user.name "gitea-actions[bot]"
          git add .
          git diff --cached --quiet || git commit -m "Update rules: $(TZ=Asia/Shanghai date +'%Y-%m-%d %H:%M:%S')"
          git push