#!/usr/bin/env bash
set -x
failed () { 
    echo "Usage:~ $(basename $0) commit-message" >&2
    echo "$@" >&2 
    exit 1
}

COMMIT_MESSAGE="$@"
[[ -z $COMMIT_MESSAGE ]] && failed ""


chmod go-rxw $PRIVATE_KEY_FILE
export GIT_SSH_COMMAND="ssh -i $PRIVATE_KEY_FILE -o IdentitiesOnly=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"

if git remote get-url update >/dev/null 2>&1; then
    git remote remove update
fi

git remote add update git@$CI_SERVER_HOST:$CI_PROJECT_PATH.git
git fetch update
git switch -c $CI_COMMIT_REF_NAME --guess update/$CI_COMMIT_REF_NAME

if [[ -n $(git status --short 2>/dev/null) ]]; then
	git add .
	git commit -am "${COMMIT_MESSAGE}"
	if ! git push; then
		git pull --rebase 
		git push
	fi
else
	echo "INFO: no changes to commit" >&2
	exit 0
fi
