#!/usr/bin/env bash
# handy wrapper for 'git commit', prepending package name
# (c) 2019-2024 Michał Górny <mgorny@gentoo.org>
# SPDX-License-Identifier: GPL-2.0-or-later

args=()
msg=
used_bump=

# check for -m ...
while [[ ${#} -gt 0 ]]; do
	case ${1} in
		-m)
			msg=${2}
			shift 2
			continue
			;;
		--bump)
			if [[ ! -f .pkgbump-pv ]]; then
				echo "No .pkgbump-pv (pkgbump not used?)" >&2
				exit 1
			fi
			# make sure it doesn't up being committed
			git rm --cached .pkgbump-pv || :
			used_bump=1
			msg="Bump to $(<.pkgbump-pv)"
			shift
			continue
			;;
	esac

	args+=( "${1}" )
	shift
done

set -e -x

tmpf=$(mktemp)
echo "$(pkg): ${msg}" > "${tmpf}"
[[ ${msg} ]] || ${EDITOR:-vim} "${tmpf}"
if [[ -s ${tmpf} ]]; then
	git commit -F "${tmpf}" "${args[@]}"
fi
rm -f "${tmpf}"
if [[ ${used_bump} ]]; then
	rm .pkgbump-pv
fi
