#!/bin/sh
# 2003-2007 (c) Etersoft www.etersoft.ru
# Author: Vitaly Lipatov <lav@etersoft.ru>
# Public domain
# $Id: rpmbph,v 1.61 2007/09/07 22:45:50 lav Exp $
#
# BPH - backport; use hasher
# 06.09.2005, 12.09.2005, 07.11.2005, 16.02.2006, 02.07.2007, 08.09.2007
# Makes backport package to any target platfrom (RPM, DEB, TGZ)
#       .    alt4
#    alt0.M24.4
# TODO: use RPMBUILDARG

. /etc/rpm/etersoft-build-functions

#VENDORVERSION=`distr_vendor -e`
DISTRVERSION=`distr_vendor -v`

# FIXME: WARULES is missed

# Get replacement rule for ALT package to local in $1
tolocal_anyrepl()
{
	local i
	local GREP=$1
	shift
	for i in $@ ; do
		REPLRULE=`grep -v "^#" "$i" 2>/dev/null | grep "^$GREP" | sed -e "$WARULES"`
		# For broken rule
		echo $REPLRULE | grep "|" >/dev/null || REPLRULE=""
		test -n "$REPLRULE" && return 0
	done
	return 1
}

alt_tolocal()
{
local SPECNAME
SPECNAME=$1

# Set buildreq
BASERELEASE=$(get_release $DEST | sed -e "s/alt//")
if [ "$VENDOR" = "alt" ] ; then
	BUILDREQ="BuildRequires: rpm-build-compat >= 0.94"
	BUILDCOMMAND=rpmbsh
	# Change release according to alt policy
	set_release $DEST alt0.$MDISTR.$BASERELEASE
	ADDDEF=""
	CLEANTEXT=""
else
	BUILDREQ="BuildRequires: rpm-build-altlinux-compat >= 0.94"
	BUILDCOMMAND=rpmbb
	set_release $DEST eter$(($BASERELEASE + 1))$VENDOR
	ADDDEF="%defattr(-, root, root)"
	CLEANTEXT=""
fi

# Do not add BuildReq if already exist
echo $i | grep rpm-build-compat &>/dev/null && BUILDREQ=""

# Too old problem
TOCHANGELOG=
if [ "$MDISTR" = "M23" ] ; then
	if grep "%configure" $SPECNAME >/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
fi

LISTDEP=`eval_spec $SPECNAME | grep BuildReq | sed -e "s|^.*:||g"`
LISTGROUP=`eval_spec $SPECNAME | grep Group | sed -e "s|^.*:||g"`

# TODO: fix space removing
WARULES="s/^ //g 
	s/ \$//g 
	s/ |/|/g 
	s/| /|/g"


# FIXME: replace only in req lines
# Package names
LIST="$PKGREPLBASE/pkgrepl.$VENDOR.$DISTRVERSION $PKGREPLBASE/pkgrepl.$VENDOR"
[ "$VENDOR" != "alt" ] && LIST="$LIST  $PKGREPLBASE/pkgrepl.fedora"
#echo LIST: $LIST
# Build replacement rules for spec
ALLREPLRULES=""
for i in $LISTDEP ; do
	tolocal_anyrepl $i $LIST && ALLREPLRULES="$ALLREPLRULES
		s|$REPLRULE|g"
done

# RPM Group names
LIST="$PKGREPLBASE/../grprepl/grprepl.$VENDOR.$DISTRVERSION $PKGREPLBASE/../grprepl/greprepl.$VENDOR"
[ "$VENDOR" != "alt" ] && LIST="$LIST $PKGREPLBASE/../grprepl/grprepl.fedora"
#echo LIST: $LIST
# Build replacement rules for spec
ALLGRPREPLRULES=""
for i in $LISTGROUP ; do
	tolocal_anyrepl $i $LIST && ALLGRPREPLRULES="$ALLGRPREPLRULES
		s|$REPLRULE|g"
done
#echo GRP: $LISTGROUP, ALLGRPREPLRULES: $ALLGRPREPLRULES

mv $SPECNAME $SPECNAME.old
# Replace all packages names in source spec and add %defattr to files sections
# FIXME:    
cat $SPECNAME.old | \
	sed -e "$ALLREPLRULES" | sed -e "$ALLGRPREPLRULES" |\
	awk 'BEGIN{desk=0}{print;if(/^%files/){printf("%s\n", text)}}' text="$ADDDEF" | \
	# add clean before %files
	awk 'BEGIN{desk=0}{if(/^%files/&&desk==0){printf("%s\n\n", text);desk++};print}' text="$CLEANTEXT" | \
	# add rpm-build-compat buildreq
	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" > $SPECNAME
#TODO:
#%if %{undefined buildroot}
#BuildRoot: %{_tmppath}/%{name}-%{version}-buildroot
#%endif
rm -f $SPECNAME.old
subst "1i# This spec is autoported to `distr_vendor -d` $DISTRVERSION automatically by rpmbh script. Do not edit it." $SPECNAME

ENTRY="- backport to `distr_vendor -d` $DISTRVERSION (by rpmbph script)"
test -n "$TOCHANGELOG" && ENTRY=`echo -e "$ENTRY\n- $TOCHANGELOG"`
add_changelog $DEST -e "$ENTRY"

$BUILDCOMMAND $LISTRPMARGS $REMOTEBUILD -$MDISTR $DEST || fatal "Can't build"

}


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

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"
}


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

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

if [ "$VENDOR" = "alt" ] ; then
	MDISTR=$MENV
	test -z "$MDISTR" && fatal "missed backport version (f.i. run with -M24 param for Master 2.4)"
	DISTRVERSION=`echo $MDISTR | sed -e "s|M24|2\.4|g" | sed -e "s|M23|2\.3|g" | sed -e "s|M30|3\.0|g" | sed -e "s|M40|4\.0|g"	| sed -e "s|SS|Sisyphus|g"`
else
	MDISTR=LOCAL
fi

unset MENV MENVARG

mkdir -p $RPMDIR/SPECS/$MDISTR

for i in $LISTNAMES ; do
	DEST=`basename $i`
	cp $i $RPMDIR/SPECS/$MDISTR/$DEST || fatal "Can't copy spec"
	cd $RPMDIR/SPECS/$MDISTR || fatal "Error"
	alt_tolocal $DEST
	cd -
done

