package com.crowdin.cli.commands.functionality;

import com.crowdin.cli.properties.CliProperties;
import com.crowdin.cli.properties.Params;
import com.crowdin.cli.properties.PropertiesBean;
import com.crowdin.cli.utils.file.FileReader;

import java.io.File;
import java.nio.file.Files;

public class PropertiesBuilder {

    private File configFile;
    private File identityFile;
    private Params params;

    public PropertiesBuilder(File configFile, File identityFile, Params params) {
        this.configFile = configFile;
        this.identityFile = identityFile;
        this.params = params;
    }

    public PropertiesBean build() {
        PropertiesBean pb = (!(Files.notExists(configFile.toPath()) && params != null))
            ? CliProperties.buildFromMap(new FileReader().readCliConfig(configFile))
            : new PropertiesBean();
        if (identityFile != null) {
            CliProperties.populateWithCredentials(pb, new FileReader().readCliConfig(identityFile));
        }
        if (params != null) {
            CliProperties.populateWithParams(pb, params);
        }
        String basePathIfEmpty = (Files.exists(configFile.toPath()) && !(params != null && params.getBasePathParam() != null))
            ? new File(configFile.getAbsolutePath()).getParent()
            : "";
        return CliProperties.processProperties(pb, basePathIfEmpty);
    }
}

@@ -228,16 +228,61 @@
 eReader;
+%0Aimport org.apache.commons.lang3.StringUtils;
 %0A%0Aimport
@@ -323,16 +323,44 @@
 e.Files;
+%0Aimport java.nio.file.Paths;
 %0A%0Apublic
@@ -1112,72 +1112,77 @@
     
-CliProperties.populateWithParams(pb, params);%0A 
+if (new File(params.getSourceParam()).isAbsolute()) %7B%0A
        
-%7D%0A
         
 Stri
@@ -1181,126 +1181,302 @@
     
-String basePathIfEmpty = (Files.exists(configFile.toPath()) && !(params != null && params.getBasePathParam() != null))
+ pb.setPreserveHierarchy(false);%0A                pb.setBasePath(configFile.toPath().getRoot().toString());%0A                params.setSourceParam(StringUtils.removePattern(params.getSourceParam(), %22%5E(%5Ba-zA-Z%5D:)?%5B%5C%5C%5C%5C/%5D+%22));%0A            %7D%0A            CliProperties.populateWithParams(pb, params);
 %0A   
@@ -1480,21 +1480,50 @@
         
+%7D%0A
     
-?
+    String basePathIfEmpty =
  new Fil
@@ -1569,25 +1569,8 @@
 nt()
-%0A            : %22%22
 ;%0A  

Fix absolute paths to sources by config params