diff --git a/getinstdebsid b/getinstdebsid
new file mode 100755
index 0000000..0b9bda0
--- /dev/null
+++ b/getinstdebsid
@@ -0,0 +1,86 @@
+#!/usr/bin/perl -w
+# creates an identifier for each package in <admindir>/status and
+# stores it in <outdir>/<pkg>
+# the following files are used for the id calculation:
+# * <admindir>/status: to get the "control" data for each package
+# * <admindir>/info/<pkg>.* (except <pkg>.list)
+use strict;
+use warnings;
+use Digest::MD5;
+use Data::Dumper;
+
+my $admindir;
+my $outdir;
+
+while (@ARGV) {
+  my $opt = shift @ARGV;
+  if ($opt eq "--admindir") {
+    $admindir = shift @ARGV;
+    next;
+  } elsif ($opt eq "--outdir") {
+    $outdir = shift @ARGV;
+    next;
+  }
+  die("invalid option: $opt\n");
+}
+
+die("--admindir <dir> option is required\n") unless defined($admindir);
+die("--outdir <dir> option is required\n") unless defined($outdir);
+
+sub process_pkg {
+  my ($pkg, @control) = @_;
+  local *F;
+  opendir(DIR, "$admindir/info") || die("$admindir/info: $!\n");
+  my @files = grep { /^\Q$pkg\E$/ || /^\Q$pkg\E\./ } readdir(DIR);
+  closedir(DIR);
+  @files = sort(@files);
+  my @md5s;
+  my $ctx = Digest::MD5->new;
+  $ctx->add(join('', sort(@control)));
+  push @md5s, $ctx->hexdigest;
+  for my $f (@files) {
+    next if $f eq "$pkg.list";
+    open(F, '<', "$admindir/info/$f") || die("$admindir/info/$f: $!\n");
+    $ctx->new;
+    $ctx->addfile(*F);
+    push @md5s, $ctx->hexdigest;
+    close(F);
+  }
+  $ctx->new;
+  $ctx->add($_) for @md5s;
+  open(F, '>', "$outdir/$pkg") || die("$outdir/$pkg: $!\n");
+  print F "$pkg " . $ctx->hexdigest . "\n";
+  close(F);
+}
+
+local *F;
+open(F, '<', "$admindir/status") || die("$admindir: $!\n");
+my @control;
+my $pkg;
+my @status = <F>;
+while (@status) {
+  my $l = shift @status;
+  if ($l eq "\n") {
+    process_pkg($pkg, @control) if defined($pkg);
+    $pkg = undef;
+    @control = ();
+    next;
+  }
+  my ($tag, $data) = split(':', $l, 2);
+  die("unexpected line: \"$l\"\n") unless defined($tag) && defined($data);
+  $data =~ s/^\s*//;
+  $data =~ s/\s*$//;
+  $pkg = $data if $tag eq "Package";
+  if ($tag eq "Status") {
+    # only process correctly installed pkgs
+    $pkg = undef if $data ne "install ok installed";
+  } elsif ($tag eq "Conffiles") {
+    shift @status while (@status && $status[0] =~ /^\s/);
+  } elsif ($tag eq "Description") {
+    push  @control, $l;
+    push @control, shift(@status) while (@status && $status[0] =~ /^ /);
+  } else {
+    push @control, $l;
+  }
+}
+close(F);
diff --git a/init_buildsystem b/init_buildsystem
index b1de006..f9d1f8f 100755
--- a/init_buildsystem
+++ b/init_buildsystem
@@ -622,7 +622,8 @@ fi
 #
 # now test if there is already a build dir.
 #
-if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages ; then
+if test ! -f $BUILD_ROOT/var/lib/rpm/packages.rpm -a ! -f $BUILD_ROOT/var/lib/rpm/Packages \
+   -a ! -f $BUILD_ROOT/var/lib/dpkg/status; then
     mkdir -p $BUILD_ROOT/var/lib/rpm || cleanup_and_exit 1
     mkdir -p $BUILD_ROOT/usr/src/packages/SOURCES || cleanup_and_exit 1
     mkdir -p $BUILD_ROOT/etc || cleanup_and_exit 1
@@ -719,6 +720,8 @@ if test -f $BUILD_ROOT/var/lib/rpm/packages.rpm -o -f $BUILD_ROOT/var/lib/rpm/Pa
 	    echo "$ii" > "$BUILD_ROOT/.init_b_cache/alreadyinstalled/$pp"
 	done
     )
+elif test -f $BUILD_ROOT/var/lib/dpkg/status -a -d $BUILD_ROOT/var/lib/dpkg/info ; then
+    perl $BUILD_DIR/getinstdebsid --admindir $BUILD_ROOT/var/lib/dpkg --outdir $BUILD_ROOT/.init_b_cache/alreadyinstalled;
 fi
 
 #
