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

# @file .config/husky/post-commit
# @brief A git hook script for the `post-commit` hook. There are no parameters but you can easily get the
#   last commit by running `git log -1 HEAD`. Generally, this script is used for notifications or something similar.

# @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"

  # Attempt to register Task from common places if it is not in PATH
  if ! type task > /dev/null; then
    PATH="$PATH:$HOME/.local/go/bin:$HOME/.local/bin:$HOME/bin:$HOME/go/bin:$HOME/.asdf/shims"
    if ! type task > /dev/null; then
      for DOTFILE in .profile .bashrc .bash_profile .zshrc; do
        . "$HOME/$DOTFILE"
        if type task > /dev/null; then
          break
        fi
      done
    fi
  fi

  # Show warning if Task is still not registered/installed, else proceed with hook
  if ! type task > /dev/null; then
    logger warn '`task` does not appear to be installed or is not registered in the `PATH` variable - please manually include it'
    logger info 'Get `task` here -> https://taskfile.dev'
  else
    task git:hook:post-commit
  fi
else
  logger warn 'Husky pre-commit hooks are currently not properly setup.'
fi