/*
 *  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.easyant.tasks;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;

import org.apache.easyant.core.EasyAntConstants;
import org.apache.easyant.core.EasyAntMagicNames;
import org.apache.easyant.core.report.EasyAntReport;
import org.apache.easyant.core.report.ImportedModuleReport;
import org.apache.easyant.core.services.PluginService;
import org.apache.ivy.core.module.id.ModuleRevisionId;
import org.apache.ivy.core.report.ResolveReport;
import org.apache.ivy.core.resolve.ResolveOptions;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.MagicNames;

public class PluginUpdateChecker extends AbstractEasyAntTask {

    private File moduleIvy;
    private File moduleAnt;
    private File overrideModuleAnt;
    private String revisionToCheck = "latest.release";

    public void execute() throws BuildException {
        PluginService pluginService = (PluginService) getProject().getReference(
                EasyAntMagicNames.PLUGIN_SERVICE_INSTANCE);
        if (moduleIvy == null) {
            String moduleIvyProperty = getProject().getProperty(EasyAntMagicNames.EASYANT_FILE);
            if (moduleIvyProperty != null) {
                moduleIvy = new File(moduleIvyProperty);
            } else {
                moduleIvy = new File(getProject().getBaseDir(), EasyAntConstants.DEFAULT_BUILD_MODULE);
            }
            if (moduleIvy.exists() && overrideModuleAnt == null) {
                overrideModuleAnt = new File(moduleIvy.getParent(), EasyAntConstants.DEFAULT_OVERRIDE_BUILD_FILE);
            }
        }
        if (moduleAnt == null) {
            String moduleAntProperty = getProject().getProperty(MagicNames.ANT_FILE);
            if (moduleAntProperty != null) {
                moduleAnt = new File(moduleAntProperty);
            } else {
                moduleAnt = new File(getProject().getBaseDir(), EasyAntConstants.DEFAULT_BUILD_FILE);
            }
        }
        try {
            EasyAntReport easyantReport = pluginService.generateEasyAntReport(moduleIvy, moduleAnt, overrideModuleAnt);
            log("Plugin updates available :");
            for (ImportedModuleReport importedModuleReport : easyantReport.getImportedModuleReports()) {
                checkNewRevision(importedModuleReport.getModuleRevisionId());
            }
        } catch (Exception e) {
            throw new BuildException(e);
        }
    }

    public void checkNewRevision(ModuleRevisionId moduleRevisionId) throws ParseException, IOException {
        ModuleRevisionId latest = ModuleRevisionId.newInstance(moduleRevisionId, revisionToCheck);

        ResolveOptions resolveOptions = new ResolveOptions();
        resolveOptions.setLog(ResolveOptions.LOG_QUIET);
        resolveOptions.setDownload(false);
        ResolveReport report = getEasyAntIvyInstance().getResolveEngine().resolve(latest, resolveOptions, true);
        String resolvedRevision = report.getModuleDescriptor().getDependencies()[0].getDependencyRevisionId()
                .getRevision();
        if (!resolvedRevision.equals(moduleRevisionId.getRevision())) {
            StringBuilder sb = new StringBuilder();
            sb.append("\t")//
                    .append(moduleRevisionId.getOrganisation()) //
                    .append('#')//
                    .append(moduleRevisionId.getName())//
                    .append("\t")//
                    .append(moduleRevisionId.getRevision())//
                    .append(" -> ")//
                    .append(resolvedRevision);
            log(sb.toString());
        }

    }

}

@@ -1645,16 +1645,64 @@
 elease%22;
+%0A    private boolean pluginUpdateDetected=false;
 %0A%0A    pu
@@ -3201,32 +3201,140 @@
 ;%0A            %7D%0A
+            if (!pluginUpdateDetected) %7B%0A                log(%22%5CtAll plugins are up to date%22);%0A            %7D%0A
         %7D catch 
@@ -4554,24 +4554,63 @@
 oString());%0A
+            pluginUpdateDetected=true;%0A
         %7D%0A%0A 

improve pluginupdatechecker logging when all plugins are up to date