Index: pykota/trunk/bin/autopykota
===================================================================
--- pykota/trunk/bin/autopykota (revision 2715)
+++ pykota/trunk/bin/autopykota (revision 2744)
@@ -64,34 +64,47 @@
         else :
             printer = self.storage.getPrinter(printername)
+            if not printer.Exists :
+                self.logdebug("Creating printer %s which doesn't exist yet." % printername)
+                os.system('pkprinters --add --description "printer created from autopykota" "%s"' % printername)
+                printer = self.storage.getPrinterFromBackend(printername)
+                if printer.Exists :
+                    self.logdebug("Printer %s created successfully." % printername)
+                else :    
+                    self.logdebug("Impossible to create printer %s." % printername)
+                printernames = [printername]
+            else :    
+                printernames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
+            
             user = self.storage.getUser(username)
-            if user.Exists :
-                self.logdebug("User %s already exits." % username) 
-                if printer.Exists :
-                    userpquota = self.storage.getUserPQuota(user, printer)
+            if not user.Exists :
+                self.logdebug("Creating user %s which doesn't exist yet." % username)
+                os.system('pkusers --add --limitby balance --balance "%s" --description "user created from autopykota" "%s"' % (options["initbalance"], username))
+                user = self.storage.getUserFromBackend(username)
+                if user.Exists :
+                    self.logdebug("User %s created successfully." % username)
+                else :    
+                    self.logdebug("Impossible to create user %s." % username)
+                
+            if user.Exists and printer.Exists :    
+                userpquota = self.storage.getUserPQuota(user, printer)
+                if not userpquota.Exists :
+                    self.logdebug("Creating a print quota entry for user %s on printers %s" % (username, printernames))
+                    os.system('edpykota --add --printer "%s" "%s"' % (','.join(printernames), username))
+                    userpquota = self.storage.getUserPQuotaFromBackend(user, printer)
                     if userpquota.Exists :
-                        # we should never get there, unless autopykota was launched manually
-                        self.logdebug("User %s's quota entry on printer %s already exists. Nothing to do." % (username, printername))
+                        self.logdebug("User %s's print quota entry on printer %s created successfully." % (username, printername))
                         return 0
                     else :    
-                        printersnames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
-                        self.logdebug("Creating a quota entry for user %s on printers %s" % (username, printersnames))
-                        return os.system('edpykota --add --printer "%s" "%s"' % (','.join(printersnames), username))
+                        self.logdebug("Impossible to create user %s's print quota entry on printer %s." % (username, printername))
+                        return -1
                 else :
-                    self.logdebug("Printer %s doesn't exist. Creating printer %s and a quota entry for user %s on printer %s." % (printername, printername, username, printername))
-                    return os.system('edpykota --add --printer "%s" "%s"' % (printername, username))
-            else :
-                if printer.Exists :
-                    printersnames = [printer.Name] + [p.Name for p in self.storage.getParentPrinters(printer)]
-                else :    
-                    printersnames = [printer.Name]
-                self.logdebug("User %s doesn't exist yet." % username)
-                self.logdebug("Creating user %s's account with balance %s and quota entries on printers %s" % (username, options["initbalance"], printersnames))
-                return os.system('edpykota --add --limitby balance --balance "%s" --printer "%s" "%s"' % (options["initbalance"], ','.join(printersnames), username))
-    
+                    self.logdebug("User %s's print quota entry on printer %s already exists. Nothing to do." % (username, printername))
+                    return 0
+            else :        
+                return -1
+                
 if __name__ == "__main__" :    
     retcode = 0
     try :
-        raise PyKotaCommandLineError, "This version is not stable, download an earlier one. Operation aborted."
-        
         defaults = { \
                      "initbalance" : 0.0
