#!/bin/sh

# Cash output coloring.
LRED='\033[1;31m'
LYELLOW='\033[00;33m'
NC='\033[0m'

GIT_DIR=$(git rev-parse --show-toplevel)/.git/hooks

# Check if the git hooks folders exists.
if test -z "$GIT_DIR"
then
  echo ${LRED}No `.git/` folder found. Initialize with `git init` or check that it was cloned as intended.${NC}
  exit 0
fi

# Determine bin directory using composer ^2.2.2 global.
if [[ -z "$COMPOSER_RUNTIME_BIN_DIR" ]]; then
  HOOK_FILE_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/pre-commit"
else
  HOOK_FILE_PATH="$COMPOSER_RUNTIME_BIN_DIR/../26b/php-pre-commit/pre-commit"
fi

# Check if hook file exists before copying.
if test -z "$HOOK_FILE_PATH"
then
  echo "${LRED}No hook file found. There could be something wrong with how the package was installed, reinstall and retry.${NC}"
  echo "${LYELLOW}If the problem persists open an issue on the development repository (https://github.com/26B/php-pre-commit).${NC}"
  exit 0
fi

# Add pre-commit hook to the hooks directory.
cp $HOOK_FILE_PATH "${GIT_DIR}/pre-commit"
chmod +x "${GIT_DIR}/pre-commit"
