#!/usr/bin/env bash
set -eo pipefail

# exit on existing commit message
[[ -z "$2" ]] || exit 0

# remove last line "$n files changed [...]"
diff=$(git diff --staged --stat | grep -v '.*.expected.out' | head -n -1)
# take file with most changes
file=$(echo "$diff" | sort -n -k 3 | tail -n 1)
# extract filename
file=$(echo "$file" | cut -f 2 -d' ')
file="${file#'src/'}"
file="${file%.*}"
case $file in
    tests/*)
        type=test
        ;;
    script/*)
        type=chore
        ;;
    *)
        type=feat
        ;;
esac
orig=$(cat "$1")
echo "$type($file):" > $1
echo "$orig" >> $1
