name: singbox_srs文件生成

on:
  workflow_dispatch:

permissions:
  contents: write

jobs:
  build-and-push-ruleset:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout master branch
        uses: actions/checkout@v4
        with:
          ref: master        # ← 检出 master 分支
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download sing-box
        run: |
          wget https://github.com/SagerNet/sing-box/releases/download/v1.12.17/sing-box-1.12.17-linux-amd64.tar.gz
          tar -xzf sing-box-1.12.17-linux-amd64.tar.gz
          echo "$PWD/sing-box-1.12.17-linux-amd64" >> $GITHUB_PATH

      - name: Compile JSON → SRS
        run: |
          cd sing-box
          for file in *.json; do
            [ -f "$file" ] || continue
            echo "Compiling $file → ${file%.json}.srs"
            ../sing-box-1.12.17-linux-amd64/sing-box rule-set compile \
              --output "${file%.json}.srs" "$file"
          done

      - name: Commit and Push to master
        run: |
          git config user.name "GitHub Action"
          git config user.email "action@github.com"
          git add sing-box/*.srs
          git commit -m "Update SRS (Auto compiled)" || echo "No changes."
          git push origin master  # ← 推回 master
