/**
 * The MIT License (MIT)
 *
 * Copyright (C) 2014 scd4j scd4j.tools@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.datamaio.scd4j.util;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.datamaio.scd4j.cmd.Command;
import com.datamaio.scd4j.conf.Configuration;

/**
 * 
 * @author Fernando Rubbo
 */
public final class PathHelper {
	private Map<String, Object> properties;
	private Path module;

	public PathHelper(Configuration conf){
		this(conf.getProps(), conf.getModule());
	}
	
	PathHelper(Map<String, Object> properties, Path module){
		this.properties = properties;
		this.module = module;
	}
	
	public final Path getTargetWithoutSuffix(Path path, String sufix) {
		// remove o .delete do final
		String fileName = path.getFileName().toString();
		fileName = fileName.replaceAll(sufix, "");
		path = path.resolveSibling(fileName);
		return getTarget(path);
	}
	
	public final Path getTarget(Path path) {
		// gets the destination path, based on module
		final Path relativized = this.module.relativize(path);
		final Path resolved = Paths.get("/").resolve(relativized);

		// resolve the variables in the directories
		return replaceVars(resolved);
    }

	public final Path replaceVars(Path path) {
    	String resolvedPath = replaceVars(path.toString());
    	return Paths.get(resolvedPath);		
	}
	
	public String replaceVars(String srcPath) {
		final Pattern p = Pattern.compile("@([^@])*@");
    	final Matcher m = p.matcher(srcPath);
    	while(m.find()) {
    		final String key = m.group();
			final Object value = properties.get(key.replaceAll("@", ""));
			if(value!=null)
				srcPath = srcPath.replace(key, value.toString());
			else 
				throw new IllegalStateException("Variable " + key.replaceAll("@", "") + " was not declared.");
    	}
		return srcPath;
	}
}

@@ -1209,24 +1209,46 @@
 4j.util;%0D%0A%0D%0A
+import java.io.File;%0D%0A
 import java.
@@ -1387,48 +1387,8 @@
 %0D%0A%0D%0A
-import com.datamaio.scd4j.cmd.Command;%0D%0A
 impo
@@ -1439,17 +1439,16 @@
 %0A/**%0D%0A *
- 
 %0D%0A * @au
@@ -1515,16 +1515,22 @@
 private 
+final 
 Map%3CStri
@@ -1563,16 +1563,22 @@
 private 
+final 
 Path mod
@@ -1604,16 +1604,22 @@
 hHelper(
+final 
 Configur
@@ -1680,17 +1680,16 @@
 );%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09PathH
@@ -1694,16 +1694,22 @@
 hHelper(
+final 
 Map%3CStri
@@ -1731,16 +1731,22 @@
 perties,
+ final
  Path mo
@@ -1811,25 +1811,24 @@
 odule;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09public fi
@@ -1869,16 +1869,22 @@
 th path,
+ final
  String 
@@ -2091,25 +2091,24 @@
 path);%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09public fi
@@ -2126,16 +2126,22 @@
 tTarget(
+final 
 Path pat
@@ -2437,16 +2437,22 @@
 aceVars(
+final 
 Path pat
@@ -2555,17 +2555,14 @@
 th);
-%09%09
 %0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09p
@@ -2710,16 +2710,17 @@
   %09while
+ 
 (m.find(
@@ -2837,27 +2837,221 @@
 %09%09if
+ 
 (value
+ 
 !=
+ 
 null)
+ %7B
 %0D%0A%09%09%09
-%09
+    Path valuePath = Paths.get(value.toString());%0D%0A%09%09%09    while (valuePath.getRoot() != null) %7B%0D%0A%09%09%09        valuePath = valuePath.getRoot().relativize(valuePath);%0D%0A%09%09%09    %7D%0D%0A                
 srcP
@@ -3078,24 +3078,28 @@
 e(key, value
+Path
 .toString())
@@ -3101,26 +3101,83 @@
 ng()
-);%0D%0A%09%09%09
+.replace(File.separatorChar, '/'));%0D%0A            %7D 
 else 
+%7B
 %0D%0A
-%09%09%09%09
+                
 thro
@@ -3264,24 +3264,39 @@
 clared.%22);%0D%0A
+            %7D%0D%0A
     %09%7D%0D%0A%09%09re

Fix for root folder in @someVar@ paths.