Index: trunk/ippTools/src/receivetool.c
===================================================================
--- trunk/ippTools/src/receivetool.c	(revision 23894)
+++ trunk/ippTools/src/receivetool.c	(revision 23898)
@@ -122,4 +122,8 @@
     PXOPT_COPY_S64(config->args, where, "-comment", "receiveSource.comment", "LIKE");
 
+    // optional
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+
     psString query = pxDataGet("receivetool_list.sql");
     if (!query) {
@@ -136,4 +140,10 @@
     psFree(where);
 
+    if (limit) {
+        psString limitString = psDBGenerateLimitSQL(limit);
+        psStringAppend(&query, " %s", limitString);
+        psFree(limitString);
+    }
+
     if (!p_psDBRunQueryF(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "Database error");
@@ -153,5 +163,5 @@
         return true;
     }
-    if (!ippdbPrintMetadatas(stdout, output, "receiveSource", true)) {
+    if (!ippdbPrintMetadatas(stdout, output, "receiveSource", !simple)) {
         psError(PS_ERR_UNKNOWN, false, "Failed to print array");
         psFree(output);
@@ -175,8 +185,17 @@
     }
 
+    psString query = pxDataGet("receivetool_addfileset.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "Failed to retreive SQL statement");
+        return false;
+    }
+
     if (!psDBTransaction(config->dbh)) {
         psError(PS_ERR_UNKNOWN, false, "Database error");
         return false;
     }
+
+    psString source_id_str = NULL;      // source_id as a string
+    psStringAppend(&source_id_str, "%" PRId64, source_id);
 
     psListIterator *iter = psListIteratorAlloc(filesets->data.list, PS_LIST_HEAD, false); // Iterator
@@ -184,16 +203,46 @@
     while ((item = psListGetAndIncrement(iter))) {
         psAssert(item && item->data.V && item->type == PS_DATA_STRING, "Argument is bad");
-
-        if (!receiveFilesetInsert(config->dbh, 0, source_id, item->data.str)) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to add fileset");
+        const char *fileset = item->data.str; // Fileset name
+
+        if (!p_psDBRunQueryF(config->dbh, query, source_id_str, fileset)) {
+            psError(PS_ERR_UNKNOWN, false, "Database error");
+            psFree(source_id_str);
+            psFree(query);
+            psFree(iter);
             if (!psDBTransaction(config->dbh)) {
                 psError(PS_ERR_UNKNOWN, false, "Database error");
-                return false;
             }
+            return false;
+        }
+
+        psArray *output = p_psDBFetchResult(config->dbh); // Output of query
+        if (!output) {
+            psError(PS_ERR_UNKNOWN, false, "Database error");
+            psFree(source_id_str);
+            psFree(query);
             psFree(iter);
             return false;
         }
+        if (psArrayLength(output) > 0) {
+            psTrace("receivetool", PS_LOG_INFO, "Fileset %s is already present", fileset);
+            psFree(output);
+            continue;
+        }
+        psFree(output);
+
+        if (!receiveFilesetInsert(config->dbh, 0, source_id, fileset)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to add fileset");
+            psFree(source_id_str);
+            psFree(query);
+            psFree(iter);
+            if (!psDBTransaction(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "Database error");
+            }
+            return false;
+        }
     }
     psFree(iter);
+    psFree(query);
+    psFree(source_id_str);
 
     if (!psDBCommit(config->dbh)) {
