#!/bin/sh -e

# wrapper around dh_strip that generates packages with external debug symbols
#
# Author: Martin Pitt <martin.pitt@ubuntu.com>
# (C) 2006 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

DEBUG=1

REAL_DHSTRIP=/usr/bin/dh_strip.pkg-create-dbgsym 
[ -x $REAL_DHSTRIP ] || REAL_DHSTRIP=/usr/bin/dh_strip

[ -x $REAL_DHSTRIP ] || {
    echo "real dh_strip $REAL_DHSTRIP does not exist, aborting" >&2
    exit 1
}

BUILDARCH=`dpkg-architecture -qDEB_BUILD_ARCH`

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

if grep -qs '^Suite: [a-z]*-autotest' /CurrentlyBuilding; then
    NO_PKG_MANGLE=1
    dbg "disabling for archive rebuild test"
fi
if grep -qs '^Purpose: PPA' /CurrentlyBuilding; then
    NO_PKG_MANGLE=1
    dbg "disabling for PPA build"
fi

if [ -n "$NO_PKG_MANGLE" ]; then
    dbg "not doing anything since NO_PKG_MANGLE is given"
    exec $REAL_DHSTRIP "$@"
    exit 0
fi

# get set of all architecture dependent packages from debian/control
packages=$(
    unset pkg
    egrep '^(Package|Architecture):' 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
	    }
	    if [ "$val" = any ] || echo "$val" | fgrep -qw $BUILDARCH; then
		printf "$pkg "
	    fi
	    unset pkg
	fi
    done 
)
dbg "all non-arch-all packages for this build platform $BUILDARCH: $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

    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 ! echo "$actpkgs" | grep -wq "$pkg" &&
	       echo "$packages" | grep -wq "$pkg"; then
		actpkgs="$pkg $actpkgs"
	    fi
	    optsel=1
	    ;;
	-N*|--no-package=*)
	    pkg=${p#--no-package=}
	    pkg=${pkg#-N}
	    if ! echo "$ignorepkgs" | grep -wq "$pkg"; then
		ignorepkgs="$pkg $ignorepkgs"
	    fi
	    ;;
	--dbg-package*)
	    dbg "not adding gnu debuglinks since --dbg-package is given"
	    nodebuglink=1
	    ;;
	--keep-debug)
	    dbg "not adding gnu debuglinks since --keep-debug is given"
	    nodebuglink=1
	    ;;
	-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 echo "$ignorepkgs" | grep -wq "$p" ||
       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

    pkg_create_dbgsym $xopts $p $pkgdir "$nodebuglink"
done

exec $REAL_DHSTRIP "$@"
