package org.jboss.pnc.jenkinsbuilddriver;

import org.jboss.pnc.common.util.IoUtils;
import org.jboss.pnc.spi.builddriver.exception.BuildDriverException;
import org.jboss.pnc.spi.repositorymanager.RepositoryConnectionInfo;
import org.jboss.util.StringPropertyReplacer;

import java.io.IOException;
import java.util.Properties;

/**
 * Created by <a href="mailto:matejonnet@gmail.com">Matej Lazar</a> on 2014-11-29.
 */
public class BuildJobConfig {
    private final RepositoryConnectionInfo connectionInfo;
    private String name;
    private String scmUrl;
    private String buildScript;

    public BuildJobConfig(String name, String scmUrl, String buildScript, RepositoryConnectionInfo connectionInfo) {
        this.name = name;
        this.scmUrl = scmUrl;
        this.buildScript = buildScript;
        this.connectionInfo = connectionInfo;
    }

    public String getName() {
        return name;
    }

    public String getXml() throws BuildDriverException {
        String xmlString = readConfigTemplate();

        Properties properties = new Properties();
        properties.setProperty("scm_url", scmUrl);
        
        //properties.setProperty("maven_settings", getMavenConfig(connectionInfo.getDependencyUrl()));
        //properties.setProperty("hudson.tasks.Shell.command", buildScript + " -s settings.xml");
        properties.setProperty("hudson.tasks.Shell.command", buildScript + "");

        return StringPropertyReplacer.replaceProperties(xmlString, properties);
    }

    private String getMavenConfig(String dependencyUrl) {
        String config = "printf \"<settings><mirrors><mirror><id>pnc-aprox</id><mirrorOf>*</mirrorOf><url>" + dependencyUrl +"</url></mirror></mirrors></settings>\" > settings.xml";
        return config.replace("<", "&lt;")
              .replace(">", "&gt;");

    }

    private String readConfigTemplate() throws BuildDriverException {
            try {
                return IoUtils.readFileOrResource("jenkins-job-template", "jenkins-job-template.xml", getClass().getClassLoader());
            } catch (IOException e) {
                throw new BuildDriverException("Cannot load config template.", e);
            }
    }

}

@@ -1127,34 +1127,32 @@
        %0A        
-//
 properties.setPr
@@ -1224,16 +1224,47 @@
 ncyUrl()
+, connectionInfo.getDeployUrl()
 ));%0A    
@@ -1271,10 +1271,8 @@
     
-//
 prop
@@ -1358,88 +1358,8 @@
 l%22);
-%0A        properties.setProperty(%22hudson.tasks.Shell.command%22, buildScript + %22%22);
 %0A%0A  
@@ -1497,16 +1497,34 @@
 dencyUrl
+, String deployUrl
 ) %7B%0A    
@@ -1644,16 +1644,33 @@
 ncyUrl +
+%0A                
 %22%3C/url%3E%3C
@@ -1688,16 +1688,349 @@
 irrors%3E%3C
+profiles%3E%3Cprofile%3E%22 +%0A                %22%3Cid%3Eaprox-deployment%3C/id%3E%3Cproperties%3E%22 +%0A                %22%3CaltDeploymentRepository%3E::default::%22 + deployUrl + %22%3C/altDeploymentRepository%3E%22 +%0A                %22%3C/properties%3E%3C/profile%3E%3C/profiles%3E%3CactiveProfiles%3E%3CactiveProfile%3Eaprox-deployment%3C/activeProfile%3E%3C/activeProfiles%3E%22 +%0A                %22%3C
 /setting

Add deployment URL