#!/usr/bin/env bash

# Copyright (C) 2014-2019 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2015-2019 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

# This file is managed by Ansible, all changes will be lost

set -e

data=$(cat)
params="${*}"
exitcodes=()
hooktype=$(basename "$0")

GIT_DIR="$(git rev-parse --git-dir)"

if [ -e "${GIT_DIR}/hooks/${hooktype}.d" ] ; then
  for hook in "${GIT_DIR}"/hooks/"${hooktype}".d/* ; do
    test -x "${hook}" || continue
    echo "${data}" | "${hook}" "${params}"
    exitcodes+=($?)
  done

  for i in "${exitcodes[@]}"; do
    [ "$i" == 0 ] || exit "$i"
  done
fi
