+ if (command->mode & NOTMUCH_COMMAND_DATABASE_EARLY) {
+ char *status_string = NULL;
+ notmuch_database_mode_t mode;
+ notmuch_status_t status;
+
+ if (command->mode & NOTMUCH_COMMAND_DATABASE_WRITE ||
+ command->mode & NOTMUCH_COMMAND_DATABASE_CREATE)
+ mode = NOTMUCH_DATABASE_MODE_READ_WRITE;
+ else
+ mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
+
+ if (command->mode & NOTMUCH_COMMAND_DATABASE_CREATE) {
+ status = notmuch_database_create_with_config (NULL,
+ config_file_name,
+ NULL,
+ ¬much,
+ &status_string);
+ if (status && status != NOTMUCH_STATUS_DATABASE_EXISTS) {
+ if (status_string) {
+ fputs (status_string, stderr);
+ free (status_string);
+ }
+
+ if (status == NOTMUCH_STATUS_NO_CONFIG)
+ fputs ("Try running 'notmuch setup' to create a configuration.", stderr);
+
+ return EXIT_FAILURE;
+ }
+ }
+
+ if (notmuch == NULL) {
+ status = notmuch_database_open_with_config (NULL,
+ mode,
+ config_file_name,
+ NULL,
+ ¬much,
+ &status_string);
+ if (status) {
+ if (status_string) {
+ fputs (status_string, stderr);
+ free (status_string);
+ }
+
+ return EXIT_FAILURE;
+ }
+ }
+ }
+
+ if (command->mode & NOTMUCH_COMMAND_CONFIG_LOAD) {
+ char *status_string = NULL;
+ notmuch_status_t status;
+ status = notmuch_database_load_config (NULL,
+ config_file_name,
+ NULL,
+ ¬much,
+ &status_string);
+
+ if (status == NOTMUCH_STATUS_NO_CONFIG && ! (command->mode & NOTMUCH_COMMAND_CONFIG_CREATE)) {
+ fputs ("Try running 'notmuch setup' to create a configuration.", stderr);
+ goto DONE;
+ }
+ switch (status) {
+ case NOTMUCH_STATUS_NO_CONFIG:
+ if (! (command->mode & NOTMUCH_COMMAND_CONFIG_CREATE)) {
+ fputs ("Try running 'notmuch setup' to create a configuration.", stderr);
+ goto DONE;
+ }
+ break;
+ case NOTMUCH_STATUS_NO_DATABASE:
+ if (! command_name) {
+ printf ("Notmuch is configured, but no database was found.\n");
+ printf ("You probably want to run \"notmuch new\" now to create a database.\n\n"
+ "Note that the first run of \"notmuch new\" can take a very long time\n"
+ "and that the resulting database will use roughly the same amount of\n"
+ "storage space as the email being indexed.\n\n");
+ status = NOTMUCH_STATUS_SUCCESS;
+ goto DONE;
+ }
+ break;
+ case NOTMUCH_STATUS_SUCCESS:
+ break;
+ default:
+ goto DONE;
+ }
+