/*******************************************************************************
 * Copyright (c) 2018 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package net.wasdev.wlp.maven.extensions.thin;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.zip.ZipException;

import org.apache.maven.model.Plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;

import com.ibm.ws.app.manager.springboot.util.SpringBootThinUtil;

@Mojo(name = "thin", defaultPhase = LifecyclePhase.PACKAGE, requiresProject = true, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class SpringBootMavenThinPluginMojo extends AbstractMojo {

	@Parameter(defaultValue = "${project}")
	private MavenProject project;

	/**
	 * Directory containing the generated archive.
	 * 
	 * @since 1.0
	 */
	@Parameter(defaultValue = "${project.build.directory}", required = true)
	private File outputDirectory;

	/**
	 * Name of the generated archive.
	 * 
	 * @since 1.0
	 */
	@Parameter(defaultValue = "${project.build.finalName}", required = true)
	private String finalName;

	/**
	 * Library index cache as a Directory
	 */
	@Parameter(property = "putLibCacheInDirectory", defaultValue = "false")
	private boolean putLibCacheInDirectory;

	/**
	 * Classifier to add to the artifact generated. If given, the artifact will be
	 * attached with that classifier and the main artifact will be deployed as the
	 * main artifact. If this is not given (default), it will replace the main
	 * artifact and only the repackaged artifact will be deployed. Attaching the
	 * artifact allows to deploy it alongside to the original one, see <a href=
	 * "http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html"
	 * > the maven documentation for more details</a>.
	 * 
	 * @since 1.0
	 */
	@Parameter
	private String classifier;

	@Override
	public void execute() throws MojoExecutionException, MojoFailureException {
		try {
			File sourceFatJar = getTargetFile();
			thin(sourceFatJar);
		} catch (IOException | NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void thin(File sourceFatJar) throws ZipException, IOException, NoSuchAlgorithmException {	
		File targetThinJar = new File(outputDirectory, "thin-" + finalName + "." + getArtifactExtension());
		String libFile = "libIndexCache";
		if (putLibCacheInDirectory) {
			libFile += "-" + finalName;
		} else {
			libFile += "-" + finalName + ".zip";
		}
		File libIndexCache = new File(outputDirectory, libFile);
		getLog().info("Thinning " + getArtifactExtension() + ": "+ targetThinJar.getAbsolutePath());
		getLog().info("Lib index cache: "+ libIndexCache.getAbsolutePath());

		SpringBootThinUtil thinUtil = new SpringBootThinUtil(sourceFatJar, targetThinJar, libIndexCache,
				putLibCacheInDirectory);
		thinUtil.execute();
	}

	private File getTargetFile() {
		String classifier = (this.classifier == null ? "" : this.classifier.trim());
		if (!classifier.isEmpty() && !classifier.startsWith("-")) {
			classifier = "-" + classifier;
		}
		if (!this.outputDirectory.exists()) {
			this.outputDirectory.mkdirs();
		}

		return new File(this.outputDirectory, this.finalName + classifier + "." + getArtifactExtension());
	}

	private String getArtifactExtension() {
		return  this.project.getArtifact().getArtifactHandler().getExtension();
	}

}

@@ -692,87 +692,26 @@
 til.
-List;%0Aimport java.util.zip.ZipException;%0A%0Aimport org.apache.maven.model.Plugin;
+zip.ZipException;%0A
 %0Aimp
@@ -864,61 +864,8 @@
 on;%0A
-import org.apache.maven.plugins.annotations.Execute;%0A
 impo
@@ -1867,172 +1867,8 @@
 e;%0A%0A
-%09/**%0A%09 * Library index cache as a Directory%0A%09 */%0A%09@Parameter(property = %22putLibCacheInDirectory%22, defaultValue = %22false%22)%0A%09private boolean putLibCacheInDirectory;%0A%0A
 %09/**
@@ -2697,65 +2697,56 @@
 %0A%09%09%09
-// TODO Auto-generated catch block%0A%09%09%09e.printStackTrac
+throw new MojoExecutionException(e.getMessag
 e()
+)
 ;%0A%09%09
@@ -2990,103 +2990,10 @@
 ache
+-
 %22
-;%0A%09%09if (putLibCacheInDirectory) %7B%0A%09%09%09libFile += %22-%22 + finalName;%0A%09%09%7D else %7B%0A%09%09%09libFile += %22-%22 
 + fi
@@ -3010,20 +3010,16 @@
 %22.zip%22;%0A
-%09%09%7D%0A
 %09%09File l
@@ -3235,17 +3235,16 @@
 ath());%0A
-%0A
 %09%09Spring
@@ -3337,35 +3337,14 @@
 che,
-%0A%09%09%09%09putLibCacheInDirectory
+ false
 );%0A%09
@@ -3657,17 +3657,16 @@
 ();%0A%09%09%7D%0A
-%0A
 %09%09return

Remove suppport of putLibCacheInDirectory