#!/bin/bash

ret=0

testit() {
	if [ -s $1 ] ; then
		echo FAIL: $1
		cat $1
		rm -f $1
		ret=1
	fi
	rm -f $1
	echo PASS: $1
}


#
# check for misc common typos
#
find ../.. \
	'(' -type d -a '(' -name CVS -o -name tests ')' -prune ')' \
	-o '(' -type f -a -print0 ')' | xargs -0 \
	grep -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\>' \
		| sed -e "s:^\.\./\.\./::g" > src.typos
testit src.typos



#
# don't allow obsolete functions
#
find ../.. '(' -name '*.c' -o -name '*.h' ')' -print0 | xargs -0 \
	grep -E -e '\<(bcmp|bcopy|bzero|getwd|index|mktemp|rindex|utimes)\>[[:space:]]*\(' \
	| sed -e "s:^\.\./\.\./::g" > src.obsolete.funcs
testit src.obsolete.funcs



#
# make sure people use our constants
#
find ../.. '(' -name '*.c' -o -name '*.h' ')' -print0 | xargs -0 \
	grep -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 ../.. '(' -name '*.c' -o -name '*.h' ')' -print0 | xargs -0 \
	grep -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 ../.. '(' -name '*.c' -o -name '*.h' ')' -print0 | xargs -0 \
	grep -E -e '\<(malloc|strdup)[[:space:]]*\(' \
	| grep -v libq/x \
	| sed -e "s:^\.\./\.\./::g" > src.use.xfuncs
testit src.use.xfuncs


#
# check for style
#
find ../.. '(' -name '*.c' -o -name '*.h' ')' -print0 | xargs -0 \
	grep -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



exit ${ret}