@@ -749,6 +752,14 @@ rpm_e()
     done
 }
 
+dpkg_purge()
+{
+    # there's no (trivial) way to remove a package if a prerm/postrm script fails
+    # (but its status changes: so we'll try to install it again (I hope dpkg can
+    # handle it..))
+    chroot $BUILD_ROOT dpkg --purge --force all $1
+}
+
 #
 # delete all packages we don't want
 #
@@ -765,8 +776,12 @@ for PKG in $BUILD_ROOT/.init_b_cache/todelete/* ; do
     PKG=${PKG##*/}
     test "$PKG" = "*" && continue
     echo "deleting $PKG"
-    rpm_e "$PKG"
-    check_exit
+    if test "$PSUF" = "deb"; then
+        dpkg_purge "$PKG"
+    else
+        rpm_e "$PKG"
+        check_exit
+    fi
 done
 rm -rf $BUILD_ROOT/.init_b_cache/todelete
 
@@ -786,6 +801,25 @@ RPMCHECKOPTS_HOST=
 test -x $BUILD_ROOT/usr/bin/rpmsign && RPMCHECKOPTS="--nodigest --nosignature"
 test -x /usr/bin/rpmsign && RPMCHECKOPTS_HOST="--nodigest --nosignature"
 
+#
+# get the id of a given deb package (modifies TMPFILE)
+#
+get_deb_id()
+{
+    ar p $1 control.tar.gz | gunzip > $TMPFILE
+    MD5S="$(tar xOf $TMPFILE ./control | perl -e 'print sort(<>);' | md5sum)"
+    MD5S=${MD5S%%  -*}
+    FILES=$(tar --exclude ./control -tf $TMPFILE | sort)
+    for i in $(tar --exclude ./control -tf $TMPFILE | sort); do
+        test "$i" = "./" && continue
+        T=$(tar xOf $TMPFILE $i | md5sum)
+        MD5S="$MD5S${T%%  -*}"
+    done
+    rm -f $TMPFILE
+    MD5=$(echo -n $MD5S | md5sum)
+    echo -n ${MD5%%  -*}
+}
+
 for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL $PACKAGES_TO_CBINSTALL; do
 
     case $PKG in
@@ -798,20 +832,32 @@ for PKG in $PACKAGES_TO_INSTALL_FIRST RUN_LDCONFIG $PACKAGES_TO_INSTALL $PACKAGE
     test -f $BUILD_ROOT/installed-pkg/$PKG && continue
 
     if test $PSUF = deb ; then
+        PKGID="$PKG $(get_deb_id $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb)"
+        if test -f $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG ; then
+            read OLDPKGID < $BUILD_ROOT/.init_b_cache/alreadyinstalled/$PKG
+            if test "$PKGID" != "$OLDPKGID"; then
+                echo deleting unwanted ${OLDPKGID%% *}
+                dpkg_purge "$PKG"
+            else
+                # keep installed pkg
+                echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
+                continue
+            fi
+        fi
 	# debian world, install deb files
 	test -L $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb || continue
 	if ! test "$BUILD_ROOT/.init_b_cache/rpms/$PKG.deb" -ef "$BUILD_ROOT/.init_b_cache/$PKG.deb" ; then
 	    rm -f $BUILD_ROOT/.init_b_cache/$PKG.deb
 	    cp $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb $BUILD_ROOT/.init_b_cache/$PKG.deb || cleanup_and_exit 1
 	fi
-	PKGID=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
-	PKGID="${PKGID##*/}"
-	PKGID="${PKGID%.deb}"
-	echo "installing ${PKGID%_*}"
+	PKGINFO=`readlink $BUILD_ROOT/.init_b_cache/rpms/$PKG.deb`
+	PKGINFO="${PKGINFO##*/}"
+	PKGINFO="${PKGINFO%.deb}"
+	echo "installing ${PKGINFO%_*}"
 	( chroot $BUILD_ROOT dpkg --install --force all .init_b_cache/$PKG.deb 2>&1 || touch $BUILD_ROOT/exit ) | \
 	    perl -ne '$|=1;/^(Configuration file|Installing new config file|Selecting previously deselected|\(Reading database|Unpacking |Setting up|Creating config file|Preparing to replace dpkg)/||/^$/||print'
 	check_exit
-	echo "$PKGID debian" > $BUILD_ROOT/installed-pkg/$PKG
+	echo "$PKGID" > $BUILD_ROOT/installed-pkg/$PKG
 	continue
     fi
 
