/*
 * Copyright 2012 James Moger
 *
 * 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 org.moxie.ant;

import java.io.File;
import java.util.Iterator;

import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.resources.FileResource;
import org.moxie.Build;
import org.moxie.Dependency;
import org.moxie.Pom;
import org.moxie.utils.FileUtils;
import org.moxie.utils.StringUtils;


public class MxInstall extends MxTask {
	
	public MxInstall() {
		super();
		setTaskName("mx:install");
	}
	
	public void execute() {
		Build build = getBuild();
		
		File sourceFolder = build.getConfig().getTargetFolder();
		
		Pom pom = build.getPom();
		String artifact = pom.artifactId + "-" + pom.version;
		String pattern = artifact + "*";
		
		Dependency asDependency = new Dependency(pom.getCoordinates());
		File moxieFile = build.getSolver().getMoxieCache().getArtifact(asDependency, asDependency.type);
		File destinationFolder = moxieFile.getParentFile();
		
		getConsole().title(getClass(), artifact);

		FileSet sourceFileset = new FileSet();
		sourceFileset.setProject(getProject());
		sourceFileset.setDir(sourceFolder);
		sourceFileset.setIncludes(pattern);
		
		// copy artifacts
		Copy copy = new Copy();
		copy.setTaskName(getTaskName());
		copy.setProject(getProject());
		copy.setTodir(destinationFolder);
		copy.setVerbose(isVerbose());
		copy.add(sourceFileset);
		copy.execute();

		// output pom file
		getConsole().log("generating pom for {0}", artifact);
		File pomFile = new File(destinationFolder, artifact + ".pom");
		FileUtils.writeContent(pomFile, pom.toXML(false, false));
		getConsole().debug(1, "wrote {0}", pomFile);
		
		// calculate checksums for each file
		getConsole().log("calculating checksums for installed artifacts");
		FileSet repoSet = new FileSet();
		repoSet.setProject(getProject());
		repoSet.setDir(destinationFolder);
		repoSet.setIncludes(pattern);
		repoSet.setExcludes("*.sha1, *.md5, *.sig, *.asc");
		Iterator<?> itr = repoSet.iterator();
		while (itr.hasNext()) {
			FileResource file = (FileResource) itr.next();
			byte [] bytes = FileUtils.readContent(file.getFile());
			
			// calculate the SHA1 hash of the content and save result
			String sha1 = StringUtils.getSHA1(bytes);
			File sha1File = new File(destinationFolder, file.getFile().getName() + ".sha1");
			FileUtils.writeContent(sha1File, sha1);
			getConsole().debug(1, "wrote {0}", sha1File);

			// calculate the MD5 hash of the content and save result
			String md5 = StringUtils.getMD5(bytes);
			File md5File = new File(destinationFolder, file.getFile().getName() + ".md5");
			FileUtils.writeContent(md5File, md5);
			getConsole().debug(1, "wrote {0}", md5File);
		}
	}
}

@@ -853,16 +853,45 @@
 Build;%0D%0A
+import org.moxie.Constants;%0D%0A
 import o
@@ -908,24 +908,24 @@
 ependency;%0D%0A
-
 import org.m
@@ -1051,24 +1051,194 @@
 xTask %7B%0D%0A%09%0D%0A
+%09private File baseFolder;%0D%0A%09%0D%0A%09public void setBaseFolder(File folder) %7B%0D%0A%09%09this.baseFolder = folder;%0D%0A%09%7D%0D%0A%09%0D%0A%09public File getBaseFolder() %7B%0D%0A%09%09return baseFolder;%0D%0A%09%7D%0D%0A%09%0D%0A
 %09public MxIn
@@ -1609,24 +1609,116 @@
 inates());%0D%0A
+%09%09File destinationFolder;%0D%0A%09%09if (baseFolder == null) %7B%0D%0A%09%09%09// install to local repository%0D%0A%09
 %09%09File moxie
@@ -1803,29 +1803,278 @@
 y.type);%0D%0A%09%09
-File 
+%09destinationFolder = moxieFile.getParentFile();%0D%0A%09%09%7D else %7B%0D%0A%09%09%09// install to specified repository%0D%0A%09%09%09String path = Dependency.getMavenPath(asDependency, asDependency.type, Constants.MAVEN2_PATTERN);%0D%0A%09%09%09File moxieFile = new File(baseFolder, path);%0D%0A%09%09%09
 destinationF
@@ -2105,24 +2105,29 @@
 entFile();%0D%0A
+%09%09%7D%0D%0A
 %09%09%0D%0A%09%09getCon

Allow setting base folder for mx:install