Better, more robust "builddeb" script for "make deb-pkg".

 * Test if the required tools (dpkg-deb and fakeroot) are available.
 * Obtain real user name and $EMAIL, if available, rather than just "user@host.domain".
 * Don't overwrite debian/changelog and debian/control files if they already exist.
 * Package is named "linux-image-$version" and provides "linux-image" and other virtual packages.
 * Wrap "dpkg-deb --build" with fakeroot, because actually running "make deb-pkg" as root or with
   fakeroot causes a few files to be rebuilt and the userid of the true kernel compiler to be lost.

diff --git a/scripts/package/builddeb b/scripts/package/builddeb
index ba6bf5d..8e5a21c 100644
--- a/scripts/package/builddeb
+++ b/scripts/package/builddeb
@@ -11,11 +11,25 @@
 
 set -e
 
+# Test if the required tools are available
+if ! type dpkg-deb > /dev/null ; then
+  echo "$0: dpkg-deb not found -- not a Debian system?"
+  exit 1
+fi
+if ! type fakeroot > /dev/null ; then
+  echo "$0: fakeroot not found -- please install it"
+  exit 1
+fi
+
 # Some variables and settings used throughout the script
 version=$KERNELRELEASE
 revision=`cat .version`
+
+# version without LOCALVERSION
+changelog_version=$KERNELVERSION
+
 tmpdir="$objtree/debian/tmp"
-packagename=linux-$version
+packagename=linux-image-$version
 
 if [ "$ARCH" == "um" ] ; then
 	packagename=user-mode-linux-$version
@@ -23,9 +37,9 @@ fi
 
 # Setup the directory structure
 rm -rf "$tmpdir"
-mkdir -p "$tmpdir/DEBIAN" "$tmpdir/lib" "$tmpdir/boot"
+mkdir -p "$tmpdir/DEBIAN" "$tmpdir/lib" "$tmpdir/boot" "$tmpdir/usr/share/doc/$packagename"
 if [ "$ARCH" == "um" ] ; then
-	mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/share/doc/$packagename" "$tmpdir/usr/bin"
+	mkdir -p "$tmpdir/usr/lib/uml/modules/$version" "$tmpdir/usr/bin"
 fi
 
 # Build and install the kernel
@@ -63,20 +77,24 @@ EOF
 	chmod 755 "$tmpdir/DEBIAN/$script"
 done
 
-name="Kernel Compiler <$(id -nu)@$(hostname -f)>"
+test -z "$EMAIL" && EMAIL="$(id -nu)@$(hostname -f)"
+name="$(getent passwd $(id -nu) | cut -d: -f5) <$EMAIL>"
+
 # Generate a simple changelog template
-cat <<EOF > debian/changelog
-linux ($version-$revision) unstable; urgency=low
+test -f debian/changelog || cat <<EOF > debian/changelog
+linux ($changelog_version-$revision) unstable; urgency=low
 
   * A standard release
 
  -- $name  $(date -R)
 EOF
 
+cp debian/changelog "$tmpdir/usr/share/doc/$packagename/changelog"
+
 # Generate a control file
 if [ "$ARCH" == "um" ]; then
 
-cat <<EOF > debian/control
+test -f debian/control || cat <<EOF > debian/control
 Source: linux
 Section: base
 Priority: optional
@@ -84,7 +102,7 @@ Maintainer: $name
 Standards-Version: 3.6.1
 
 Package: $packagename
-Provides: kernel-image-$version, linux-image-$version
+Provides: linux-image, linux-image-$VERSION.$PATCHLEVEL, linux-image-$VERSION.$PATCHLEVEL.$SUBLEVEL
 Architecture: any
 Description: User Mode Linux kernel, version $version
  User-mode Linux is a port of the Linux kernel to its own system call
@@ -98,7 +116,7 @@ Description: User Mode Linux kernel, version $version
 EOF
 
 else
-cat <<EOF > debian/control
+test -f debian/control || cat <<EOF > debian/control
 Source: linux
 Section: base
 Priority: optional
@@ -106,7 +124,7 @@ Maintainer: $name
 Standards-Version: 3.6.1
 
 Package: $packagename
-Provides: kernel-image-$version, linux-image-$version
+Provides: linux-image, linux-image-$VERSION.$PATCHLEVEL, linux-image-$VERSION.$PATCHLEVEL.$SUBLEVEL
 Architecture: any
 Description: Linux kernel, version $version
  This package contains the Linux kernel, modules and corresponding other
@@ -114,13 +132,20 @@ Description: Linux kernel, version $version
 EOF
 fi
 
-# Fix some ownership and permissions
-chown -R root:root "$tmpdir"
-chmod -R go-w "$tmpdir"
-
 # Perform the final magic
 dpkg-gencontrol -isp
-dpkg --build "$tmpdir" ..
+(cd "$tmpdir"; find . -type f ! -regex '.*/DEBIAN/.*' -printf '%P\0' | xargs -r0 md5sum > DEBIAN/md5sums)
+
+# Fix permissions
+chmod -R go-w "$tmpdir"
+
+# Build the package
+# (fakeroot defaults to giving all "unknown" files uid=gid=0,
+#  so dpkg-deb will see the right ownership by default)
+fakeroot -- dpkg-deb --build "$tmpdir" ..
+
+# Clean up
+rm -rf "$tmpdir"
 
 exit 0
 

