From 863fb59476947ef125b24ad89afc040301938b96 Mon Sep 17 00:00:00 2001
Package: debootstrap
Version: 1.0.21
From: Guillem Jover <guillem@debian.org>
Date: Fri, 20 Nov 2009 21:25:32 +0100
Status: applied
Subject: [PATCH 2/3] Use dpkg-deb if available instead of ar

---
 debian/control |    2 +-
 debootstrap    |    2 ++
 functions      |   41 +++++++++++++++++++++++++++++++++++++++--
 3 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/debian/control b/debian/control
index 577b759..01e7d2d 100644
--- a/debian/control
+++ b/debian/control
@@ -9,7 +9,7 @@ Vcs-Svn: svn://svn.debian.org/d-i/trunk/packages/debootstrap
 
 Package: debootstrap
 Architecture: all
-Depends: wget, binutils
+Depends: wget
 Recommends: gnupg
 Description: Bootstrap a basic Debian system
  debootstrap is used to create a Debian base system from scratch,
diff --git a/debootstrap b/debootstrap
index ac821b8..54ae85d 100755
--- a/debootstrap
+++ b/debootstrap
@@ -517,6 +517,8 @@ if am_doing_phase maketarball; then
 fi
 
 if am_doing_phase first_stage; then
+	choose_extractor
+
 	# first stage sets up the chroot -- no calls should be made to
 	# "chroot $TARGET" here; but they should be possible by the time it's
 	# finished
diff --git a/functions b/functions
index 66021e8..e2a9f73 100644
--- a/functions
+++ b/functions
@@ -717,7 +717,22 @@ get_debs () {
 
 ################################################################ extraction
 
-extract_deb_field () {
+# Native dpkg-deb based extractors
+extract_dpkg_deb_field () {
+	local pkg="$1"
+	local field="$2"
+
+	dpkg-deb -f "$pkg" "$field"
+}
+
+extract_dpkg_deb_data () {
+	local pkg="$1"
+
+	dpkg-deb --fsys-tarfile "$pkg" | tar -xf -
+}
+
+# Raw .deb extractors
+extract_ar_deb_field () {
 	local pkg="$1"
 	local field="$2"
 
@@ -726,7 +741,7 @@ extract_deb_field () {
 	    grep -i "^$field:" | sed -e 's/[^:]*: *//' | head -n 1
 }
 
-extract_deb_data () {
+extract_ar_deb_data () {
 	local pkg="$1"
 	local tarball=$(ar -t "$pkg" | grep "^data.tar.[bgx]z")
 
@@ -744,6 +759,28 @@ extract_deb_data () {
 	fi
 }
 
+choose_extractor () {
+	local extractor
+
+	if type dpkg-deb >/dev/null 2>&1; then
+		extractor="dpkg-deb"
+	else
+		extractor="ar"
+	fi
+
+	info CHOSENEXTRACTOR "Chosen extractor for .deb packages: %s" "$extractor"
+	case "$extractor" in
+	dpkg-deb)
+		extract_deb_field () { extract_dpkg_deb_field "$@"; }
+		extract_deb_data () { extract_dpkg_deb_data "$@"; }
+		;;
+	ar)
+		extract_deb_field () { extract_ar_deb_field "$@"; }
+		extract_deb_data () { extract_ar_deb_data "$@"; }
+		;;
+	esac
+}
+
 extract () { (
 	cd "$TARGET"
 	local p=0 cat_cmd
-- 
1.6.5.3

