# -*- mode: sh; sh-shell: bash -*-
# vim: set ft=bash:
# shellcheck shell=bash

### Support library for managing githooks

require git_lib

function githook_enable
{
     local hook
     # shellcheck disable=SC2155
     local bar="$(git_bar)"
     bar="${bar##*/}"
     for hook in "$@"; do
         info "enabling hook: $hook"
         ln -s "../../$bar" "$(git_dir)/hooks/$hook" 2>/dev/null || {
             if [[ "$(readlink -f "$(git_dir)/hooks/$hook")" = "$(git_bar)" ]]; then
                 info "hook $hook already enabled"
             else
                 error "installing hook $hook failed"
                 return 1
             fi
          }
     done
}

## Takes a list of githooks to enable as parameters. Must be called from the git_toplevel.
## Fails when a hook is not available.
rule githook_enable: is_git_toplevel -

function githook_disable
{
    local hook
     for hook in "$@"; do
         info "disabling hook $hook"

         if [[ "$(readlink -f "$(git_dir)/hooks/$hook")" = "$(git_bar)" ]]; then
             rm "$(git_dir)/hooks/$hook"
             debug "removed hook $hook"
         else
             error "$hook: not a hook under bar control"
             return 1
         fi
     done
}

## Takes a list of githooks to disable as parameters. Must be called from the git_toplevel.
## Fails when a hook is not pointing to bar.
rule githook_disable: is_git_toplevel -
