// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.pgm.init;

import com.google.gerrit.lucene.LuceneChangeIndex;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.pgm.init.api.InitStep;
import com.google.gerrit.pgm.init.api.Section;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.index.ChangeSchemas;
import com.google.gerrit.server.index.IndexModule.IndexType;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import java.io.IOException;

/** Initialize the {@code index} configuration section. */
@Singleton
class InitIndex implements InitStep {
  private final ConsoleUI ui;
  private final Section index;
  private final SitePaths site;
  private final InitFlags initFlags;

  @Inject
  InitIndex(ConsoleUI ui,
      Section.Factory sections,
      SitePaths site,
      InitFlags initFlags) {
    this.ui = ui;
    this.index = sections.get("index", null);
    this.site = site;
    this.initFlags = initFlags;
  }

  @Override
  public void run() throws IOException {
    ui.header("Index");

    IndexType type = index.select("Type", "type", IndexType.LUCENE);
    if (site.isNew && type == IndexType.LUCENE) {
      LuceneChangeIndex.setReady(
          site, ChangeSchemas.getLatest().getVersion(), true);
    } else {
      final String message = String.format(
        "\nThe index must be %sbuilt before starting Gerrit:\n"
        + "  java -jar gerrit.war reindex -d site_path\n",
        site.isNew ? "" : "re");
      ui.message(message);
      initFlags.autoStart = false;
    }
  }

  @Override
  public void postRun() throws Exception {
  }
}

@@ -639,16 +639,60 @@
 .init;%0A%0A
+import com.google.common.collect.Iterables;%0A
 import c
@@ -734,16 +734,16 @@
 eIndex;%0A
-
 import c
@@ -1188,16 +1188,109 @@
 ception;
+%0Aimport java.nio.file.DirectoryStream;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;
 %0A%0A/** In
@@ -1520,16 +1520,48 @@
 itFlags;
+%0A  private final Section gerrit;
 %0A%0A  @Inj
@@ -1737,16 +1737,64 @@
  null);%0A
+    this.gerrit = sections.get(%22gerrit%22, null);%0A
     this
@@ -1995,58 +1995,8 @@
 E);%0A
-    if (site.isNew && type == IndexType.LUCENE) %7B%0A
     
@@ -2084,24 +2084,93 @@
 n(), true);%0A
+    if ((site.isNew %7C%7C isEmptySite()) && type == IndexType.LUCENE) %7B%0A
     %7D else %7B
@@ -2393,24 +2393,24 @@
 e(message);%0A
-
       initFl
@@ -2439,24 +2439,270 @@
 %0A    %7D%0A  %7D%0A%0A
+  private boolean isEmptySite() %7B%0A    try (DirectoryStream%3CPath%3E files =%0A        Files.newDirectoryStream(site.resolve(gerrit.get(%22basePath%22)))) %7B%0A      return Iterables.isEmpty(files);%0A    %7D catch (IOException e) %7B%0A      return true;%0A    %7D%0A  %7D%0A%0A
   @Override%0A

Initialize Lucene index automatically at init