#!/bin/sh
# 2003-2006 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# $Id: rpmbph,v 1.51 2007/05/30 18:08:15 lav Exp $
#
# BPH - backport; use hasher
# 06.09.2005, 12.09.2005, 07.11.2005, 16.02.2006
# Makes backport package
#       .    alt4
#    alt0.M24.4
# TODO: use RPMBUILDARG

. /etc/rpm/etersoft-build-functions

BUILDCOMMAND=rpmbsh

#############################

Usage="Usage: $name [-r -u -n -i -m] spec..."
function mygetopts()
{
name=${0##*/}
Descr="$name - backports spec and rebuild it"

phelp()
{
	echog "$Descr"
	echog "$Usage"
	echog "Options:"
	echog " -r - remote build"
	echog " -m - send result via e-mail"
	echog " -u - sign and upload after build"
	echog " -n - do not build in hasher"
	echog " -i - install built packages in test hasher"
}

make_substitution()
{
	# ttmkfdir do not coexist with X?
	# done with rpm-build-compat:
	#s|%_pkgconfigdir|%_libdir/pkgconfig|g
	
	sed -e "
		s|%__python_version|$PYTHON|g
		s|%python-dev||g
		s|%python-base||g
		s|%py_provides \(.*\)$|Provides: python$PYTHON(\1)|g
		s|libstdc++3.4-devel-precompiled||g
		s|pkg-config|pkgconfig|g
		s|gcc3.4|gcc$GCC|g
		s|postgresql8.[01]|postgresql|g
		s|libpq4|libpq3|g
		s|libICE-devel|XFree86-devel|g
		s|libX11-devel|XFree86-devel|g
		s|libXt-devel|XFree86-devel|g
		s|libgnomeoffice-devel||g
		s|libXext-devel|XFree86-devel|g
		s|linux-libc-headers|glibc-kernheaders|g
		s|xorg-cf-files||g
		s|libfreetype|freetype2|g
		s|xorg-x11-proto-devel|XFree86-devel|g
		s|libSDL_sound-devel|libSDL-devel|g
		s|Development/Documentation|Development/C|g
		s|\(Build.*\)\(ttmkfdir\)\(.*\)|\1freetype\3|g" |
		# add buildrequires (line from php-coder@)
		awk 'BEGIN{desk=0}{if(/^%description/&&desk==0){printf("%s\n\n", text);desk++};print}' text="$BUILDREQ" |
		# add reconf for M23 after %build
		awk 'BEGIN{desk=0}{print;if(/^%build/&&desk==0){printf("%s\n\n", text);desk++}}' text="$RECONFT"
	#sed -e "`grep -n file | cut -d: -f1`aBuildRequires: rpm-build-compat\n"

}

while getopts :hni opt; do
    case $opt in
    h) phelp; exit 0;;
    n) BUILDCOMMAND="rpmbs";;
    +?) echog "$name: options should not be preceded by a '+'." 1>&2; exit 2;;
#    ?)  echog "$name: $OPTARG: bad option.  Use -h for help." 1>&2 ; exit 2;;
	?) OPTIND=$((OPTIND-1)); break;
    esac
done

# remove args that were options
if [ $# -gt 0 ]; then 
	shift $((OPTIND - 1))
fi

# pass other options to RPM:
LISTRPMARGS=$@
}

parse_cmd_pre "$@"
mygetopts $LISTARGS

MDISTR=$MENV
unset MENV MENVARG
test -z "$MDISTR" && fatal "missed backport version (f.i. run with -M24 param for Master 2.4)"
TOCHANGELOG=

mkdir -p $RPMDIR/SPECS/$MDISTR

test -z "$LISTNAMES" && fatal "Error: no such file"

for i in $LISTNAMES ; do
	#grep Packager ../$i || { echog "Packager field must be in backported spec"; exit 1 ; }
	DEST=`basename $i`
	cp $i $RPMDIR/SPECS/$MDISTR/$DEST || fatal "Can't copy spec"
	cd $RPMDIR/SPECS/$MDISTR || fatal "Error"
	BASERELEASE=$(get_release $DEST | sed -e "s/alt//")
	set_release $DEST alt0.$MDISTR.$BASERELEASE
	case $MDISTR in
		"M23")
			GCC=3.2
			PYTHON=2.3
			if grep "%configure" $i >/dev/null && ! grep "^autoreconf" $i >/dev/null && ! grep "^%__autoreconf" $i >/dev/null ; then
				RECONFT="%__autoreconf"
				TOCHANGELOG="add __autoreconf macros in build section for fix so name problem (by script)"
			fi
			;;
		"M24")
			GCC=3.3
			PYTHON=2.3
		;;
		"M30")
			GCC=3.4
			PYTHON=2.4
			;;
		"M40")
			GCC=4.2
			PYTHON=2.4
			;;
		*)
			GCC=3.4
			PYTHON=2.4
			;;
	esac
	BUILDREQ="BuildRequires: rpm-build-compat >= 0.4"
	echo $i | grep rpm-build-altlinux-compat &>/dev/null && BUILDREQ=""
	mv $DEST $DEST.old && make_substitution <$DEST.old >$DEST || fatal "Can't make substituon"
	rm $DEST.old
	if [ "$MDISTR" = "M23" ] || [ "$MDISTR" = "M24" ] ; then
		subst "s|xorg-x11-|XFree86-|g" $DEST
		subst "s|libcairo-devel|cairo-devel|g" $DEST
#	else
#		subst "s|linux-libc-headers
	fi

    # Hack for wine against broken glibc in Compact 3.0
	#if [ "$MDISTR" = "M30" -a "$DEST" = "wine.spec" ] ; then
	#    subst "1iRequires: glibc-core-i686" $DEST
	#	subst "1i# Special hack for wine (by rpmbph script)" $DEST
	#fi

	ENTRY="- backport to ALT Linux $MDISTR (by rpmbph script)"
	test -n "$TOCHANGELOG" && ENTRY=`echo -e "$ENTRY\n- $TOCHANGELOG"`
	add_changelog $DEST -e "$ENTRY"
	echo "NOTE: $TOCHANGELOG"
	subst "1i# This spec for backport to $MDISTR is autogenerated. Do not edit it." $DEST
	$BUILDCOMMAND $LISTRPMARGS $REMOTEBUILD -$MDISTR $DEST || fatal "Can't build"
	cd -
done

