Index: /tea4cups/trunk/tea4cups
===================================================================
--- /tea4cups/trunk/tea4cups (revision 585)
+++ /tea4cups/trunk/tea4cups (revision 587)
@@ -32,4 +32,5 @@
 import tempfile
 import ConfigParser
+import threading
 from struct import unpack
 
@@ -518,4 +519,7 @@
         if self.isTrue(self.getPrintQueueOption(self.PrinterName, "serialize", ignore=1)) :
             self.logDebug("Serialized Tees")
+            if self.RealBackend :
+                self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName))
+                exitcode = self.runOriginalBackend()
             for (branch, command) in branches.items() :
                 self.logDebug("Launching %s : %s" % (branch, command))
@@ -530,4 +534,6 @@
             self.logDebug("Forked Tees")
             pids = {}
+            if self.RealBackend :
+                branches["Original backend"] = None     # Fakes a tee to launch one more child
             for (branch, command) in branches.items() :
                 pid = os.fork()
@@ -535,11 +541,15 @@
                     pids[branch] = pid
                 else :    
-                    self.logDebug("Launching %s : %s" % (branch, command))
-                    retcode = os.system(command)
-                    if os.WIFEXITED(retcode) :
-                        retcode = os.WEXITSTATUS(retcode)
-                    else :    
-                        retcode = -1
-                    sys.exit(retcode)
+                    if branch == "Original backend" :
+                        self.logDebug("Launching original backend %s for printer %s" % (self.RealBackend, self.PrinterName))
+                        sys.exit(self.runOriginalBackend())
+                    else :
+                        self.logDebug("Launching %s : %s" % (branch, command))
+                        retcode = os.system(command)
+                        if os.WIFEXITED(retcode) :
+                            retcode = os.WEXITSTATUS(retcode)
+                        else :    
+                            retcode = -1
+                        sys.exit(retcode)
             for (branch, pid) in pids.items() :
                 (childpid, retcode) = os.waitpid(pid, 0)
@@ -551,4 +561,68 @@
                     exitcode = 1
         return exitcode
+        
+    def runOriginalBackend(self) :    
+        """Launches the original backend."""
+        originalbackend = os.path.join(os.path.split(sys.argv[0])[0], self.RealBackend)
+        arguments = sys.argv
+        self.logDebug("Starting original backend %s with args %s" % (originalbackend, " ".join(['"%s"' % a for a in ([os.environ["DEVICE_URI"]] + arguments[1:])])))
+        subprocess = Popen4ForCUPS([originalbackend] + arguments[1:], bufsize=0, arg0=os.environ["DEVICE_URI"])
+        rendezvous = threading.Event()
+        if self.InputFile is None :
+           self.logDebug("Launching data thread.")
+           infile = open(self.DataFile, "rb")
+           iothread = threading.Thread(target = self.handleOriginalBackendIO, kwargs = { "parent" : threading.currentThread(), "event" : rendezvous, "inf": infile, "outf" : subprocess.tochild})
+           iothread.start()
+           
+        # here we only want to read its output and send it on our stderr
+        while (self.InputFile is not None) or iothread.isAlive() : # until all data transmitted to original backend
+            data = subprocess.fromchild.readline()
+            if (not data) or rendezvous.isSet() :
+                break
+            sys.stderr.write(data)    
+            sys.stderr.flush()
+            
+        if not rendezvous.isSet() :    
+            rendezvous.set()    
+        if self.InputFile is None :
+            infile.close()
+            iothread.join()
+        subprocess.fromchild.close()
+        subprocess.tochild.close()
+        status = subprocess.wait()
+        if os.WIFEXITED(status) :
+            return os.WEXITSTATUS(status)
+        else :    
+            return 1
+        
+    def handleOriginalBackendIO(self, parent, event, inf, outf) :    
+        """Thread to handles the original backend's I/O."""
+        dummy = 0
+        totalsent = 0
+        while 1 :
+            if not parent.isAlive() :
+                self.logInfo("Parent died unexpectedly.", level = "warn")
+                break
+            if event.isSet() :    
+                self.logInfo("Parent said work is finished.")
+                break
+            try :    
+                data = inf.readline()
+                if not data :
+                    break
+                outf.write(data)    
+                totalsent += len(data)
+                dummy += 1
+                if not (dummy % 10) :
+                    self.logDebug("Sent %i bytes to original backend." % totalsent)
+                try :
+                    outf.flush()
+                except :    
+                    pass
+            except IOError, msg :
+                self.logInfo("I/O Error : %s" % msg, level = "warn")
+                break
+        event.set()        
+        sys.exit(0)
         
 if __name__ == "__main__" :    
