/*
 * Licensed Materials - Property of IBM Corp.
 * IBM UrbanCode Deploy
 * (c) Copyright IBM Corporation 2015. All Rights Reserved.
 *
 * U.S. Government Users Restricted Rights - Use, duplication or disclosure restricted by
 * GSA ADP Schedule Contract with IBM Corp.
 */
import com.urbancode.air.AirPluginTool
import com.urbancode.air.CommandHelper
import com.urbancode.air.plugin.cf.ic.helper.ResourceHelper

def apTool = new AirPluginTool(args[0], args[1])
def isWindows = apTool.isWindows
def props = apTool.getStepProperties()
def ch = new CommandHelper(new File(".").canonicalFile)

// ucd setup
def ucdUser = apTool.getAuthTokenUsername()
def ucdPass = apTool.getAuthToken()
def ucdUri = new URI(System.getenv("AH_WEB_URL"))
def ucdHelper = new ResourceHelper(ucdUri, ucdUser, ucdPass)

def standardInstallPaths = []
def overridePath = props['overrideCommandPath']
def api = props['apiEndpoint']

// override path must be checked first if it exists
if (overridePath && overridePath != '${p?:agent/cf.commandPath}') {
    standardInstallPaths << overridePath
}

if (isWindows) {
    standardInstallPaths << ("C:" + File.separator + "Program Files" + File.separator + "CloudFoundry"
        + File.separator + "cf.exe")
    standardInstallPaths << ("C:" + File.separator + "Program Files (x86)" + File.separator + "CloudFoundry"
        + File.separator + "cf.exe")
}
else {
    standardInstallPaths << "/opt/CloudFoundry/cf"
    standardInstallPaths << "/usr/share/CloudFoundry/cf"
}

def foundPath = ""
for (def path : standardInstallPaths) {
    File cfPath = new File(path)

    if (cfPath.exists() && cfPath.isFile()) {
        foundPath = cfPath.getAbsolutePath()
        break
    }
}

if (foundPath) {
    println("[OK] Found cf command line tool at " + foundPath)
}
else {
    // discovery process cannot continue without cf executable
    println("[ERROR] Cannot find cf command line executable tool. Ending discovery process.")
    println("[Possible Solution] Override the standard installation path by setting the 'cf.commandPath' property on" +
            " the agent.")
    System.exit(0)
}

def apiEndpoint = ""

def apiSet = {
    it.out.close() // close stdin
    def out = new PrintStream(System.out, true)
    def outputStream = new StringBuilder()
    try {
        it.waitForProcessOutput(outputStream, out)
    }
    finally {
        out.flush();
    }

    // parse out url from output
    int beginIndex = outputStream.indexOf(':')

    // if api has not yet been set
    if (beginIndex == -1) {
        return
    }
    else {
        beginIndex += 2
    }

    outputStream.delete(0, beginIndex)
    int endIndex = outputStream.indexOf(' ')
    outputStream.delete(endIndex, outputStream.length())

    apiEndpoint = outputStream.toString()
}

if (api && api != "{p?:agent/cf.api}") {
    apiEndpoint = api
}
else {
    // acquire api endpoint if not already specified
    def cmdArgs = [foundPath, "api"]
    int exitVal = ch.runCommand("[Action] Acquiring API Endpoint...", cmdArgs, apiSet)

    if (exitVal != 0) {
        println("[ERROR] Could not acquire an API Endpoint. This must be specified with a 'cf.api' property on the" +
                "agent instead.")
        System.exit(1)
    }

    // api endpoint was never set in this environment
    if (!apiEndpoint) {
        println("[ERROR] Auto-Discovery of API endpoint failed, because it was never set.")
        println("[Possible Solution] use the 'cf api' command in the agent environment to set the endpoint.")
        System.exit(1)
    }
}

// create the resource in ucd
def rootResourcePath = props['resourcePath']
def subResourcePath = rootResourcePath + "/Bluemix Instance - " + apiEndpoint
def subResourceDescription = "Bluemix instance with the api endpoint '${apiEndpoint}"
def subResourceName = subResourcePath.substring(subResourcePath.lastIndexOf("/Bluemix Instance") + 1)
def subResource = ucdHelper.getOrCreateSubResource(subResourcePath, rootResourcePath, subResourceName, subResourceDescription)

// create properties for the role
def roleProperties = new HashMap<String, String>()
roleProperties.put("cf.commandPath", foundPath)
roleProperties.put("cf.api", apiEndpoint)
def roleName = "BluemixInstance"

ucdHelper.addRoleToResource(subResource, roleName, roleProperties)

println("[OK] Bluemix Auto-Discovery has completed successfully.")