
name: Update packages

on:
  push:
    paths:
      - '.github/workflows/kenzo.yml'
      - 'diy/**'
  schedule:
    - cron: 0 */12 * * *
  watch:
    types: started
  repository_dispatch:

env:
  TZ: Asia/Shanghai

jobs:
  build:
    if: github.event.repository.owner.id == github.event.sender.id
    runs-on: Ubuntu-20.04

    name: Update ${{matrix.target}}
    strategy:
      fail-fast: false
      matrix:
        target: [main]
        
    steps:
    - name: Checkout
      uses: actions/checkout@main

    - name: Initialization environment
      env:
        DEBIAN_FRONTEND: noninteractive
      run: |
        sudo -E apt-get -qq update
        sudo -E apt-get -qq install git subversion git-core wget
        sudo timedatectl set-timezone "$TZ"
        git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
        git config --global user.name "github-actions[bot]"
        
    - name: Clone packages
      run: |
        cd $GITHUB_WORKSPACE
        chmod +x ${{matrix.target}}.sh
        git clone -b ${{matrix.target}} https://github.com/kenzok8/small-package.git ${{matrix.target}}
        cd ${{matrix.target}}
        git rm -r --cache * >/dev/null 2>&1 &
        rm -rf `find ./* -maxdepth 0 -type d ! -name "diy"` >/dev/null 2>&1
        $GITHUB_WORKSPACE/${{matrix.target}}.sh
        $GITHUB_WORKSPACE/diy/Modify.sh
        
    - name: Upload
      env: 
        ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
      run: |
        [ -e $GITHUB_WORKSPACE/LICENSE ] && cp $GITHUB_WORKSPACE/LICENSE $GITHUB_WORKSPACE/${{matrix.target}}
        [ -e $GITHUB_WORKSPACE/README.md ] && cp $GITHUB_WORKSPACE/README.md $GITHUB_WORKSPACE/${{matrix.target}}
        cd $GITHUB_WORKSPACE/${{matrix.target}}
        git add .
        git commit -m "update$(TZ='Asia/Shanghai' date +-%m.%d)"
        git push --quiet "https://${{ secrets.ACCESS_TOKEN }}@github.com/kenzok8/small-package.git" HEAD:${{matrix.target}}
        
        
    - name: Delete workflow runs
      uses: GitRML/delete-workflow-runs@main
      with:
        retain_days: 1
        keep_minimum_runs: 0

    - name: Remove old Releases
      uses: dev-drprasad/delete-older-releases@v0.1.0
      if: env.UPLOAD_RELEASE == 'true' && !cancelled()
      with:
        keep_latest: 1
        delete_tags: true
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
