+/* Scan what the user has entered looking for:
+ *
+ * client=<client-name>
+ *
+ * if error or not found, put up a list of client DBRs
+ * to choose from.
+ *
+ * returns: 0 on error
+ * 1 on success and fills in CLIENT_DBR
+ */
+int get_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+{
+ int i;
+
+ if (cr->Name[0]) { /* If name already supplied */
+ if (db_get_client_record(ua->db, cr)) {
+ return 1;
+ }
+ bsendmsg(ua, _("Could not find Client %s: ERR=%s"), cr->Name, db_strerror(ua->db));
+ }
+ for (i=1; i<ua->argc; i++) {
+ if (strcasecmp(ua->argk[i], _("client")) == 0 && ua->argv[i]) {
+ bstrncpy(cr->Name, ua->argv[i], sizeof(cr->Name));
+ if (!db_get_client_record(ua->db, cr)) {
+ bsendmsg(ua, _("Could not find Client %s: ERR=%s"), ua->argv[i],
+ db_strerror(ua->db));
+ cr->ClientId = 0;
+ break;
+ }
+ return 1;
+ }
+ }
+ if (!select_client_dbr(ua, cr)) { /* try once more by proposing a list */
+ return 0;
+ }
+ return 1;
+}
+
+/*
+ * Select a Client record from the catalog
+ * Returns 1 on success
+ * 0 on failure
+ */
+int select_client_dbr(UAContext *ua, CLIENT_DBR *cr)
+{
+ CLIENT_DBR ocr;
+ char name[MAX_NAME_LENGTH];
+ int num_clients, i;
+ uint32_t *ids;
+
+
+ cr->ClientId = 0;
+ if (!db_get_client_ids(ua->db, &num_clients, &ids)) {
+ bsendmsg(ua, _("Error obtaining client ids. ERR=%s\n"), db_strerror(ua->db));
+ return 0;
+ }
+ if (num_clients <= 0) {
+ bsendmsg(ua, _("No clients defined. Run a job to create one.\n"));
+ return 0;
+ }
+
+ start_prompt(ua, _("Defined Clients:\n"));
+ for (i=0; i < num_clients; i++) {
+ ocr.ClientId = ids[i];
+ if (!db_get_client_record(ua->db, &ocr)) {
+ continue;
+ }
+ add_prompt(ua, ocr.Name);
+ }
+ free(ids);
+ if (do_prompt(ua, _("Select the Client"), name, sizeof(name)) < 0) {
+ return 0;
+ }
+ memset(&ocr, 0, sizeof(ocr));
+ bstrncpy(ocr.Name, name, sizeof(ocr.Name));
+
+ if (!db_get_client_record(ua->db, &ocr)) {
+ bsendmsg(ua, _("Could not find Client %s: ERR=%s"), name, db_strerror(ua->db));
+ return 0;
+ }
+ memcpy(cr, &ocr, sizeof(ocr));
+ return 1;
+}
+
+
+