#!/bin/sh
#
# generate control preamble from control.pcp
#
tmp=/var/tmp/$$
trap "rm -f $tmp.*; exit 0" 0 1 2 3 15


# dh-python not always available ... this is not checked for in configure
# so have to base it on the platform type and version
#
rm -f $tmp.no

if [ -f /etc/os-release ]
then
    NAME=`sed -n -e 's/"//'g -e '/^NAME=/s/NAME=//p' </etc/os-release`
    if [ "$NAME" = "Debian GNU/Linux" ]
    then
	VERSION_ID=`sed -n -e 's/"//'g -e '/^VERSION_ID=/s/VERSION_ID=//p' </etc/os-release`
	case "$VERSION_ID"
	in
	    7)
		touch $tmp.no
		;;
	esac
    elif [ "$NAME" = "Ubuntu" ]
    then
	VERSION_ID=`sed -n -e 's/"//'g -e '/^VERSION_ID=/s/VERSION_ID=//p' </etc/os-release`
	case "$VERSION_ID"
	in
	    12.04|12.10|13.04)
		touch $tmp.no
		;;
	esac
    fi
elif [ -f /etc/debian_version ]
then
    VERSION_ID=`cat /etc/debian_version`
    case "$VERSION_ID"
    in
	6.*)
	    touch $tmp.no
	    ;;
    esac
fi

echo "s/$/ /" >$tmp.sed

if [ -f $tmp.no ]
then
    echo "s/?{dh-python}, //" >>$tmp.sed
else
    echo "s/?{dh-python}, /dh-python, /" >>$tmp.sed
fi

# for some others we rely on what configure has discovered
#
if [ ! -f ../src/include/builddefs ]
then
    echo "fixcontrol: Botch: ../src/include/builddefs does not exist"
    exit 1
fi

eval `egrep '^(ENABLE_|PMDA_|HAVE_|QT_)' ../src/include/builddefs | sed -e 's/ //g'`

if $PMDA_PERFEVENT
then
    echo "s/?{libpfm4-dev}, /libpfm4-dev, /" >>$tmp.sed
else
    echo "s/?{libpfm4-dev}, //" >>$tmp.sed
fi

if $PMDA_INFINIBAND
then
    echo "s/?{libibumad-dev}, /libibumad-dev, /" >>$tmp.sed
    echo "s/?{libibmad-dev}, /libibmad-dev, /" >>$tmp.sed
else
    echo "s/?{libibumad-dev}, //" >>$tmp.sed
    echo "s/?{libibmad-dev}, //" >>$tmp.sed
fi

if $PMDA_BCC
then
    echo "s/?{python3-bpfcc}, /python3-bpfcc [amd64 arm64 armhf ppc64el s390x ppc64], /" >>$tmp.sed
else
    echo "s/?{python3-bpfcc}, //" >>$tmp.sed
fi

if $PMDA_BPFTRACE
then
    echo "s/?{bpftrace}, /bpftrace (>= 0.9.2) [amd64 arm64 ppc64el], /" >>$tmp.sed
else
    echo "s/?{bpftrace}, //" >>$tmp.sed
fi

if $PMDA_POSTGRESQL
then
    if $ENABLE_PYTHON3
    then
	echo "s/?{python-psycopg2}, /python3-psycopg2, /" >>$tmp.sed
    else
	echo "s/?{python-psycopg2}, /python-psycopg2, /" >>$tmp.sed
    fi
else
    echo "s/?{python-psycopg2}, //" >>$tmp.sed
fi

# PMDA_SYSTEMD is only true if we have libsystemd, but HAVE_SYSTEMD
# may be true when we're using systemd even if libsystemd is not
# installed ... so use the former to enable the libsystemd-dev in
# the Build-Depends: clause
# Note: that the HAVE_SYSTEMD_SD_DAEMON_H macro that is used to
# guard the use of libsystemd from libpcp is not available here, but
# the semantics of HAVE_SYSTEMD_SD_DAEMON_H and PMDA_SYSTEMD are
# close enough to being the same!
#
if $PMDA_SYSTEMD
then
    echo "s/?{libsystemd-dev}, /systemd, libsystemd-dev, /" >>$tmp.sed
else
    echo "s/?{libsystemd-dev}, //" >>$tmp.sed
fi

