/**
 * Copyright (c) 2010-2011 Martin M Reed
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.hardisonbrewing.maven.cxx.generic;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.hardisonbrewing.maven.core.JoJoMojo;
import org.hardisonbrewing.maven.core.JoJoMojoImpl;
import org.hardisonbrewing.maven.cxx.TargetDirectoryService;

/**
 * @goal validate
 * @phase validate
 */
public final class ValidateMojo extends JoJoMojoImpl {

    /**
     * @parameter
     */
    public Source[] sources;

    @Override
    public final void execute() throws MojoExecutionException, MojoFailureException {

        String[] resourceFilePaths = TargetDirectoryService.getResourceFilePaths();
        for (int i = 0; resourceFilePaths != null && i < resourceFilePaths.length; i++) {
            ValidationService.validateInsideProject( resourceFilePaths[i] );
        }

        if ( sources != null ) {

            boolean defaultDirectoryUsed = false;

            for (Source source : sources) {

                if ( source.directory != null ) {
                    ValidationService.validateInsideProject( source.directory );
                    continue;
                }

                if ( defaultDirectoryUsed ) {
                    JoJoMojo.getMojo().getLog().error( "Default directory used for multiple <source/> entries." );
                    throw new IllegalArgumentException();
                }

                defaultDirectoryUsed = true;
            }
        }
    }
}
