#!/bin/bash
#
# This script is used by the `make indent` command, and expects to be called
# with a list of files in the current directory that are NOT to be reindented.
# It reindents all files in the current directory not in that list that match
# the *.c pattern.

export allow_null_glob_expansion=Y

function reformat() {
    indent "${FILE}" --ignore-profile				\
		--braces-on-if-line				\
		--braces-on-struct-decl-line			\
		--cuddle-else					\
		--dont-break-procedure-type			\
		--else-endif-column24				\
		--indent-level4 				\
		--line-length79 				\
		--no-space-after-function-call-names		\
		--no-space-after-parentheses			\
		--space-after-cast				\
		--start-left-side-of-comments			\
		--tab-size8
}

function reindent() {
    printf 'Reindenting %s ...\n' "$1" >&2
    if fgrep -q '#asm' "$1" ; then
	printf "Can't reindent %s as it contains an '#asm' statement.\n" "$1" >&2
	sleep 1
    else
	reformat "$1"
    fi
}

OK=`printf '%s\n' "$@" | grep '^~' | cut -b 2-`

if [ -n "${OK}" ]; then
    for FILE in $OK ; do
	if [ -f "${FILE}" ]; then
	    reindent "${FILE}"
	fi
    done
else
    for FILE in *.[ch] ; do
	if [ -f "${FILE}" ]; then
	    if ! printf '%s\n' "$@" | grep -q ^${FILE}'$' ; then
		reindent "${FILE}"
	    fi
	fi
    done
fi
