#!/bin/sh
# shellcheck disable=SC1091,SC2016

# @file .config/husky/prepare-commit-msg
# @brief A git hook script for the `prepare-commit-msg` hook. Add the `-n` flag to bypass.
# @arg $1 string The full path to the MERGE_MSG
# @arg $2 string The type of the `prepare-commit-msg` event. For a `git pull origin master`
#   command, the event type is 'merge'.

[ -f .config/log ] && chmod +x .config/log
if [ -f "$(dirname "$0")/_/husky.sh" ]; then
  . "$(dirname "$0")/_/husky.sh"
  if [ "$2" != 'merge' ]; then
    .config/log info 'This git hook is configured to run even when --no-verify is used. In order to bypass this prompt, use the -n flag instead.'
    .config/log info 'Opening a `git commit` dialog'
    if ! type pnpx > /dev/null && type npm > /dev/null; then
      npm install -g pnpm
    elif ! type pnpx > /dev/null; then
      .config/log error '`pnpm` or `npm` must be installed'
    fi
    if ! type git-cz &> /dev/null; then
      pnpm install -g commitizen
    fi
    exec < /dev/tty && (git-cz --hook || true)
  fi
else
  .config/log warn 'Husky pre-commit hooks are currently not properly setup.'
fi
