#!/bin/bash

RELEASE=$(cd $(dirname $0)/.. && pwd)
PRE_COMMIT=${RELEASE}/.git/hooks/pre-commit
PRE_PUSH=${RELEASE}/.git/hooks/pre-push

if [ ! -f ${PRE_COMMIT} ]; then
  echo "#!/bin/bash" > ${PRE_COMMIT}
  chmod +x ${PRE_COMMIT}
fi

if [ ! -f ${PRE_PUSH} ]; then
  echo "#!/bin/bash" > ${PRE_PUSH}
  chmod +x ${PRE_PUSH}
fi

if grep -q 'git-hooks/pre-commit' ${PRE_COMMIT}; then
  echo "pre-commit already installed."
else
  echo >> ${PRE_COMMIT}
  echo './git-hooks/pre-commit' >> ${PRE_COMMIT}
  echo "pre-commit installed."
fi

if grep -q 'git-hooks/pre-push' ${PRE_PUSH}; then
  echo "pre-push already installed."
else
  echo >> ${PRE_PUSH}
  echo './git-hooks/pre-push' >> ${PRE_PUSH}
  echo "pre-push installed."
fi

CFDOT_HOOKS_DIR=${RELEASE}/.git/modules/src/code.cloudfoundry.org/cfdot/hooks
PRE_COMMIT=${CFDOT_HOOKS_DIR}/pre-commit

if [ ! -d ${CFDOT_HOOKS_DIR} ]; then
    mkdir -p ${CFDOT_HOOKS_DIR}
fi

if [ ! -f ${PRE_COMMIT} ]; then
  echo "#!/bin/bash" > ${PRE_COMMIT}
  chmod +x ${PRE_COMMIT}
fi

if grep -q 'git-hooks/pre-commit' ${PRE_COMMIT}; then
  echo "pre-commit already installed."
else
  echo >> ${PRE_COMMIT}
  echo './git-hooks/pre-commit' >> ${PRE_COMMIT}
  echo "pre-commit installed."
fi