if $ENABLE_PYTHON2
then
    echo "s/?{python-all}, /python-all, /" >>$tmp.sed
    (
	. /etc/os-release
	case "$ID"
	in
	    debian)
		if [ -z "$VERSION_ID" ] || [ "$VERSION_ID" -ge 11 ]; then
		    echo "s/?{python-dev}, /python2-dev, /" >>$tmp.sed
		else
		    echo "s/?{python-dev}, /python-dev, /" >>$tmp.sed
		fi
		;;
	    ubuntu)
		VERSION_NO=$(echo "$VERSION_ID" | sed 's/[^0-9]//')
		if [ "$VERSION_NO" -ge 2004 ]; then
		    echo "s/?{python-dev}, /python2-dev, /" >>$tmp.sed
		else
		    echo "s/?{python-dev}, /python-dev, /" >>$tmp.sed
		fi
		;;
	esac
    )
else
    echo "s/?{python-dev}, //" >>$tmp.sed
    echo "s/?{python-all}, //" >>$tmp.sed
fi

if $ENABLE_PYTHON3
then
    echo "s/?{python-pcp}, /python3-pcp, /" >>$tmp.sed
    echo "s/?{python}, /python3, /" >>$tmp.sed
elif $ENABLE_PYTHON2
then
    echo "s/?{python-pcp}, /python-pcp, /" >>$tmp.sed
    echo "s/?{python}, /python, /" >>$tmp.sed
else
    echo "s/?{python-pcp}, //" >>$tmp.sed
    echo "s/?{python}, //" >>$tmp.sed
fi

if $HAVE_PYTHON_JSONPOINTER
then
    if $ENABLE_PYTHON3
    then
	echo "s/?{python-json-pointer}, /python3-json-pointer, /" >>$tmp.sed
    else
	echo "s/?{python-json-pointer}, /python-json-pointer, /" >>$tmp.sed
    fi
else
    echo "s/?{python-json-pointer}, //" >>$tmp.sed
fi

if $HAVE_PYTHON_OPENPYXL
then
    if $ENABLE_PYTHON3
    then
	echo "s/?{python-openpyxl}, /python3-openpyxl, /" >>$tmp.sed
    else
	echo "s/?{python-openpyxl}, /python-openpyxl, /" >>$tmp.sed
    fi
else
    echo "s/?{python-openpyxl}, //" >>$tmp.sed
fi

if $HAVE_PYTHON_REQUESTS
then
    if $ENABLE_PYTHON3
    then
	echo "s/?{python-requests}, /python3-requests, /" >>$tmp.sed
    else
	echo "s/?{python-requests}, /python-requests, /" >>$tmp.sed
    fi
else
    echo "s/?{python-requests}, //" >>$tmp.sed
fi

if $HAVE_PYTHON_SIX
then
    if $ENABLE_PYTHON3
    then
	echo "s/?{python-six}, /python3-six, /" >>$tmp.sed
    else
	echo "s/?{python-six}, /python3-six, /" >>$tmp.sed
    fi
else
    echo "s/?{python-six}, //" >>$tmp.sed
fi

if $ENABLE_LZMA
then
    echo "s/?{liblzma-dev}, /liblzma-dev, /" >>$tmp.sed
else
    echo "s/?{liblzma-dev}, //" >>$tmp.sed
fi

if [ "$QT_VERSION" -ge 5 ]
then
    echo "s/?{qt-dev}, /qtbase5-dev, qtbase5-dev-tools, libqt5svg5-dev, qtchooser, /" >>$tmp.sed
elif [ "$QT_VERSION" -ge 4 ]
then
    echo "s/?{qt-dev}, /libqt4-dev, qt4-qmake, /" >>$tmp.sed
else
    echo "s/?{qt-dev}, //" >>$tmp.sed
fi

if $HAVE_OPENSSL
then
    echo "s/?{libssl-dev}, /libssl-dev, /" >>$tmp.sed
else
    echo "s/?{libssl-dev}, //" >>$tmp.sed
fi

if $HAVE_LIBUV
then
    echo "s/?{libuv1-dev}, /libuv1-dev, /" >>$tmp.sed
else
    echo "s/?{libuv1-dev}, //" >>$tmp.sed
fi

echo "s/ $//" >>$tmp.sed
sed -f $tmp.sed <control.pcp
