Fix install_hooks.sh

I accidentally swapped the arguments around for a git command. Also,
this script needs to clean up the git config and the .git/hooks dir
before it adds new hooks. Otherwise git will run the same command
multiple times in parallel.

Bug: b/187094215
Change-Id: I70ca3bd3ba381e581b176bc9794a26d89937f00f
diff --git a/install_hooks.sh b/install_hooks.sh
index 90c3480..822a324 100755
--- a/install_hooks.sh
+++ b/install_hooks.sh
@@ -1,7 +1,7 @@
 #! /usr/bin/env sh
 
-#git config --add hook.pre-commit.command `realpath 
 dir=`realpath $(dirname "$BASH_SOURCE:-0")`
+githookdir=`git rev-parse --git-dir`/hooks
 
 # Space separated list of hooks to install
 declare -a HOOKS=("pre-commit" "commit-msg")
@@ -10,6 +10,16 @@
     if [ ! -f "${dir}/${hook}" ]; then
         continue
     fi
+
+    # Soft remove the old script in .git/hooks so that git doesn't try to run
+    # multiple hooks.
+    if [ -f "${githookdir}/${hook}" ]; then
+        mv "${githookdir}/${hook}" "${githookdir}/${hook}.old"
+    fi
+
+    git config --unset-all hook.${hook}.command
     git config --add hook.${hook}.command "${dir}/${hook}"
-    git list hook "${hook}"
+    # Sanity check: there should be a single hook per line and it should point to the
+    # git-hooks repository.
+    git hook list "${hook}"
 done