#!/bin/bash

function usage () {
  myName="$(basename "$0")"
  echo "Current working directory = $(pwd)

${myName} - Use astyle to style source code.

Usage:
  ${myName} - No options, style all .h, .hpp, .c, and .cpp files in current working directory
  ${myName} [-h | --help | help | usage] - Show this help list
  ${myName} [file 1 [file 2 [...]]] - File list, style all files in the given file listing
"
  exit 0;
}

while getopts h opt; do
  case "$opt" in
  h) usage
    ;;
  esac
done

if [ "$1" = --help ] || [ "$1" = help ] || [ "$1" = usage ];then
  usage
fi

optFile="$(dirname $(readlink -f "$0"))/libmacro.astylerc"
if [ $# -gt 0 ]; then
  astyle --options="$optFile" "$@"
else
  find . -regex '.*\.\(h\|c\|hpp\|hxx\|cpp\|cc\|tpp\)$' \
    -exec astyle --options="$optFile" {} \;
fi
