/**
 * Copyright (C) 2014 Apigee Corporation
 *
 * 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 io.apigee.buildTools.enterprise4g.mavenplugin;

import io.apigee.buildTools.enterprise4g.utils.PackageConfigurer;
import io.apigee.buildTools.enterprise4g.utils.ZipUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.io.FileUtils;

import java.io.File;


/**
 * Goal to upload 4g gateway  bundle on server
 *
 * @author sdey
 * @goal configure
 * @phase package
 */

public class ConfigureMojo extends GatewayAbstractMojo {


	public void execute() throws MojoExecutionException, MojoFailureException {

		if (super.isSkip()) {
			getLog().info("Skipping");
			return;
		}

		Logger logger = LoggerFactory.getLogger(ConfigureMojo.class);
		File configFile = findConfigFile(logger);

		if (configFile != null) {
			configurePackage(logger, configFile);
		}

        String nodeDirPath = super.getBuildDirectory() + "/apiproxy/resources/node/";
        File nodeDir = new File(nodeDirPath);
		if (nodeDir != null && nodeDir.isDirectory()) {
			logger.info("\n\n=============Now zipping node modules================\n\n");

			String[] filesInNodeDir = nodeDir.list();

			for (String fileName:filesInNodeDir) {
                String filePath = nodeDirPath+fileName;
				File dirFile = new File (filePath);
				if (dirFile.isDirectory())
				{
                    logger.info("Zipping "+fileName+" (it is a directory).");
					try {
						ZipUtils zu = new ZipUtils();
						zu.zipDir(new File(filePath + ".zip"),
								dirFile, fileName);
                        FileUtils.deleteDirectory(dirFile);
                    } catch (Exception e) {
						throw new MojoExecutionException(e.getMessage());
					}
				}
			}

		}

		logger.info("\n\n=============Now zipping the App Bundle================\n\n");

		try {
			ZipUtils zu = new ZipUtils();
			zu.zipDir(new File(super.getApplicationBundlePath()),
					new File(super.getBuildDirectory() + "/apiproxy"), "apiproxy");
		} catch (Exception e) {
			throw new MojoExecutionException(e.getMessage());
		}
	}

	private void configurePackage(Logger logger, File configFile) throws MojoExecutionException {
		logger.debug("\n\n=============Now updating the configuration values for the App Bundle================\n\n");
		try {
			if (super.getProfile().getProfileId() != null && super.getProfile().getProfileId() != "") {
				PackageConfigurer.configurePackage(super.getProfile().getProfileId(), configFile);
			} else {
				PackageConfigurer.configurePackage(super.getProfile().getEnvironment(), configFile);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new MojoExecutionException(e.getMessage());
		}
	}

	private File findConfigFile(Logger logger) throws MojoExecutionException {
		File configFile = new File(super.getBaseDirectoryPath() + File.separator + "config.json");

		if (configFile.exists()) {
			return configFile;
		}

		logger.info("No config.json found. Skipping package configuration.");
		return null;
	}
}

@@ -1526,157 +1526,1467 @@
     
-String nodeDirPath = super.getBuildDirectory() + %22/apiproxy/resources/node/%22;%0A        File nodeDir = new File(nodeDirPath);%0A%09%09if (nodeDir != null && 
+logger.info(%22%5Cn%5Cn=============Checking for node.js app================%5Cn%5Cn%22);%0A        //sometimes node.js source lives outside the apiproxy/ in node/ within the base project directory%0A        String externalNodeDirPath = super.getBaseDirectoryPath() + %22/node/%22;%0A        File externalNodeDir = new File(externalNodeDirPath);%0A%0A        //if node.js source is inside apiproxy/ directory, it will be in the build directory%0A        String nodeDirPath = super.getBuildDirectory() + %22/apiproxy/resources/node/%22;%0A        File nodeDir = new File(nodeDirPath);%0A%0A%0A        //if we find files in the node/ directory outside apiproxy/, move into apiproxy/resources/node%0A        //this takes precedence and we will overwrite potentially stale node.js code in  apiproxy/resources/node%0A        if(externalNodeDir.isDirectory())%7B%0A            String%5B%5D filesInExternalNodeDir = externalNodeDir.list();%0A            if(filesInExternalNodeDir.length %3E 0) %7B%0A                logger.info(%22Node.js app code found outside apiproxy/ directory. Moving to target/apiproxy/resources/node (will overwrite).%22);%0A                try %7B%0A                    FileUtils.deleteDirectory(nodeDir);%0A                    FileUtils.copyDirectory(externalNodeDir, nodeDir);%0A                %7Dcatch(Exception e)%7B%0A                    throw new MojoExecutionException(e.getMessage());%0A                %7D%0A            %7D%0A        %7D%0A%0A        //always handle zipping of any directories in apiproxy/resources/node%0A%09%09if (
 node

Enhance support for node.js app code living outside apiproxy/resources/node in node/ folder.