/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.maven.plugin.resources.remote;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.Comparator;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.resources.remote.io.xpp3.RemoteResourcesBundleXpp3Writer;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.util.DirectoryScanner;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

/**
 * Bundle up resources that should be considered as a remote-resource,
 * generating <code>META-INF/maven/remote-resources.xml</code> descriptor.
 */
@Mojo(name = "bundle", defaultPhase = LifecyclePhase.GENERATE_RESOURCES, threadSafe = true)
public class BundleRemoteResourcesMojo extends AbstractMojo {
    public static final String RESOURCES_MANIFEST = "META-INF/maven/remote-resources.xml";

    private static final String[] DEFAULT_INCLUDES = new String[] {
        "**/*.txt", "**/*.vm",
    };

    /**
     * The directory which contains the resources you want packaged up in this resource bundle.
     */
    @Parameter(defaultValue = "${basedir}/src/main/resources")
    private File resourcesDirectory;

    /**
     * The directory where you want the resource bundle manifest written to.
     */
    @Parameter(defaultValue = "${project.build.outputDirectory}", required = true)
    private File outputDirectory;

    /**
     * A list of files to include. Can contain ant-style wildcards and double wildcards.
     * The default includes are
     * <code>**&#47;*.txt   **&#47;*.vm</code>
     *
     * @since 1.0-alpha-5
     */
    @Parameter
    private String[] includes;

    /**
     * A list of files to exclude. Can contain ant-style wildcards and double wildcards.
     *
     * @since 1.0-alpha-5
     */
    @Parameter
    private String[] excludes;

    /**
     * Encoding of the bundle.
     *
     * @since 1.1
     */
    @Parameter(defaultValue = "${project.build.sourceEncoding}")
    private String sourceEncoding;

    /**
     * List of project properties needed to process Velocity
     * template by this resource bundle.
     *
     * @since 3.3.0
     */
    @Parameter(property = "bundle.requiredProjectProperties")
    private List<String> requiredProjectProperties;

    @Override
    public void execute() throws MojoExecutionException {
        if (!resourcesDirectory.exists()) {
            getLog().info("skip non existing resourceDirectory " + resourcesDirectory.getAbsolutePath());
            return;
        }

        if (sourceEncoding == null || sourceEncoding.isEmpty()) {
            getLog().warn("sourceEncoding has not been set; using platform encoding " + Charset.defaultCharset()
                    + "; i.e. build is platform dependent!");
            sourceEncoding = Charset.defaultCharset().name();
        }

        // Look at the content of the resourcesDirectory and create a manifest of the files
        // so that velocity can easily process any resources inside the JAR that need to be processed.

        RemoteResourcesBundle remoteResourcesBundle = new RemoteResourcesBundle();
        remoteResourcesBundle.setSourceEncoding(sourceEncoding);
        remoteResourcesBundle.setRequiredProjectProperties(requiredProjectProperties);

        DirectoryScanner scanner = new DirectoryScanner();
        scanner.setFilenameComparator(Comparator.naturalOrder());

        scanner.setBasedir(resourcesDirectory);
        if (includes != null && includes.length != 0) {
            scanner.setIncludes(includes);
        } else {
            scanner.setIncludes(DEFAULT_INCLUDES);
        }

        if (excludes != null && excludes.length != 0) {
            scanner.setExcludes(excludes);
        }

        scanner.addDefaultExcludes();
        scanner.scan();

        String[] includedFiles = scanner.getIncludedFiles();

        for (String resource : includedFiles) {
            remoteResourcesBundle.addRemoteResource(StringUtils.replace(resource, '\\', '/'));
        }

        int n = remoteResourcesBundle.getRemoteResources().size();
        getLog().info("Writing " + RESOURCES_MANIFEST + " descriptor with " + n + " entr" + ((n > 1) ? "ies" : "y"));

        RemoteResourcesBundleXpp3Writer w = new RemoteResourcesBundleXpp3Writer();

        File f = new File(outputDirectory, RESOURCES_MANIFEST);

        FileUtils.mkdir(f.getParentFile().getAbsolutePath());

        try (Writer writer = new FileWriter(f)) {
            w.write(writer, remoteResourcesBundle);
        } catch (IOException e) {
            throw new MojoExecutionException("Error creating remote resources manifest.", e);
        }
    }
}
