#!/bin/bash

set -ex

COMMAND_STR="clang-format --style="file" -i"

if [[ $# -eq 0 ]] ; then
    echo "Usage: $0 --all" >&2
    echo "       $0 file1 [...]" >&2
    echo -e "\nRuns \"clang-format --style=file -i file\" against each" >&2
    echo -e "file specified. If \"--all\" is used, searches recursively" >&2
    echo -e "for any file ending with \".cpp\" or \".hpp\".\n" >&2
    exit 1
elif [[ $# -eq 1 ]] ; then
    if [[ "X$1" == "X--all" ]] ; then
        find . -name "*.cpp" -or -name "*.hpp" -print0 | xargs -0 $COMMAND_STR
    else
        $COMMAND_STR "$1"
    fi
else
    $COMMAND_STR "$@"
fi
