#!/bin/bash

# Autobuild doesn't support package names with macros. This script will update versions in package names.

ORIG_SPEC=${2%-mini}
# Never update -mini fíle when it is already opened. It will break advanced build scripts:
if test "$2" != "$ORIG_SPEC" ; then
    exit
fi

if ! test -f $1/$ORIG_SPEC.spec ; then
    exit
fi

for PKGNAME in libcdio libcdio_cdda libcdio_paranoia libcdio++ libiso9660 libudf ; do
    # Packages with name ending by number needs separator
    if test x`echo $PKGNAME | sed "s/.*[0-9]$//"` = x ; then
	SEPARATOR=-
    else
	SEPARATOR=
    fi
    sed -i "
	# Copy ${PKGNAME}_name to hold space
	/^%define ${PKGNAME}_name / {
	    h;
	    s/^%define ${PKGNAME}_name //;
	    x;
	};

	# Update ${PKGNAME}_name everywhere
	/$PKGNAME$SEPARATOR[0-9][-_0-9]*/ {
	    G;
	    s/$PKGNAME$SEPARATOR[0-9][-_0-9]*\(.*\)\n\(.*\)/$PKGNAME$SEPARATOR\2\1/;
	};" $1/$ORIG_SPEC.spec
done

# Edit file to fit -mini build needs:
sed '
    s/STANDARD_BUILD\ 1/STANDARD_BUILD 0/;
    s/^.ame:.*/&-mini/;
    # No more needed.
    #s/^..hangelog.*/& mini/;
    s/libcdio-cdparanoia-compat/libcdio-cdparanoia-compat-mini/g
    s/\(libcdio_cdda\|libcdio_paranoia\|libcdio++\|libcdio\|libiso9660-\|libudf\)\([0-9][-_0-9]*\)/\1\2-mini/g
    s/^\(# WARNING:\).*After editing.*/\1 Do not edit this auto generated file./
    s/^%package -n libcdio7-mini/&\nProvides:       libcdio-mini = %{version}-%{release}/
    s/^Summary:[[:space:]]*/&Temporary Build Staging Package of /
' <$1/$ORIG_SPEC.spec >$1/$ORIG_SPEC-mini.spec

cp -a $1/$ORIG_SPEC.changes $1/$ORIG_SPEC-mini.changes
