on:
  schedule:
    - cron:  '00 00 * * *'
  workflow_dispatch:

name: Create new artwork

jobs:
  update:
    name: Create new artwork
    runs-on: macos-latest
    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
    steps:
      
      # Get current date
      - name: Get current date
        id: date
        run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
      
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v4
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
          fetch-depth: 0 # otherwise, you will failed to push refs to dest repo
      - uses: r-lib/actions/setup-r@v2

      # Install required packages and update README
      - name: Install required packages
        run: |
          install.packages("remotes")
          remotes::install_github("koenderks/aRtsy")
        shell: Rscript {0}

      - name: Create new artwork
        run: |
          source(".github/workflows/generate_artwork.R")
        shell: Rscript {0}

      - name: Commit files
        run: |
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git commit -m "Daily artwork of ${{ steps.date.outputs.date }}" -a
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}