#! /bin/sh
diff_files=`git diff --cached --name-only | grep -E '\.cxx$|\.hxx$|\.h$' `
if [ "$(uname)" == "Darwin" ]; then
  clangFile=.githooks/clangFormatMac
  if [ -f "$clangFile" ]; then
    echo "clangFormat already downloaded."
  else
    curl -o .githooks/clangFormatMac https://data.kitware.com/api/v1/file/5d274e88877dfcc902effc47/download
    chmod u+x .githooks/clangFormatMac
  fi
  for file in $diff_files; do
    .githooks/clangFormatMac -style=file -i --verbose $file;
  done
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]; then
  clangFile=.githooks/clangFormatLinux
  if [ -f "$clangFile" ]; then
    echo "clangFormat already downloaded."
  else
    curl -o .githooks/clangFormatLinux https://data.kitware.com/api/v1/file/5d2b8c87877dfcc902fda594/download
    chmod u+x .githooks/clangFormatLinux
  fi
  for file in $diff_files; do
    .githooks/clangFormatLinux -style=file -i --verbose $file;
  done
else
  clangFile=.githooks/clangFormatWindows.exe
  if [ -f "$clangFile" ]; then
    echo "clangFormat already downloaded."
  else
    curl -o .githooks/clangFormatWindows.exe https://data.kitware.com/api/v1/file/5d2b8775877dfcc902fd8236/download
  fi
  for file in $diff_files; do
    .githooks/clangFormatWindows.exe -style=file -i --verbose $file;
  done
fi
git status
