# clone:
#   git:
#     image: woodpeckerci/plugin-git
#     settings:
#       SSH_PRIVATE_KEY:
#         from_secret: SSH-KEY
#       use-ssh: true
steps:
  - name: age-check
    image: ubuntu:latest
    commands:
      - cd $CI_WORKSPACE
      - ls -al
      - /bin/sh 'update-age.sh' infos_persos.tex && echo "Age check succeeded" || echo "Age check failed"
    when:
      - branch: main
        event: [push, pull_request, manual, cron]

  - name: push commit
    image: alpine/git
    commands:
      - apk add --update openssh-client bash
      - eval $(ssh-agent -s)
      - export GIT_SSH_COMMAND="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
      - git remote set-url origin "${CI_REPO_CLONE_SSH_URL}"
      - bash -c 'ssh-add <(echo "$SSH_PRIVATE_KEY")'
      - git config --global user.email "${author_email:-git@polarolouis.fr}"
      - git config --global user.name "${author_name:-Bot}"
      - |
        # Check if we have modifications to commit
        CHANGES=$(git status --porcelain | wc -l)

        if [ "$CHANGES" -gt "0" ]; then
          git add -f infos_persos.tex
          git status
          git commit -m "Update age in infos_persos.tex"
          git push origin "${CI_REPO_DEFAULT_BRANCH}"
        fi
    environment:
      # GIT_SSH_COMMAND: "ssh -i ~/.ssh/id_ed25519 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
      author_name: "Woodpecker CI"
      author_email: "git@polarolouis.fr"
      SSH_PRIVATE_KEY:
        from_secret: SSH-KEY
    when:
      - branch: "main"
        event: [push, pull_request, manual, cron]
