#!/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'.

# @description Register appropriate logging utility
if [ -f "$(dirname "$0")/../.config/log" ]; then
  alias logger="$(dirname "$0")/../.config/log"
  chmod +x "$(dirname "$0")/../.config/log"
elif command -v logg > /dev/null; then
  alias logger='logg'
fi

if [ -f "$(dirname "$0")/_/husky.sh" ]; then
  . "$(dirname "$0")/_/husky.sh"
  if [ "$2" != 'merge' ]; then
   logger 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.'
    logger 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
      logger 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
  logger warn 'Husky pre-commit hooks are currently not properly setup.'
fi