#!/bin/bash

get_ticket_number() {
	git symbolic-ref --short HEAD | grep -Ei '^ocst-[0-9]{2,5}-' | sed -E 's/^(ocst-[0-9]{2,5}).*$/\1/i' | tr '[:upper:]' '[:lower:]'
}

format_commit_message() {
	local message="$1"
	local ticket="$2"

	if echo "$message" | grep -E '^[^:]+:' >/dev/null; then
		local type=$(echo "$message" | cut -d: -f1)
		local content=$(echo "$message" | cut -d: -f2- | sed 's/^ //')
		echo "$type($ticket): $content"
	else
		echo "$message"
	fi
}

main() {
	local ticket=$(get_ticket_number)
	if [ -n "$ticket" ]; then
		git commit -m "$(format_commit_message "$1" "$ticket")" "${@:2}"
	else
		git commit -m "$1" "${@:2}"
	fi
}

main "$@"
