name: update clash-meta.yaml

on:
  push:
    paths-ignore:
      - "README.md"
      - "script/**"
      - "source/**"
      - ".gitignore"
    branches:
      - main
  schedule:
    - cron: '0 23 * * *'
    - cron: '0 11 * * *'

jobs:
  Build:
    permissions: write-all
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          persist-credentials: false
          fetch-depth: 0

      - name: Update Clash 0
        run: |
          wget -O source/v2ray.txt ${{ secrets.V2RAY_API }}
          wget -O source/clash-meta.yaml ${{ secrets.CLASH_API }}
          rand=_$(date +%s%N | md5sum |cut -c 2-6)_
          sed -i "s/${{ secrets.CLASH_REPLACE }}/$rand/g" source/clash-meta.yaml

      - name: Update Clash 1
        run: |
          curl -L ${{ secrets.CLASH_API_1 }} > source/clash-meta-1.yaml
          rand=www.$(date +%s%N | md5sum |cut -c 3-7).com
          sed -i "s/${{ secrets.CLASH_REPLACE_1 }}/$rand/g" source/clash-meta-1.yaml

      - name: Update Clash 2
        run: |
          rand=$(curl ${{ secrets.CLASH_API_2 }} | grep -Eo "Clash: +.*" | sed 's/Clash: //')
          curl $rand > source/clash-meta-2.yaml
          rand=$(curl ${{ secrets.CLASH_API_2 }} | grep -Eo "V2Ray: +.*" | sed 's/V2Ray: //')
          curl $rand > source/v2ray-2.txt

      - name: Commit files
        run: |
          git config --local user.email "snakem982@gmail.com"
          git config --local user.name "snakem982"
          git add .
          git commit -a -m "update clash-meta.yaml"

      - name: Git Push
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}
          
