1#!/bin/sh
2
3if [ $(basename "$0") != "install" ]; then
4  if [ -x "$0.local" ]; then
5    "$0.local" "$@" || exit $?
6  fi
7  if [ -x hooks/$(basename $0) ]; then
8    hooks/$(basename $0) "$0" || exit $?
9  fi
10else
11  pushd "$(git rev-parse --show-toplevel)"
12  python <<\EOF
13import os, os.path
14TOP = os.path.realpath(".")
15HOOKS = os.path.realpath(".git/hooks")
16src = os.path.join(TOP, "hooks", "install")
17for hook in os.listdir("hooks"):
18  if hook != "install":
19    tgt = HOOKS + os.sep + hook
20    # there is a file there
21    if os.path.isfile(tgt) and os.access(tgt, os.X_OK):
22      if os.path.realpath(tgt) != src:
23        print("hook " + hook + " is already installed. Moving to " + hook + ".local")
24        os.rename(tgt, tgt + ".local")
25    if os.path.lexists(tgt):
26      os.unlink(tgt)
27    os.symlink(os.path.relpath(os.path.realpath("hooks/install"), os.path.realpath(".git/hooks/")), tgt)
28EOF
29  popd
30fi
31