#!/bin/sh

# Ensure no tests are broken
scripts/run-tests

# Ensure committed files would have a newline at the end of it
files=$(git diff-index --name-status --cached HEAD | grep -v ^D | cut -c3-)
if [ "$files" != "" ]
then
  for f in $files
  do
    # Add a linebreak to the file if it doesn't have one
    if [ "$(tail -c1 $f)" != '\n' ]
    then
      echo >> $f
      git add $f
    fi
    # Remove trailing whitespace if it exists
    if grep -q "[[:blank:]]$" $f
    then
      sed -i "" -e $'s/[ \t]*$//g' $f
      git add $f
    fi
  done
fi

# Ensure documentations are up-to-date
scripts/update-docs
git add public/*
