#!/usr/bin/env bash

set -x 
set -e

[ -n "`git status --porcelain`" ] || exit 0

git config user.name || git config user.name "robot"
git config user.email || git config user.email "$GITHUB_ACTOR-the-robot@users.noreply.github.com"

[ -f "tmp/.commit_msg" ]\
    && COMMIT_MSG=$(printf "%s" "$(< tmp/.commit_msg)")\
    || COMMIT_MSG="Patched"

[ -n "$GITHUB_RUN_ID" ]\
    && COMMIT_MSG=$(printf "%s\n\n%s" "$COMMIT_MSG" "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID")\
    || :


git add -A patched
git commit  -m "$COMMIT_MSG"
git pull -r
git push