#!/bin/sh
# wrapper around dh_gencontrol that updates the binary version of corresponding
# ddebs
#
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# (C) 2015 Canonical Ltd.
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
set -e

DEBUG=1

if [ -x /usr/bin/dh_gencontrol.pkg-create-dbgsym ]; then
    REAL_DHGENCONTROL=/usr/bin/dh_gencontrol.pkg-create-dbgsym
elif [ "$0" != /usr/bin/dh_gencontrol ]; then
    # called as "dh_gencontrol" somewhere before /usr/bin in the PATH, e.g.
    # from the testsuite during package build
    REAL_DHGENCONTROL=/usr/bin/dh_gencontrol
    if ! [ -x "$REAL_DHGENCONTROL" ]; then
	echo "FATAL: Can't run /usr/bin/dh_gencontrol; is debhelper installed?" >&2
	exit 1
    fi
else
    echo "FATAL: Can't find the diverted original dh_gencontrol and /usr/bin/dh_gencontrol loops back to myself!" >&2
    exit 1
fi

# call real dh_gencontrol first, so that it will set correct package versions
$REAL_DHGENCONTROL "$@"

HOSTARCH=`dpkg-architecture -qDEB_HOST_ARCH`

# print debug message
dbg() {
    [ -n "$DEBUG" ] || return 0
    echo "dh_gencontrol debug symbol wrapper: $@"
}

# return 0 if $1 is in the string list $2, otherwise return 1
in_list()
{
    echo "$2" | grep -q "\(^\|[[:space:]]\)$1\(\$\|[[:space:]]\)"
}

# get set of all architecture dependent packages from debian/control
packages=$(
    unset pkg
    sed -n 's/[[:space:]]*$//; s/^\(Package\|Architecture\):[[:space:]]*\(.\+\)/\1 \2/p' debian/control | while read key val; do
	if [ "$key" = Package ]; then
	    [ -z "$pkg" ] || {
		echo "Error: Package: and Architecture: do not alternate in debian/control" >&2
		exit 1
	    }
	    pkg="$val"
	fi
	if [ "$key" = Architecture ]; then
	    [ -n "$pkg" ] || {
		echo "Error: Package: and Architecture: do not alternate in debian/control" >&2
		exit 1
	    }
	    for arch in $val; do
		if dpkg-architecture -a"$HOSTARCH" -i"$arch"; then
		    printf "$pkg "
		    break
		fi
	    done
	    unset pkg
	fi
    done
)
dbg "all non-arch-all packages for this build platform $HOSTARCH: $packages"

# parse command line to determine set of packages to act on
for p in $DH_OPTIONS $@; do
    if [ -n "$next_arg_is_xopt" ]; then
	xopts="$xopts -X$p"
	unset next_arg_is_xopt
	continue
    fi

    p=${p#-O}
    case "$p" in
	-i|--indep)
	    unset actpkgs
	    optsel=1
	    ;;
	-a|--arch|-s|--same-arch)
	    actpkgs="$packages"
	    optsel=1
	    ;;
	-p*|--package=*)
	    pkg=${p#--package=}
	    pkg=${pkg#-p}
	    if ! in_list "$pkg" "$actpkgs" && in_list "$pkg" "$packages"; then
		actpkgs="$pkg $actpkgs"
	    fi
	    optsel=1
	    ;;
	-N*|--no-package=*)
	    pkg=${p#--no-package=}
	    pkg=${pkg#-N}
	    if ! in_list "$pkg" "$ignorepkgs"; then
		ignorepkgs="$pkg $ignorepkgs"
	    fi
	    ;;
	-P*|--tmpdir=*)
	    pkgdirarg=${p#--tmpdir=}
	    pkgdirarg=${pkgdirarg#-P}
	    dbg "-P/--tmpdir option: setting temporary package dir to $pkgdirarg"
	    ;;
	-X)
	    next_arg_is_xopt=1
	    ;;
	-X*)
	    xopts="$xopts $p"
	    ;;
    esac
done

[ -n "$optsel" ] || actpkgs="$packages"

dbg "packages to act on: $actpkgs"
dbg "ignored packages: $ignorepkgs"

# check for debhelper compat level 1
dhcompat1=1
if [ -e debian/compat ]; then
    if [ "`cat debian/compat`" != 1 ]; then
	unset dhcompat1
    fi
fi
if grep -q '^[[:space:]]*\(export[[:space:]]*\)\?DH_COMPAT[[:space:]]*:\?=[[:space:]]*[2-9]\>' debian/rules; then
    unset dhcompat1
elif grep -q '^[[:space:]]*\(export[[:space:]]*\)\?DH_COMPAT[[:space:]]*:\?=[[:space:]]*1\>' debian/rules; then
    dhcompat1=1
fi

if [ -n "$dhcompat1" ]; then
    first_control_pkg="`grep -m 1 ^Package debian/control | cut -f2- -d\ `"
    dbg "using obsolete debhelper compat mode 1"
fi

# process all packages
for p in $actpkgs; do
    if in_list "$p" "$ignorepkgs" ||
	echo $p | grep -q -- '-dbg$' ; then
	continue
    fi

    if [ -n "$pkgdirarg" ]; then
	pkgdir="$pkgdirarg"
    elif [ -n "$dhcompat1" -a $p = "$first_control_pkg" ]; then
	pkgdir=debian/tmp
    else
	pkgdir=debian/$p
    fi

    ddebpkgdir=debian/${p}-dbgsym
    if [ ! -d $ddebpkgdir ]; then
	dbg "no $ddebpkgdir, skipping package $p"
	continue
    fi

    auto_stamp=$ddebpkgdir/DEBIAN/auto.pkg-create-dbgsym
    if [ ! -e $auto_stamp ]; then
	dbg "$ddebpkgdir not produced by pkg_create_dbgsym dh_strip wrapper, skipping package $p"
	continue
    fi
    rm $auto_stamp

    dbg "processing package $p (pkgdir $pkgdir, ddeb package dir $ddebpkgdir)"

    # adjust ddeb version from deb, for dpkg-gencontrol's -v option
    debversion=`grep '^Version:' $pkgdir/DEBIAN/control | cut -f2- -d\ `
    sed -i "s/^Version: .*\$/Version: $debversion/; /^Depends:/ s/(= .*)/(= $debversion)/" $ddebpkgdir/DEBIAN/control
    # add Source: version too, if binary version is different
    srcversion=`dpkg-parsechangelog -SVersion`
    if [ "$debversion" != "$srcversion" ]; then
        sed -i "/^Source:/ s/\$/ ($srcversion)/" $ddebpkgdir/DEBIAN/control
    fi

    # build .ddeb from its package directory
    ddebarch=`grep '^Architecture:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddebsection=`grep '^Section:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddebpriority=`grep '^Priority:' $ddebpkgdir/DEBIAN/control | cut -f2- -d\ `
    ddeb="${p}-dbgsym_${debversion#*:}_${ddebarch}.ddeb"
    dbg "building $ddeb"
    NO_PKG_MANGLE=1 dpkg-deb -Zxz --build $ddebpkgdir ../$ddeb
    add_to_files_stamp=$ddebpkgdir/DEBIAN/add_to_files.pkg-create-dbgsym
    if [ -e $add_to_files_stamp ]; then
	rm $add_to_files_stamp
	dbg "dpkg-distaddfile $ddeb $ddebsection $ddebpriority"
	dpkg-distaddfile "$ddeb" "$ddebsection" "$ddebpriority"
    fi

    # clean up ddeb pkg dir, since this package does not appear in debian/control
    rm -rf "$ddebpkgdir"
done
