#!/bin/sh
# Handy script to symlink version controlled hooks (from git_hooks/) to local hooks (.git/hooks/)

HOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks
for hook in "$(git rev-parse --show-toplevel)"/git_hooks/*; do
  # If the hook already exists, is executable, and is not a symlink
  if [ ! -h "$HOOK_DIR/$hook" ] && [ -x "$HOOK_DIR/$hook" ]; then
    echo "$hook already exists, renaming to $hook.local"
    mv "$HOOK_DIR/$hook" "$HOOK_DIR/$hook.local"
  fi
  # create the symlink, overwriting the file if it exists
  # probably the only way this would happen is if using an old version of git,
  # -- back when the sample hooks were not executable, instead of being named ____.sample
  ln -s -f "$hook" "$HOOK_DIR/"
  echo "created symlink $hook"
done
echo "done"