#!/usr/bin/env bash
set -euo pipefail


##############################################
# Clang-format
##############################################

driving_root=$1
script_dir=$driving_root/scripts/ansible/roles/clang_config/files

echo -------------------------------------------------------
echo Setting up the pre-commit hook...

if [ -d "$driving_root/.git" ]; then
  ln -sf "$script_dir/pre-commit" "$driving_root/.git/hooks/"
else
  # Check if .git is a file. It means driving is installed as a submodule.
  # In that case we try to install pre-commit hook into the supermodule directory.
  if [ -f "$driving_root/.git" ]; then
    dot_git_content=$(<"$driving_root/.git")
    echo dot_git_content
    git_module_path=${dot_git_content#"gitdir: "}
    echo git_module_path
    if [ -d $git_module_path ]; then
      echo Installing clang format to $git_module_path/hooks
      ln -sf "$script_dir/pre-commit" $git_module_path/hooks
    else
      echo Failed to set up pre-commit hook because supermodule hooks directory not found: $git_module_path/hooks
    fi
  fi
fi
