/**
 * 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.hooks.file;

import static com.datamaio.scd4j.conf.Configuration.HOOK_SUFFIX;
import static com.datamaio.scd4j.conf.Configuration.MODULES_FOLDER;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.datamaio.scd4j.conf.Configuration;
import com.datamaio.scd4j.hooks.HookEvaluator;
import com.datamaio.scd4j.util.PathHelper;

/**
 * 
 * @author Fernando Rubbo
 */
public class FileHookEvaluator extends HookEvaluator {
    private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
    private Path src;
    private PathHelper pathHelper;
    
	public FileHookEvaluator(final Path src, final Path target, final Configuration conf) {
		super(Paths.get(src + HOOK_SUFFIX), buildBinding(src, target), conf);
		this.src = src;
		this.pathHelper = new PathHelper(conf);
	}

	@Override
	protected String getScriptBaseClass() {
		return FileHook.class.getName();
	}	
	
	public boolean pre(){
		LOGGER.info("INSTALLING: " + relativize() );
		LOGGER.info(" :PRE");		
		boolean pre = super.pre();
		if(!pre) {
			LOGGER.info(" :POST (Skipped)");
		}
		return pre;
	}
	
	public void post(){
		LOGGER.info(" :POST");
		super.post();		
	}
	
	private static Map<String, Object> buildBinding(final Path src, final Path target) {
		Map<String, Object> map = new HashMap<>();
		map.put("src", src.toString());
		map.put("target", target.toString());
		return map;
	}

	private String relativize() {
		String relative = src.toString();
		Path p = Paths.get(relative.substring(relative.indexOf(MODULES_FOLDER + File.separator)));
		String path = p.subpath(p.getNameCount() == 2 ? 1 : 2, p.getNameCount()).toString();
		return pathHelper.replaceVars(path);
	}
}

@@ -1517,24 +1517,64 @@
 .Logger;%0D%0A%0D%0A
+import com.datamaio.scd4j.cmd.Command;%0D%0A
 import com.d
@@ -1800,20 +1800,17 @@
 ator %7B%0D%0A
-    
+%09
 private 
@@ -1844,16 +1844,21 @@
 = Logger
+%0D%0A%09%09%09
 .getLogg
@@ -1889,20 +1889,17 @@
 NAME);%0D%0A
-    
+%09
 private 
@@ -1909,20 +1909,17 @@
 h src;%0D%0A
-    
+%09
 private 
@@ -1946,12 +1946,8 @@
 r;%0D%0A
-    
 %0D%0A%09p
@@ -2004,17 +2004,21 @@
  target,
- 
+%0D%0A%09%09%09
 final Co
@@ -2271,20 +2271,18 @@
 e();%0D%0A%09%7D
-%09
 %0D%0A
-%09
 %0D%0A%09publi
@@ -2296,16 +2296,17 @@
 an pre()
+ 
 %7B%0D%0A%09%09LOG
@@ -2343,17 +2343,16 @@
 tivize()
- 
 );%0D%0A%09%09LO
@@ -2370,18 +2370,16 @@
  :PRE%22);
-%09%09
 %0D%0A%09%09bool
@@ -2406,16 +2406,17 @@
 );%0D%0A%09%09if
+ 
 (!pre) %7B
@@ -2474,25 +2474,24 @@
 n pre;%0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09public vo
@@ -2499,16 +2499,17 @@
 d post()
+ 
 %7B%0D%0A%09%09LOG
@@ -2548,17 +2548,14 @@
 t();
-%09%09
 %0D%0A%09%7D%0D%0A
-%09
 %0D%0A%09p
@@ -2608,33 +2608,37 @@
 (final Path src,
- 
+%0D%0A%09%09%09
 final Path targe
@@ -2926,17 +2926,22 @@
 S_FOLDER
- 
+%0D%0A%09%09%09%09
 + File.s
@@ -2954,16 +2954,18 @@
 or)));%0D%0A
+%0D%0A
 %09%09String
@@ -2986,36 +2986,8 @@
 ath(
-p.getNameCount() == 2 ? 1 : 
 2, p
@@ -3012,24 +3012,26 @@
 oString();%0D%0A
+%0D%0A
 %09%09return pat

Fix for Windows copy.