#!/bin/sh
# $Id: setup-clean,v 1.10 2024/05/04 10:46:10 tom Exp $

failed() {
	echo "?? $*" >&2
	exit 1
}

purge() {
	find . -type f -name "$@" -exec rm -f {} \; -print
}

if [ $# != 0 ]
then
	MKFILE=$1
	shift 1
else
	MKFILE=Makefile
	test -f makefile.in && MKFILE=makefile
	test -f makefile    && MKFILE=makefile
fi

while [ $# != 0 ]
do
	test -f $1 || failed "missing $1"
	shift 1
done

if [ -f "$MKFILE" ]
then
	if "${MAKE-make}" distclean 2>/dev/null
	then
		:
	elif [ -f "$MKFILE" ]
	then
		"${MAKE-make}" clean
		rm -f "$MKFILE"
	fi
	rm -f tags TAGS
	rm -f *.out mk_shared_lib.sh
	purge 'coverage.info'
	purge '*.plist'
	purge '*.gcda'
	purge '*.gcno'
	purge '*.o'
	purge core
	purge '*.core'
	purge 'valgrind*'
fi
