name: openwrt_helloworld

on:
  schedule:
    - cron: 0 */4 * * *
  repository_dispatch:
  workflow_dispatch:

jobs:
  job_openwrt_helloworld:
    if: github.event.repository.owner.id == github.event.sender.id || ! github.event.sender.id
    runs-on: ubuntu-latest

    name: Update openwrt_helloworld
    strategy:
      fail-fast: false
      matrix:
        target: [openwrt-25.12]
        
    steps:
    - name: Checkout
      uses: actions/checkout@main
      with:
        fetch-depth: 0

    - name: Initialization environment
      run : |
        git config --global user.email "2519840456@qq.com"
        git config --global user.name "Xiaokailnol"
        
    - name: Clone packages
      run: |
        cd $GITHUB_WORKSPACE
        chmod +x .github/diy/openwrt_helloworld.sh
        git clone -b openwrt-25.12 https://gitea.kejizero.xyz/zhao/openwrt_helloworld.git ${{matrix.target}}
        cd ${{matrix.target}}
        git rm -r --cache * >/dev/null 2>&1 &
        rm -rf `find ./* -maxdepth 0 -type d ! -name "commit"` >/dev/null 2>&1
        $GITHUB_WORKSPACE/.github/diy/openwrt_helloworld.sh
        bash /$GITHUB_WORKSPACE/.github/diy/convert_translation.sh
        bash /$GITHUB_WORKSPACE/.github/diy/create_acl_for_luci.sh -a
        bash /$GITHUB_WORKSPACE/.github/diy/Modify.sh

    - name: Upload
      env:
        GIT_PASSWORD: ${{ secrets.git_password }}
      run: |
        cd $GITHUB_WORKSPACE/${{matrix.target}}
        if git status --porcelain | grep .; then
          Emoji=("🎉" "🤞" "✨" "🎁" "🎈" "🎄" "🎨" "💋" "🍓" "🍕" "🍉" "💐" "🌴" "🚀" "🛸" "🗽" "⛅" "🌈" "🔥" "⛄" "🐶" "🏅" "🦄" "🐤")
          git add .
          git commit -am "${Emoji[$[$RANDOM % ${#Emoji[@]}]]} Sync $(date +%Y-%m-%d" "%H:%M:%S)"
          git push --quiet "https://zhao:${GIT_PASSWORD}@gitea.kejizero.xyz/zhao/openwrt_helloworld.git" HEAD:openwrt-25.12
        else
          echo "nothing to commit"
          exit 0
        fi || exit 0