#!/bin/bash
#
# specwash.sh - wash unneeded curlies from scripts and spec-files
#               use macros in favor of anything else
#

if [ $# -lt 1 ]; then
	echo "Usage: ${0##*/} scripts/spec-files"
	exit 1
fi

# varchars in regex form
# using this whitelist allows things like ${0##*/} to pass unmolested
# it might break some other things though, so we keep the backups.
vc="a-zA-Z0-9_"
#		-e "s/^\([^#].*[^_]\)mkdir/\1%__mkdir/" \

for i in "$@"; do
	mv -i $i $i.bak || continue
	sed -e "s/ *$//g" \
		-e "s/\([\%\$]\){\([$vc]*\)\}\([^$vc]\)/\1\2\3/g" \
		-e "s/\([\%\$]\){\([$vc]*\)\}$/\1\2/g" \
		-e 's,/usr/bin/,%_bindir/,g' -e 's,/usr/sbin/,%_sbindir/,g' \
		-e 's,/usr/libexec/,%_libexecdir/,g' -e 's,/usr/lib/,%_libdir/,g' \
		-e 's,/usr/include/,%_includedir/,g' \
		-e 's,/usr/info/,%_infodir/,g' -e 's,/usr/man/,%_mandir/,g' \
		-e 's,/usr/share/info/,%_infodir/,g' -e 's,/usr/share/man/,%_mandir/,g' \
		-e 's,/usr/,%_prefix/,g' \
		-e 's,\$RPM_BUILD_ROOT,%buildroot,g' \
		-e 's,\$RPM_OPT_FLAGS,%optflags,g' \
	$i.bak > $i
done
