#!/bin/sh
# 2005-2007 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# $Id: rpmcs,v 1.43 2007/12/07 07:26:07 lav Exp $
#
# CS -- cleanup spec (like cleanup_spec command)
# :
# -  -
. /etc/rpm/etersoft-build-functions

dummy_spec()
{
grep "^Name" $1
grep "^Version" $1
grep "^Release" $1
grep "^Packager" $1
cat <<EOF
Summary: dummy
License: dummy
Group: dummy
%description
EOF
}

# Get replacement rule for some package in $1 to ALT
toalt_pkgrepl()
{
	local i
	WARULES="s/^ //g 
		s/ \$//g 
		s/ |/|/g 
		s/| /|/g"
	for i in $PKGREPLBASE/pkgrepl.fedora $PKGREPLBASE/pkgrepl.* ; do
		#echo $i - $1
		REPLRULE=`grep -v "^#" "$i" 2>/dev/null | grep "$1\$" | sed -e "$WARULES" | sed -e "s/^\(.*\)|\(.*\)\$/\2|\1/g" | head -n1`
		# For broken rule
		echo $REPLRULE | grep "^|" >/dev/null && REPLRULE=""
		echo $REPLRULE | grep "|\$" >/dev/null && REPLRULE=""
		test -n "$REPLRULE" && return 0
	done
	return 1
}


parse_cmd_pre "$@"

if [ "$1" = "-h" ]; then
	echog "rpmcs spec - cleanup spec (like cleanup_spec command), adapted to ALT"
	exit 0
fi

