#!/usr/bin/env bash
{
format_file() {
  local file="${1}"
  if [ -f $file ]; then
    clang-format -i -style=file ${1}
  fi
}

case "${1}" in
  --about)
    echo "Runs formatters on any files that are going to be checked in."
    ;;
  *)
    # make newlines the only separator
    IFS=$'\n'

    echo "running scripts/scripts/format.sh on all staged files"
    for file in `git diff-index --cached --name-only --diff-filter=ACMRT HEAD | grep -iE '\.(cpp|cc|h|hpp)$'` ; do
      format_file "${file}"
      git add "${file}"
    done
    ;;
esac
}
