#!/usr/bin/env bash

if [ -z $1 ]; then
  echo 'usage jirahook --add or jirahook --remove'
  exit 1
fi

if [ ! -d .git/hooks ]; then
    echo 'No .git/hooks directory found. Please run this from the root of your project'
    exit 1
fi

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
  -r | --remove )
    if [ ! -f .git/hooks/prepare-commit-msg ]; then
            echo 'Git hook has aready been removed.'
            exit 0
        else
            echo 'Deleting hook'
            rm .git/hooks/prepare-commit-msg
        fi
    ;;
  -a | --add )
    if [ ! -f .git/hooks/prepare-commit-msg ]; then
        echo 'Installing automatic JIRA ticket number hook.'
        cp vendor/roland/code-quality/prepare-commit-msg .git/hooks/prepare-commit-msg
        chmod +x .git/hooks/prepare-commit-msg
    else
        echo 'Hook is already installed'
    fi
    exit 0
    ;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
