#! /bin/bash

# This script is not yet installed to system path. Guess its location:
if [ "$0" = backport-factory ] ; then
    DIR="$(if ! which 2>/dev/null backport-factory ; then echo "$PWD/backport-factory" ; fi )"
    DIR="${DIR%/*}"
else
    DIR="${0%/*}"
fi
if [ "${DIR:0:1}" != / ] ; then
    DIR=$PWD/$DIR
fi

BS_PROJECT=$1
HACKDIR=$DIR/backport-factory-hacks/$BS_PROJECT

if test -z "$BS_PROJECT" -o "$BS_PROJECT" = "--help" ; then
    echo "Usage: backport-factory BS_PROJECT [ foo1.spec [ foo2.spec ...]]"
    echo "If no argument is given, apply for *.spec."
    exit 1
fi

declare -a SED_SCRIPT

# package name    in > suse_version  BS_PROJECTS_UPDATED ("," separated)  old package name(s) (use \n for newline)
# TODO: rule for more packages -> something
echo '
# Renamed packages:
DirectFB-devel       1010 -              DirectFB
dbus-1-glib-devel    1010 GNOME:UNSTABLE dbus-1-devel dbus-1-glib
linux-kernel-headers 1010 -              kernel-source
libexpat-devel       1020 -              expat
libopenssl-devel     1020 -              openssl-devel
libqt4-devel         1010 -              qt-devel
libusb-devel         1020 -              libusb
python-gtk-devel     1010 GNOME:UNSTABLE python-gtk
texlive-devel        1020 -              tetex
texlive              1020 -              tetex
# Bug fixes:
cups-devel           1020 -              cups-devel openssl-devel
transfig             1020 -              transfig ghostscript-fonts-std ghostscript-library
' |
(

SED_SCRIPT[${#SED_SCRIPT[@]}]='#!/bin/sed
# Skip sections already branched by %if %suse_version:
# Put mark "@NESTED_SUSEVERSION@" mark to hold space for any "%if %suse_version"
/^%if %suse_version/{
    x
    s/^/@NESTED_SUSEVERSION@/
    x
    b fendif
}
# Put mark "@NESTED@" mark to hold space for any other "%if"
/^%if/{
    x
    s/^/@NESTED@/
    x
}

# Remove mark from hold space if "%endif" is found.
: fendif
/^%endif/{
    x
    s/^@NESTED[^@]*@//
    x
}
# Uncomment to visualize nested flag in the hold space:
#/^/G;s/\n@/ @/;s/\n//;

# Reset t replace flag
t treset1
: treset1

# If we are inside "%if %suse_version", do nothing.
x
/@NESTED_SUSEVERSION@/{
    b skip
}
x

# Here is a good place to fix unconditionally used new RPM tags:
s/^Enhances:.*/%if %suse_version > 1000\n&\n%endif/
s/^Suggests:.*/%if %suse_version > 1000\n&\n%endif/
s/^Freshens:.*/%if %suse_version > 1000\n&\n%endif/
# FIXME: Sometimes we may need Requires.
s/^Supplements:.*/%if %suse_version > 1000\n&\n%endif/
s/^Recommends:.*/%if %suse_version > 1000\n&\n%endif/
# This cannot be mapped:
s/^Essentialfor:.*/%if %suse_version > 1000\n&\n%endif/
'

    while read -r PACKAGE SUSE_VERSION BS_UPDATED RAW_REPLACEMENT ; do
	if test -z "$PACKAGE" ; then
	    continue
	fi
	if test "${PACKAGE:0:1}" = '#' ; then
	    continue
	fi
	if test "$BS_UPDATED" = "-" ; then
	    BS_UPDATED=""
	else
	    BS_UPDATED="${BS_UPDATED//,/ }"
	fi
	BS_STRING=""
	for DISTRO in $BS_UPDATED ; do
	    if test "$DISTRO" = "$BS_PROJECT" ; then
		BS_STRING=" || 0%{?opensuse_bs}"
	    fi
	done
	REPLACEMENT="%if %suse_version > $SUSE_VERSION$BS_STRING\\nRequires: $PACKAGE\\n%else\\nRequires: $RAW_REPLACEMENT\\n%endif"
	BREPLACEMENT="%if %suse_version > $SUSE_VERSION$BS_STRING\\nBuildRequires: $PACKAGE\\n%else\\nBuildRequires: $RAW_REPLACEMENT\\n%endif"
	SED_SCRIPT[${#SED_SCRIPT[@]}]="# Fixes for $PACKAGE:
# Update Requires, line not yet edited
s/^\\(Requires:.*\\) $PACKAGE\\( .*\\|$\\)/# Auto modified by backport-factory:\\n#&\\n\\1\\2/
T rqo-$PACKAGE
a \\
$REPLACEMENT
t rqe-$PACKAGE
: rqo-$PACKAGE
# Update Requires, line already edited
s/\\(\\nRequires:.*\\) $PACKAGE\\( .*\\|$\\)/\\1\\2/
T rqe-$PACKAGE
a \\
$REPLACEMENT
: rqe-$PACKAGE

# Update BuildRequires, line not yet edited
s/^\\(BuildRequires:.*\\) $PACKAGE\\( .*\\|$\\)/# Auto modified by backport-factory:\\n#&\\n\\1\\2/
T bro-$PACKAGE
a \\
$BREPLACEMENT
t bre-$PACKAGE
: bro-$PACKAGE
# Update BuildRequires, line already edited
s/\\(\\nBuildRequires:.*\\) $PACKAGE\\( .*\\|$\\)/\\1\\2/
T bre-$PACKAGE
a \\
$BREPLACEMENT
: bre-$PACKAGE
"
    done

SED_SCRIPT[${#SED_SCRIPT[@]}]='#!/bin/sed
# Remove remaining empty tags:
/\(^\|\n\)BuildRequires:[[:space:]]*\(\n\|$\)/d
/\(^\|\n\)Requires:[[:space:]]*\(\n\|$\)/d
# Skipped
x
: skip
x
'

IFS=$'\n'

if test -z "$2" ; then
    SPECLIST=( *.spec )
else
    shift
    SPECLIST=( "$@" )
fi

for SPEC in "${SPECLIST[@]}" ; do
    sed -i "${SED_SCRIPT[*]}" "$SPEC"
    if ! grep -q ^%debug_package "$SPEC" ; then
	sed -i '/^%prep/i \
%debug_package' "$SPEC"
    fi

    # Add known auto hacks for known packages.
    PKGNAME=${SPEC%.spec}
    PKGNAME=${PKGNAME##*/}
    hack_pkg_config=false
    hack_libdns_sd=false
    if test -f $HACKDIR/$PKGNAME ; then
	. $HACKDIR/$PKGNAME
    fi
    if $hack_pkg_config ; then
	echo "Adding pkg-config work-around for $PKGNAME..."
	sed -i '1,/^BuildRequires:/{
/^BuildRequires:/a# Auto added by backport-factory:\
%if 0%{?opensuse_bs}\nBuildRequires:  pkg-config\n%endif
}' $SPEC
    fi
    if $hack_libdns_sd ; then
	echo "Adding libdns_sd work-around for $PKGNAME..."
	sed -i '1,/^BuildRequires:/{
/^BuildRequires:/a# Auto added by backport-factory:\
BuildRequires: mDNSResponder-devel
}' $SPEC
    fi

done

)
