#!/usr/bin/env bash

# SPDX-FileCopyrightText: 2019 Christoph Cullmann <cullmann@kde.org>
# SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
#
# SPDX-License-Identifier: BSD-3-Clause
# Based on okular/hooks/pre-commit, credits go to Albert Astals Cid

check_if_install() {
  which $1 &>/dev/null
  if [ $? -eq 0 ]; then
    echo yes
  else
    echo no
  fi
}

# check clang-format is installed
have_cf=$(check_if_install clang-format)
[ $have_cf = no ] && echo "ERROR: clang-format not found, please install with: 'sudo apt install clang-format'" && exit 1

readonly output=$(git clang-format --extensions 'cpp,h,hpp,c' -v --diff)

if [[ ! -f .clang-format ]]; then
    if [[ @HAS_CLANG_FORMAT_COMMAND_INCLUDED@ = TRUE ]]; then
        echo "ERROR: no .clang-format file found in repository root, abort format"
        echo "       run cmake for this repository to generate it"
    else
        echo "ERROR: no .clang-format file found in repository root, abort format"
        echo "Make sure the KDEClangFormat CMake module is included, which will copy the KDE .clang-format file during the CMake configuration."
        echo "Alternatively you can manually copy a .clang-format file to the repository root directory."
    fi
    exit 1
fi
if [[ "$output" == *"no modified files to format"* ]]; then exit 0; fi
if [[ "$output" == *"clang-format did not modify any files"* ]]; then exit 0; fi

echo "ERROR: You have unformatted changes, please format with:  [git clang-format --extensions 'cpp,h,hpp,c' --force]"
echo "       git clang-format --extensions 'cpp,h,hpp,c' --force # format the changed parts"
echo "       git clang-format --extensions 'cpp,h,hpp,c' --diff # preview the changes done by the formatter"
exit 1
