# !/bin/sh
# To enable this hook, name this file "pre-commit".
# Remember to make it executable by calling the following in the terminal: chmod +x filename

julia -e '
    using Pkg
    Pkg.activate("./docs")
    using JuliaFormatter
    format(".")
    out = Cmd(`git diff --name-only`) |> read |> String
    if out == ""
        exit(0)
    else
        @error "Some files have been formatted !!!"
        write(stdout, out)
        exit(1)
    end'

#Setup
reponame=$(basename `git rev-parse --show-toplevel`)

#Intro text
echo testing_failed " \n -- Running pre-commit hook -- \n"
echo  " \n -- Testing $reponame -- \n"

#Run quick tests
julia --project=. test/quicktests.jl

#If tests failed
if [[ $? -ne 0 ]]; then
        #Raise an error
        echo  "\n -- ERROR: Testing failed, commit halted -- \n"
        #Stop the commit
        exit 1
#Otherwise
else
        #Inform that all tests passed
        echo  "\n -- All tests passed, commit completed -- \n"
fi