/*
 * Copyright Thoughtworks, 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.
 */

tasks.register('bumpSchemaVersion') {
  description = "Helper to bump `schemaVersion` and setup cruise-config.xml migrations."
  doFirst {

    File goConstantsFile = project(':base').file("src/main/java/com/thoughtworks/go/util/GoConstants.java")
    def originalXSDFile = project(':config:config-server').file('src/main/resources/cruise-config.xsd')
    def originalDefaultXMLFile = project(':installers').file('go-server/release/cruise-config.xml')

    def goConstantsContents = goConstantsFile.getText('utf-8')
    def xsdContents = originalXSDFile.getText('utf-8')
    def xmlContents = originalDefaultXMLFile.getText('utf-8')

    def currentVersion = Integer.parseInt(goConstantsFile.readLines().grep(~/.*CONFIG_SCHEMA_VERSION\s*=\s*.*/).first().trim().split("=").last().trim().replace(";", ""))
    def nextVersion = currentVersion + 1

    goConstantsFile.withWriter { out ->
      out.write(goConstantsContents.replaceAll(/(.*)CONFIG_SCHEMA_VERSION\s*=\s*.*/, "\$1CONFIG_SCHEMA_VERSION = ${nextVersion};"))
    }

    copy {
      from originalXSDFile
      into project(':config:config-server').file("src/main/resources/schemas")
      rename "cruise-config.xsd", "${currentVersion}_cruise-config.xsd"
    }

    originalXSDFile.withWriter { out ->
      out.print(xsdContents.replaceAll("""<xsd:attribute name="schemaVersion" type="xsd:int" use="required" fixed="${currentVersion}"/>""", """<xsd:attribute name="schemaVersion" type="xsd:int" use="required" fixed="${nextVersion}"/>"""))
    }

    originalDefaultXMLFile.withWriter { out ->
      out.print(xmlContents.replaceAll("${currentVersion}", "${nextVersion}"))
    }

    def xslFile = project(':config:config-server').file("src/main/resources/upgrades/${nextVersion}.xsl")
    xslFile.withWriter { out ->
      out.println("""
<?xml version="1.0"?>
<!--
  ~ Copyright Thoughtworks, 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.
  -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/cruise/@schemaVersion">
        <xsl:attribute name="schemaVersion">${nextVersion}</xsl:attribute>
    </xsl:template>
    <!-- Copy everything -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>
""".trim())

      println "Now edit the file ${xslFile}"
    }
  }
}