for i in $LISTNAMES
do
	if [ -z ${i/*rpm/} ] ; then
		fatal "'$i' is not not spec file"
	fi

	# Make backup copy
	cp -f $i ${i}~

	# Unneeded on ALT
	subst "s|^Prefix:.*$||" $i
	subst "s|^BuildRoot:.*$||i" $i

	# Add packager
	if ! grep "^Packager" $i ; then
		PACKAGT="Packager: $($RPMBUILD --eval %packager $i)"
		cat $i | awk 'BEGIN{desk=0}{if(/^Source/&&desk==0){printf("%s\n\n", text);desk++};print}' text="$PACKAGT" >$i.tmp && mv -f $i.tmp $i
	fi

	# If non alt release, set initial alt release
	if get_release $i | grep -v "^alt" >/dev/null ; then
		set_release $i "alt1"
	fi

	# If it was PLD spec
	if grep "^%define date" $i ; then
		# with dummy spec we can't have any parsing problems
		if false ; then
			dummy_spec $i >$i.dummy
			subst "s|^%changelog.*|`stamp_spec $i.dummy`|" $i
			rm -f $i.dummy
			subst "s|^%define date.*|%changelog|" $i
			subst "s|* %{date} PLD Team\(.*\)|- initial build for ALT Linux Sisyphus (spec from PLD Team)|" $i
		else
			awk '{if ( ! /^%define date/){print}else{exit}}' $i > $i.tmp && mv -f $i.tmp $i
			add_changelog $i -e "- initial build for ALT Linux Sisyphus (spec from PLD)"
		fi
	fi

	echog "cleanup_spec for $i..."
	cleanup_spec $i

    # convert from PLD's cases	
	subst "s|%if %{with \(.*\)}|%if_with \1|g" $i
	subst "s|%if %{without \(.*\)}|%if_without \1|g" $i
	# replaces with and without too
	subst "s|^%bcond_with|%def_with|g" $i
	#subst "s|%{?with_\(.*\):|%{subst_with

	echog "fix name and release..."
	build_rpms_name "$i"
	# For case if name was macros
	test -n "$BASENAME" && set_var $i Name $BASENAME
	
	echog -n "fix non alt constructions..."
	subst "s|Source0:|Source:|" $i
	# Use tar.bz2 in any case
	subst "s|\(Source:.*\).tar.gz|\1.tar.bz2|" $i
	

	subst "s|\(%post.*-p *\)/sbin/ldconfig|\1%post_ldconfig|g" $i
	subst "s|\(%postun.*-p *\)/sbin/ldconfig|\1%postun_ldconfig|g" $i
	
	subst "s|\$RPM_BUILD_ROOT/%|%buildroot%|g" $i
	subst "s|\$RPM_BUILD_ROOT|%buildroot|g" $i
	#subst "s|%buildrootetc|%buildroot/etc|g" $i

    # Fix groups to ALT's
	subst "s|Group: *Libraries|Group: System/Libraries|g" $i

    # It is not recommended to use macroses for commands (__subst is unique in this)
	subst  "s|%__install |install |g 
			s|%__cp |cp |g 
			s|%__rm -f |rm -f |g 
			s|%__mv |mv |g 
			s|%__mkdir_p |mkdir -p |g 
			s|%__mkdir |mkdir |g 
			s|%__make |make |g 
			s|%__tar |tar |g" $i

	subst "s|%__make *install|%make_install install|g" $i
	subst "s|^make *install|%make_install install|g" $i

	subst "s|/etc/|%_sysconfdir/|g" $i
	subst "s|%_sysconfdir/rc.d/init.d|%_initdir/|g" $i
	subst "s|%_sysconfdir/rc.d/init.d/|%_initrddir/|g" $i
	subst "s|%_sysconfdir/init.d/|%_initrddir/|g" $i
	subst "s|%_sysconfdir/X11/fontpath.d|%_fontpathdir|g" $i

	subst "s|%_libdir/mono/gac|%_monogacdir|g" $i
	subst "s|%_libdir/mono|%_monodir|g" $i

	subst "s|Requires(post): /sbin/install-info|Requires(post): %install_info|g" $i
	subst "s|Requires(preun): /sbin/install-info|Requires(preun): %uninstall_info|g" $i

	# FIXME: only in rpm-build-compat
	#subst "s|^%_datadir/aclocal/|%_aclocaldir/|g" $i
	# TODO: not in expressions
	subst "s|/usr/sbin/|%_sbindir/|g" $i
	subst "s|/usr/bin/|%_bindir/|g" $i
	subst "s|/usr/lib|%_libdir|g" $i
	subst "s|/usr/share|%_datadir|g" $i

    # subst normalize	
	subst "s|^sed -i -e |%__subst |g" $i
	subst "s|^subst |%__subst |g" $i
	subst "s|perl -pi -e |%__subst |g" $i

	subst "s|^python |%__python |g" $i
	subst "s|%py_sitescriptdir|%python_sitelibdir|g" $i
	subst "s|%python_sitelib/|%python_sitelibdir/|g" $i
	subst "s|%python_sitearch|%python_sitelibdir|g" $i
	
	subst "s|%_mandir/man\([0-9]\)|%_man\1dir|g" $i
	subst "s|%_libdir/pkgconfig|%_pkgconfigdir|g" $i
	subst "s|%_datadir/pixmaps|%_pixmapsdir|g" $i
	subst "s|%_datadir/applications|%_desktopdir|g" $i

	subst "s|%update_desktop_database|%update_desktopdb|g" $i
	subst "s|%clean_desktop_database|%clean_desktopdb|g" $i

	subst "s|%gconf_schema_install \(.*\)\.schemas|%gconf2_install \1|g" $i

	subst "s|%_iconsdir/hicolor/48x48/apps|%_icons48dir|g" $i
	subst "s|%_iconsdir/hicolor/32x32/apps|%_niconsdir|g" $i
	subst "s|%_iconsdir/hicolor/16x16/apps|%_icons16dir|g" $i
	subst "s|%_iconsdir/mini|%_miconsdir|g" $i
	subst "s|%_iconsdir/large|%_liconsdir|g" $i


	subst "s|%_datadir/doc|%_docdir|g" $i

	subst "s|%rpmcflags|%optflags|g" $i

	subst "s|^%clean$||g" $i
	subst "s|^rm -rf %buildroot$||g" $i
    subst "s|^%defattr.*||g" $i
    subst "s|^%attr(.*)\(.*\)$|\1|g" $i
	subst "s|^ *%|%|g" $i

    # fix requires
	LISTDEP=`eval_spec $i | grep ^BuildReq | sed -e "s|^.*:||g"`
	# Build replacement rules for spec
	#echo $LISTDEP
	ALLREPLRULES=""
	for n in $LISTDEP ; do
		toalt_pkgrepl $n && ALLREPLRULES="$ALLREPLRULES
	s|$REPLRULE|g"
		#echo REPLRULE: $REPLRULE
	done

	#echo ALL: $ALLREPLRULES

	# Replace package names in source spec
	mv $i $i.old.cs.txt && cat $i.old.cs.txt | \
		sed -e "$ALLREPLRULES" > $i || mv $i.old.cs.txt $i
	rm -f $i.old.cs.txt
	echog "DONE"
done
