// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.application.preprocessor;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.application.api.DeployLogger;
import com.yahoo.config.model.application.provider.BaseDeployLogger;
import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Zone;
import com.yahoo.yolean.Exceptions;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

/**
 * Main entry for preprocessing an application package.
 *
 * @author Ulf Lilleengen
 */
public class ApplicationPreprocessor {

    private final File applicationDir;
    private final Optional<File> outputDir;
    private final Optional<Environment> environment;
    private final Optional<RegionName> region;

    public ApplicationPreprocessor(File applicationDir, Optional<File> outputDir, Optional<Environment> environment, Optional<RegionName> region) {
        this.applicationDir = applicationDir;
        this.outputDir = outputDir;
        this.environment = environment;
        this.region = region;
    }

    public void run() throws IOException {
        DeployLogger logger = new BaseDeployLogger();
        FilesApplicationPackage.Builder applicationPackageBuilder = new FilesApplicationPackage.Builder(applicationDir);
        outputDir.ifPresent(applicationPackageBuilder::preprocessedDir);
        ApplicationPackage preprocessed = applicationPackageBuilder.build().preprocess(
                new Zone(environment.orElse(Environment.defaultEnvironment()), region.orElse(RegionName.defaultName())),
                logger);
        preprocessed.validateXML();
    }

    public static void main(String args[]) {
        int argCount = args.length;
        if (argCount < 1) {
            System.out.println("Usage: application-preprocessor <application> [environment] [region] [outputDirectory]");
            System.exit(1);
        }
        File applicationDir = new File(args[0]);
        Optional<Environment> environment = (argCount > 1) ? Optional.of(Environment.valueOf(args[1])) : Optional.empty();
        Optional<RegionName> region = (argCount > 2) ? Optional.of(RegionName.from(args[2])) : Optional.empty();
        Optional<File> outputDir = (argCount > 3) ? Optional.of(new File(args[3])) : Optional.empty();
        ApplicationPreprocessor preprocessor = new ApplicationPreprocessor(applicationDir, outputDir, environment, region);
        try {
            preprocessor.run();
            System.out.println("Application preprocessed successfully. Preprocessed application stored in " +
                               new File(applicationDir, FilesApplicationPackage.preprocessed).getAbsolutePath());
        } catch (Exception e) {
            System.err.println("Error validating application package: " + Exceptions.toMessageString(e));
            System.exit(1);
        }
    }

}

@@ -207,62 +207,8 @@
 ge;%0A
-import com.yahoo.config.application.api.DeployLogger;%0A
 impo
@@ -272,16 +272,16 @@
 Logger;%0A
+
 import c
@@ -517,17 +517,16 @@
 ptions;%0A
-%0A
 import j
@@ -1265,62 +1265,8 @@
 n %7B%0A
-        DeployLogger logger = new BaseDeployLogger();%0A
     
@@ -1684,14 +1684,30 @@
     
-l
+new BaseDeployL
 ogger
+()
 );%0A 
@@ -1786,15 +1786,15 @@
 ring
+%5B%5D
  args
-%5B%5D
 ) %7B%0A
@@ -1896,16 +1896,22 @@
 %22Usage: 
+vespa-
 applicat
@@ -1939,16 +1939,29 @@
 lication
+ package path
 %3E %5Benvir
@@ -1988,17 +1988,13 @@
 tput
-Directory
+ path
 %5D%22);
@@ -2663,44 +2663,23 @@
 ully
-. Preprocessed applicatio
+ and writte
 n 
-s
 to
-red in
  %22 +
@@ -2706,24 +2706,41 @@
             
+outputDir.orElse(
 new File(app
@@ -2789,16 +2789,17 @@
 ocessed)
+)
 .getAbso

Fix output of path preprocessed application package is written to