/*
 * © Copyright 2013 EntIT Software LLC
 *  Certain versions of software and/or documents (“Material”) accessible here may contain branding from
 *  Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company.  As of September 1, 2017,
 *  the Material is now offered by Micro Focus, a separately owned and operated company.  Any reference to the HP
 *  and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
 *  marks are the property of their respective owners.
 * __________________________________________________________________
 * MIT License
 *
 * Copyright (c) 2018 Micro Focus Company, L.P.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * ___________________________________________________________________
 *
 */

package com.hpe.application.automation.tools.run;

import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.XmlFile;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;


import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;

/**
 * Created in order to fix previous builds that we're build with HP convention plugin and move them to HPE
 */
public class JobConfigRebrander  extends Builder implements SimpleBuildStep {

    @DataBoundConstructor
    public JobConfigRebrander() {
    }

    /**
     * Run this step.
     *
     * @param run       a build this is running as a part of
     * @param workspace a workspace to use for any file operations
     * @param launcher  a way to start processes
     * @param listener  a place to send output
     * @throws InterruptedException if the step is interrupted
     */
    @Override
    public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnull Launcher launcher,
                        @Nonnull TaskListener listener) throws InterruptedException, IOException {
        File root = Jenkins.getInstance().getRootDir();
        File projectsDir = new File(root,"jobs");
        File[] subdirs = projectsDir.listFiles();
        for (final File subdir : subdirs) {
            XmlFile xmlFile = new XmlFile(new File(subdir,"config.xml"));
            if (xmlFile.exists()) {
                convertHpToHpe(listener, xmlFile);
            }

            final File buildsFolder = new File(subdir, "builds");
            File[] builds = buildsFolder.listFiles();
            for(final File buildDir : builds){
                XmlFile buildXmlFile = new XmlFile(new File(buildDir,"build.xml"));
                if (buildXmlFile.exists()) {
                    convertHpToHpe(listener, buildXmlFile);
                }
            }

        }

    }

    private void convertHpToHpe(@Nonnull TaskListener listener, XmlFile confXmlFile) {
        try {
            String configuration = FileUtils.readFileToString(confXmlFile.getFile());
            String newConfiguration = StringUtils.replace(configuration, ".hp.", ".hpe.");
            FileUtils.writeStringToFile(confXmlFile.getFile(), newConfiguration);
        } catch (IOException e) {
            listener.error(
                    "failed to convert configuration 5.1 to new 5.2 format (hp to HPE): " + e.getMessage());
        }
    }


    @Extension
    public static class Descriptor extends BuildStepDescriptor<Builder> {

        public Descriptor() {
            load();
        }

        @Override
        public String getDisplayName() {
            return "Fix old HPE Jenkins builds";
        }

        @Override
        public boolean isApplicable(Class<? extends AbstractProject> aClass) {
            return true;
        }

    }

}

@@ -3558,38 +3558,115 @@
          convert
-HpToHp
+OldNameToNewName(listener, xmlFile, %22.hp.%22);%0A                convertOldNameToNewNam
 e(listener, xmlF
@@ -3664,24 +3664,33 @@
 ner, xmlFile
+, %22.hpe.%22
 );%0A         
@@ -4018,22 +4018,108 @@
  convert
-HpToHp
+OldNameToNewName(listener, buildXmlFile, %22.hp.%22);%0A                    convertOldNameToNewNam
 e(listen
@@ -4134,16 +4134,25 @@
 dXmlFile
+, %22.hpe.%22
 );%0A     
@@ -4225,14 +4225,23 @@
 vert
-HpToHp
+OldNameToNewNam
 e(@N
@@ -4289,16 +4289,32 @@
 fXmlFile
+, String oldName
 ) %7B%0A    
@@ -4486,21 +4486,29 @@
 on, 
-%22.hp.%22, %22.hpe
+oldName, %22.microfocus
 .%22);
@@ -4672,16 +4672,30 @@
         
+String.format(
 %22failed 
@@ -4723,31 +4723,16 @@
 ion 
-5.1 to new 5.2 
 format (
 hp t
@@ -4731,23 +4731,40 @@
 at (
-hp
+%25s
  to 
-HPE): %22 +
+microfocus): %25s%22, oldName,
  e.g
@@ -4771,24 +4771,25 @@
 etMessage())
+)
 ;%0A        %7D%0A
@@ -5034,19 +5034,27 @@
 Fix old 
-HPE
+Micro Focus
  Jenkins

Update old rebranding step as a contingency