#!/bin/bash

. ../init.sh || exit 1

testit() {
	if [ -s $1 ] ; then
		tfail "$1"
		cat $1
		rm -f $1
		return
	fi
	rm -f $1
	tpass "$1"
}

prune=(
	'('
		'('
		'('
			-type d -a
			'('
				-name tests -o
				-name autotools -o
				-name autom4te.cache -o
				-name .deps
			')'
		')'
		-o
		'('
			-type f -a
			'('
				-name '*~' -o
				-name config.h -o
				-name config.log -o
				-name config.status
			')'
		')'
		')'
		-prune
	')'
)
all_files=(
	"${prune[@]}" -o -type f
)
src_files=(
	"${prune[@]}" -o -type f -name '*.[ch]'
)



#
# check for misc common typos
#
find ${ats} "${all_files[@]}" -print0 | xargs -0 \
	grep -n -I \
		-e '\<compatability\>' \
		-e '\<compatable\>' \
		-e '\<fordeground\>' \
		-e '\<depency\>' \
		-e '\<defalt\>' \
		-e '\<remaing\>' \
		-e '\<queuing\>' \
		-e '\<detatch\>' \
		-e '\<sempahore\>' \
		-e '\<reprenstative\>' \
		-e '\<overriden\>' \
		-e '\<readed\>' \
		-e '\<formated\>' \
		-e '\<algorithic\>' \
		-e '\<deamon\>' \
		-e '\<derefernce\>' \
		-e '\<lenght\>' \
		| sed -e "s:^\.\./\.\./::g" > src.typos
testit src.typos



#
# don't allow obsolete functions
#
find ${ats} "${src_files[@]}" -print0 | xargs -0 \
	grep -n -E -e '\<(bcmp|bcopy|bzero|getwd|index|mktemp|rindex|utime)\>[[:space:]]*\(' \
	| sed -e "s:^\.\./\.\./::g" > src.obsolete.funcs
testit src.obsolete.funcs



#
# make sure people use our constants
#
find ${ats} "${src_files[@]}" -print0 | xargs -0 \
	grep -n -E -e '\<PATH_MAX\>' | grep -v _Q_PATH_MAX \
	| sed -e "s:^\.\./\.\./::g" > src.bad.constants
testit src.bad.constants



#
# don't allow obsolete headers
#
find ${ats} "${src_files[@]}" -print0 | xargs -0 \
	grep -n -E -e '\<(malloc|memory|sys/(errno|fcntl|signal|stropts|termios|unistd))\.h\>' \
	| sed -e "s:^\.\./\.\./::g" > src.obsolete.headers
testit src.obsolete.headers



#
# make sure people use the x* helper funcs
#
find ${ats} "${src_files[@]}" -print0 | xargs -0 \
	grep -n -E -e '\<(malloc|strdup)[[:space:]]*\(' \
	| grep -v libq/x \
	| sed -e "s:^\.\./\.\./::g" > src.use.xfuncs
testit src.use.xfuncs


#
# check for style
#
find ${ats} "${src_files[@]}" -print0 | xargs -0 \
	grep -n -E \
		-e '\<(for|if|switch|while)\(' \
		-e '\<(for|if|switch|while) \( ' \
		-e ' ;' \
		-e '[[:space:]]$' \
		-e '\){' \
		-e '(^|[^:])//' \
	| sed -e "s:^\.\./\.\./::g" > src.style
testit src.style


#
# Auto clean up the space issues
#
for x in $(find ${ats} "${src_files[@]}" -print); do
	# skip paths we don't have write access to
	touch "$x~" 2>/dev/null || continue
	${s}/space "$x" > "$x~"
	if ! diff -u "$x" "$x~" ; then
		echo "New file: $x~"
	else
		rm -f "$x~"
	fi
done > src.space
testit src.space

end
