--- a/trunk/wharfie/lib/makefile.py
+++ b/trunk/wharfie/lib/makefile.py
@@ -62,7 +62,7 @@
 #
 # Write a Makefile
 #
-def write_makefile(filename, dry_run, installpath='.', incremental=False):
+def write_makefile(filename, dry_run, installpath='.', incremental=False, proc=True):
     backup_levels=list()
     f = open(filename, 'w');
     # write header
@@ -83,12 +83,27 @@
     for target in makeTargets:
         if 'comment' in target:
             f.write("# %s\n" % target['comment']);
-        f.write("%s: %s\n" % (target['name'], " ".join(target['dep'])));
-        # target and host commands have to extract and repack always.
-        # simple commands don't do that.
         if 'trgcmd' in target or 'hostcmd' in target or 'temporary' in target:
-            f.write("\t${Q}-mkdir $$(basename $@ .tar)\n");
-
+            # independent of if the command was successful or not, we cleanup the directory
+            # - killall processes running inside
+            # - umount proc, if necessary
+            # - delete the directory
+            cleanup_commands="${SUDO} fuser -k ./rootfs.piling;" \
+                "[ -d ./rootfs.piling/proc ] && ${SUDO} umount ./rootfs.piling/proc;" \
+                "${SUDO} rm -Rf ./rootfs.piling";
+            f.write("%s: %s\n" % (target['name'], " ".join(target['dep'])));
+            f.write("\t${Q}-mkdir rootfs.piling\n");
+            f.write("\t${Q}${MAKE} %s_sub || (%s; false)\n" % (target['name'], cleanup_commands));
+            f.write("\t${Q}-%s\n" % cleanup_commands);
+            # don't add any dependencies to a subtarget. Resolving them in the sub-process would lead
+            # to really unexpected effects. The sub-target is just called, that we can tidy things up
+            # well in the main build target.
+            f.write("\n");
+            f.write(".PHONY: %s_sub\n" % (target['name']));
+            f.write("%s_sub:\n" % (target['name']));
+            # target and host commands have to extract and repack always.
+            # simple commands don't do that.
+            f.write("\t${Q}${SUDO} rm -f .trg.sh .hst.sh\n");
             if 'trgcmd' in target:
                 cmd = templateTrgCmd % (target['trgcmd'].replace('$', '\\$$').replace('"', '\\"'));
                 f.write("\t${Q}echo '******************************'\n");
@@ -110,7 +125,7 @@
 
             # extracting from multiple backup levels
             backup_levels.append(target["dep"][0])
-            f.write("cd $$(basename $@ .tar);")
+            f.write("cd rootfs.piling;")
             lastincrement = None
             for increment in backup_levels[-1:]:
                 if lastincrement == None:
@@ -125,25 +140,35 @@
                 lastincrement = increment
 
             if not dry_run:
+                # mount proc here if such a folder exists. it is umounted after this command or
+                # in the cleanup actions
+                # - target
+                if proc:
+                    f.write("if [ -d proc ]; then mount -t proc proc proc; fi;");
                 f.write("if [ -f ../.trg.sh ]; then mv ../.trg.sh .; chroot . ./.trg.sh || exit 1; fi; rm -f ./.trg.sh;");
+                if proc:
+                    f.write("if [ -d proc ]; then umount proc || umount -l proc; fi;");
+                # - host
                 f.write("if [ -f ../.hst.sh ]; then ../.hst.sh || exit 1; fi; rm -f ../.hst.sh;");
+
+
+            # pack result
             if not 'temporary' in target or not target['temporary']:
                 # copy over snar file from previous level and add changes to the new one
-                f.write("[ ! -f ../$<.snar ] || cp ../$<.snar ../$@.snar;");
+                f.write("[ ! -f ../%s.snar ] || cp ../%s.snar ../%s.snar;" % (target['dep'][0], target['dep'][0], target['name']));
             if incremental:
-                f.write("tar -g ../$@.snar -cf '../$@' .;");
+                f.write("tar -g ../%s.snar -cf '../%s' .;" % (target['name'], target['name']));
             else:
                 if not 'temporary' in target or not target['temporary']:
-                    f.write("tar -cf '../$@' .;");
+                    f.write("tar -cf '../%s' .;" % target['name']);
             f.write("\"\n");
             # ... end command
             
 
             if 'temporary' in target and target['temporary']:
-                f.write("\t[ ! -f $< ] || cp $< $@;");
-                f.write("\t[ ! -f $<.snar ] || cp $<.snar $@.snar;\n");
+                f.write("\t[ ! -f %s ] || cp %s %s;" % (target['dep'][0], target['dep'][0], target['name']));
+                f.write("\t[ ! -f %s.snar ] || cp %s.snar %s.snar;\n" % (target['dep'][0], target['dep'][0], target['name']));
 
-            f.write("\t${Q}-${SUDO} rm -Rf ./$$(basename $@ .tar)\n");
         #
         # end of target/host commands.
         # we keep it open that those command types can still be combined
@@ -151,6 +176,7 @@
         #
 
         if 'simplecmd' in target:
+            f.write("%s: %s\n" % (target['name'], " ".join(target['dep'])));
             # note, that a simple command has full control
             # over make. it can contain references to the maketarget
             # and to make variables. Therefore we don't replace anything
@@ -163,8 +189,7 @@
         f.write("\n");
 
     # write footer
-    f.write("include %s/wharfie.mk\n" % installpath);
-    if installpath != '.':
-        f.write("-include wharfie.mk\n");
+    f.write("WHARFIE_MK ?= %s/wharfie.mk\n" % installpath);
+    f.write("include ${WHARFIE_MK}\n");
     f.write("\n");