#!/usr/bin/env bash

set -e -o pipefail

coauthor=$1
email=$2

test -z "$coauthor" && echo "co-author required." 1>&2 && exit 1
test -z "$email" && echo "co-author email required." 1>&2 && exit 1

old_message="$(git log --format=%B -n1)"
if [[ "$old_message" == *"Co-authored-by:"* ]]; then
  git commit --amend -m "$old_message
Co-authored-by: $coauthor <$email>"
else
  git commit --amend -m "$old_message" -m "Co-authored-by: $coauthor <$email>"
fi
