/*-
 * Copyright (c) 2013-2019 Red Hat, Inc.
 *
 * 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 org.fedoraproject.xmvn.tools.subst;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.fedoraproject.xmvn.config.Configurator;
import org.fedoraproject.xmvn.locator.ServiceLocator;
import org.fedoraproject.xmvn.locator.ServiceLocatorFactory;
import org.fedoraproject.xmvn.metadata.MetadataRequest;
import org.fedoraproject.xmvn.metadata.MetadataResolver;
import org.fedoraproject.xmvn.metadata.MetadataResult;

/**
 * @author Mikolaj Izdebski
 */
public class SubstCli
{
    private final List<String> configuredMetadataRepos;

    private MetadataResolver metadataResolver;

    public SubstCli( Configurator configurator, MetadataResolver metadataResolver )
    {
        this.metadataResolver = metadataResolver;
        configuredMetadataRepos = configurator.getConfiguration().getResolverSettings().getMetadataRepositories();
    }

    private void run( SubstCliRequest cliRequest )
    {
        List<MetadataResult> metadataResolvers = new ArrayList<>();

        if ( cliRequest.getRoot() != null )
        {
            List<String> metadataRepos = new ArrayList<>();
            Path root = Paths.get( cliRequest.getRoot() );

            for ( String configuredRepo : configuredMetadataRepos )
            {
                Path repoPath = Paths.get( configuredRepo );
                if ( repoPath.isAbsolute() )
                {
                    metadataRepos.add( root.resolve( Paths.get( "/" ).relativize( repoPath ) ).toString() );
                }
            }

            metadataResolvers.add( metadataResolver.resolveMetadata( new MetadataRequest( metadataRepos ) ) );
        }

        metadataResolvers.add( metadataResolver.resolveMetadata( new MetadataRequest( configuredMetadataRepos ) ) );

        ArtifactVisitor visitor = new ArtifactVisitor( cliRequest.isDebug(), metadataResolvers );

        visitor.setTypes( cliRequest.getTypes() );
        visitor.setFollowSymlinks( cliRequest.isFollowSymlinks() );
        visitor.setDryRun( cliRequest.isDryRun() );

        try
        {
            for ( String path : cliRequest.getParameters() )
            {
                Files.walkFileTree( Paths.get( path ), visitor );
            }
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }

        if ( cliRequest.isStrict() && visitor.getFailureCount() > 0 )
            System.exit( 1 );
    }

    public static void main( String[] args )
    {
        try
        {
            SubstCliRequest cliRequest = new SubstCliRequest( args );
            if ( cliRequest.isDebug() )
                System.setProperty( "xmvn.debug", "true" );

            ServiceLocator locator = new ServiceLocatorFactory().createServiceLocator();
            Configurator configurator = locator.getService( Configurator.class );
            MetadataResolver metadataResolver = locator.getService( MetadataResolver.class );

            SubstCli cli = new SubstCli( configurator, metadataResolver );

            cli.run( cliRequest );
        }
        catch ( Throwable e )
        {
            // Helper exceptions used with our integration tests should be ignored
            if ( e.getClass().getName().startsWith( "org.fedoraproject.xmvn.it." ) )
                throw (RuntimeException) e;

            System.err.println( "Unhandled exception" );
            e.printStackTrace();
            System.exit( 2 );
        }
    }
}

@@ -855,24 +855,79 @@
 nfigurator;%0A
+import org.fedoraproject.xmvn.config.ResolverSettings;%0A
 import org.f
@@ -1274,38 +1274,26 @@
 ate 
-final List%3CString%3E configuredM
+MetadataResolver m
 etad
@@ -1297,19 +1297,22 @@
 tadataRe
-pos
+solver
 ;%0A%0A    p
@@ -1322,41 +1322,41 @@
 ate 
-MetadataResolver metadataResolver
+ResolverSettings resolverSettings
 ;%0A%0A 
@@ -1502,38 +1502,31 @@
         
-configuredMetadataRepo
+resolverSetting
 s = conf
@@ -1578,34 +1578,335 @@
 gs()
-.getMetadataRepositories()
+;%0A    %7D%0A%0A    private MetadataResult resolveMetadata( List%3CString%3E repos )%0A    %7B%0A        MetadataRequest request = new MetadataRequest( repos );%0A        request.setIgnoreDuplicates( resolverSettings.isIgnoreDuplicateMetadata() );%0A        MetadataResult result = metadataResolver.resolveMetadata( request );%0A        return result
 ;%0A  
@@ -2007,21 +2007,19 @@
 adataRes
-olver
+ult
 s = new 
@@ -2250,26 +2250,36 @@
 dRepo : 
-configured
+resolverSettings.get
 Metadata
@@ -2279,24 +2279,33 @@
 etadataRepos
+itories()
  )%0A         
@@ -2602,73 +2602,33 @@
 aRes
-olver
+ult
 s.add( 
-metadataResolver.resolveMetadata( new MetadataRequest
+resolveMetadata
 ( me
@@ -2636,26 +2636,24 @@
 adataRepos )
- )
  );%0A        
@@ -2678,85 +2678,55 @@
 aRes
-olver
+ult
 s.add( 
-metadataResolver.resolveMetadata( new MetadataRequest( configured
+resolveMetadata( resolverSettings.get
 Meta
@@ -2734,17 +2734,24 @@
 ataRepos
- 
+itories(
 ) ) );%0A%0A
@@ -2838,21 +2838,19 @@
 adataRes
-olver
+ult
 s );%0A%0A  

Make xmvn-subst honor settings for ignoring duplicate metadata