name: Generate Supporting Files

on:
  push:
    branches:
      - main

jobs:
  generate-supporting-files:
    runs-on: ubuntu-latest

    env:
      GIT_STRATEGY: clone

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.10'

      - name: Install dependencies
        run: |
          sudo apt-get update && sudo apt-get install -y imagemagick
          pip install --upgrade pip

      - name: Prepare icon directories
        run: |
          git checkout main
          git status
          rm -rf icons-small
          rm -rf icons-medium
          mkdir -p icons-small
          mkdir -p icons-medium

      - name: Resize icons
        run: |
          for icon in icons/*.png; do
            small_icon="icons-small/$(basename "$icon")"
            medium_icon="icons-medium/$(basename "$icon")"
            convert "$icon" -resize 16x16 "$small_icon"
            convert "$icon" -resize 50x50 "$medium_icon"
          done

      - name: Generate README
        run: python scripts/generate_readme.py

      - name: Configure Git
        run: |
          git remote set-url origin https://${{ secrets.CI_USER }}:${{ secrets.CI_TOKEN }}@git.ms-ds.org/msds/icons.git
          git add *
          git config --global user.email "ci@git.ms-ds.org"
          git config --global user.name "Gitea Actions"
          git commit -m "Auto-updated small icons + README"
          git push origin HEAD:main
        env:
          CI_USER: ${{ secrets.CI_USER }}
          CI_TOKEN: ${{ secrets.CI_TOKEN }}
