#!/usr/bin/env bash

. "$(git rev-parse --show-toplevel)/gitTools/functions"
cd "${GitTools}"

case "$1" in
  --Windows)
    setGitConfig copasi.astyle "${GitTools}/Windows/AStyle.exe" --path
    setGitConfig copasi.wget "${GitTools}/Windows/wget.exe" --path
    unsetGitConfig copasi.gawk
    unsetGitConfig copasi.dos2unix
    ;;

  --reset)
    git config --remove-section copasi
    git config --remove-section filter."common"
    git config --remove-section user
    ;;

  --tool)
    if [ $# == 3 ]; then
      dirName=`dirname "$3"`

      if [ x"${dirName}" != x. ]; then
        cd "${dirName}" 
        tool="`pwd`/`basename $3`"
        cd "${GitTools}"
      else
        tool="`which $3`"
      fi

      setGitConfig copasi.$2 "${tool}" --path
    fi
    ;;

  --name)
    setGitConfig user.name "$2"
    ;;

  --email)
    setGitConfig user.email "$2"
    ;;
esac

checkGitTool astyle
checkGitTool gawk
checkGitTool wget

if [ ${isWindows} == true ]; then
  checkGitTool dos2unix
  setGitConfig core.autocrlf true 
else
  setGitConfig core.autocrlf input
fi

setGitConfig copasi.gittools "${GitTools}" --path
setGitConfig filter."common".clean "${GitTools}/clean %f"
setGitConfig filter."common".smudge "${GitTools}/smudge %f"

if [ "x`git config --local user.name`" == x ]; then
  read -p "Name [`git config user.name`]: " name
  name="${name:-`git config user.name`}"
  setGitConfig user.name "$name"
fi

if [ "x`git config --local user.email`" == x ]; then
  read -p "Email [`git config user.email`]: " email
  email="${email:-`git config user.email`}"
  setGitConfig user.email "$email"
fi

cp hooks/* ../.git/hooks/
