#!/bin/sh
# a script to clean up RPM %__macro abuse 
# (those are actually rpm's _internal_ macros, 
# not intended for direct usage in specs and
# in fact cluttering them up without any bonus)
# 2006 by Michael Shigorin <mike@altlinux.org>
# use freely

fatal()
{
	echo "$0: $*" >&2
	exit 1
}

[ -s "$1" ] || fatal "gimme a spec!"

grep -q '^[^#].*%__' "$1" || exit 0

TMPSPEC=`mktemp $1.XXXXXX` || fatal "unable to create temporary file"

sed \
	-e 's@%__awk@gawk@g' \
	-e 's@%__bzip2@bzip2@g' \
	-e 's@%__cat@cat@g' \
	-e 's@%__chgrp@chgrp@g' \
	-e 's@%__chmod@chmod@g' \
	-e 's@%__chown@chown@g' \
	-e 's@%__cp@cp@g' \
	-e 's@%__cpio@cpio@g' \
	-e 's@%__file@file@g' \
	-e 's@%__gpg@gpg@g' \
	-e 's@%__grep@grep@g' \
	-e 's@%__gzip@gzip@g' \
	-e 's@%__id@id@g' \
	-e 's@%__install@install@g' \
	-e 's@%__install_info@install-info@g' \
	-e 's@%__ln_s@ln -s@g' \
	-e 's@%__make@make@g' \
	-e 's@%__mkdir@mkdir@g' \
	-e 's@%__mkdir_p@mkdir -p@g' \
	-e 's@%__mv@mv@g' \
	-e 's@%__patch@patch@g' \
	-e 's@%__perl@perl@g' \
	-e 's@%__pgp@pgp@g' \
	-e 's@%__python@python@g' \
	-e 's@%__rm@rm@g' \
	-e 's@%__rsh@rsh@g' \
	-e 's@%__sed@sed@g' \
	-e 's@%__ssh@ssh@g' \
	-e 's@%__subst@subst@g' \
	-e 's@%__tar@tar@g' \
	-e 's@%__unzip@unzip@g' \
	-e 's@%__ar@ar@g' \
	-e 's@%__as@as@g' \
	-e 's@%__cc@gcc@g' \
	-e 's@%__cpp@gcc -E@g' \
	-e 's@%__cxx@g++@g' \
	-e 's@%__ld@ld@g' \
	-e 's@%__nm@nm@g' \
	-e 's@%__objcopy@objcopy@g' \
	-e 's@%__objdump@objdump@g' \
	-e 's@%__ranlib@ranlib@g' \
	-e 's@%__strip@strip@g' \
	-e 's@%__libtoolize@libtoolize@g' \
	-e 's@%__aclocal@aclocal@g' \
	-e 's@%__autoheader@autoheader@g' \
	-e 's@%__automake@automake@g' \
	-e 's@%__autoconf@autoconf@g' \
	< "$1" > "$TMPSPEC" \
&& add_changelog -e '- spec macro abuse cleanup' "$TMPSPEC"
echo "please inspect $TMPSPEC for cleaned up version" 
