#!/bin/bash

FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .tcc"

# Determins if a file has the right extension to be clang-format'ed.
should_clang_format() {
    local filename=$(basename "$1")
    local extension=".${filename##*.}"
    local ext

    local result=0

    # Ignore the test/catch*.hpp file
    if [[ "$1" != *"catch"* ]]; then
        for ext in $FILE_EXTS; do
            # Otherwise, if the extension is in the array of extensions to reformat, echo 1.
            [[ "$ext" == "$extension" ]] && result=1 && break
        done
    fi

    echo $result
}

# Run the clang-format across the project's changed files.
for file in $(git diff-index --cached --name-only HEAD); do
    if [ -f "${file}" ] && [ "$(should_clang_format "${file}")" != "0" ] ; then
        echo "clang-format ${file}"
        clang-format -i --style=file "${file}"
        git add "${file}"
    fi
done

# Update the README.md example code with the given macros.
# template_contents=$(cat '.githooks/readme-template.md')
cp .githooks/readme-template.md README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_task.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_TASK_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_generator.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_GENERATOR_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_event.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_EVENT_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_latch.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_LATCH_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_mutex.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_MUTEX_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_thread_pool.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_THREAD_POOL_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_io_scheduler.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_IO_SCHEDULER_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_task_container.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_TASK_CONTAINER_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_semaphore.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_SEMAPHORE_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_ring_buffer.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_RING_BUFFER_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_shared_mutex.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_SHARED_MUTEX_CPP\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_sync_wait.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_SYNC_WAIT\}/$example_contents}" > README.md

template_contents=$(cat 'README.md')
example_contents=$(cat 'examples/coro_when_all.cpp')
echo "${template_contents/\$\{EXAMPLE_CORO_WHEN_ALL\}/$example_contents}" > README.md

git add README.md
