#!/usr/bin/env sh
#
# mkchangelog - generate a changelog from Gits shortlog

set -eu

usage() {
  printf %s "\
$(basename "$0")
  a simple script to generate a changelog from Git's shortlog, excl. tests

$(basename "$0") [options] [length]

options:
  -h, --help    - Display this help message.
  -s, --short   - Exclude chore/doc/test semantic commits.
"
}

# when the user passess bad args, send a msg to stderr and exit
# usage: bad_option <option> <reason>
bad_option() {
  echo "$(basename "$0"): option $1: $2" >&2
  usage >&2
  exit 1
}

short=0
# options
while [ "$#" -gt 0 ]; do
  case "$1" in
    -h | --help)
      usage
      exit 0
      ;;
    -s | --short)
      short=1
      ;;
    *)
      bad_option "$1" 'invalid option'
      ;;
  esac
  shift
done

last_tag="$(git describe --abbrev=0 --tags)"

remove_extra() {
  anything_but_a_closing_paren='[^(\))]*'
  stuff_in_parens="\(${anything_but_a_closing_paren}"'\)'
  grep -Evi "^\s*(chore|doc|test)(${stuff_in_parens})?:"
}

full_shortlog() {
  git shortlog --oneline "${last_tag}"..HEAD
}

if [ "${short}" = 1 ]; then
  full_shortlog | remove_extra
else
  full_shortlog
fi
