plugins{
    id 'distribution'
}

ext.addonName = "ActionDurationReminder"
ext.addonId = 1536
ext.addonVersion = "3.1.17a"
ext.addonCompatible = '5.1.5,5.0.5'

distTar.enabled = false
distributions {
    main {
        baseName = addonName
        contents {
            from addonName
            filter{
                it.replaceAll('@@ADDON_VERSION@@',addonVersion)
            }
        }
    }
}

distZip{
    archiveFile.set(file("${distZip.destinationDir}/${addonName}-${addonVersion}.zip"))
}

if (hasProperty('ESO_PTS_ADDONS_DIR')){
    def installDir =  new File(project.property( 'ESO_PTS_ADDONS_DIR'),addonName)
    installDist{
        into installDir
    }
}

task upload {
    group = "distribution"
    description = "upload addon zip file to esoui.com"
    dependsOn distZip
    doLast{
        def changelog = file('changelog').text
        changelog = groovy.json.StringEscapeUtils.escapeJava(changelog).replaceAll('\\\\r\\\\n',"\r\n")
        println changelog
        def proc = ['curl',
            '-H', "\"x-api-token: ${project.property('com.esoui.apiToken')}\"",
            '-F', "id=${addonId}",
            '-F', "version=$addonVersion",
            '-F', "compatible=$addonCompatible",
            '-F', "changelog=$changelog",
            '-F', "\"updatefile=@${distZip.archiveFile.get().asFile.absolutePath}\"",
            'https://api.esoui.com/addons/update']
                   .execute()
        proc.consumeProcessOutput(System.out, System.err)
        proc.waitFor()
    }
}

task addonInfo{
    group = "help"
    description = "show addon info"
    doLast{
        def proc = ['curl',
            '-H', "\"x-api-token: ${project.property('com.esoui.apiToken')}\"",
            "https://api.esoui.com/addons/details/${addonId}.json"]
                   .execute()
        proc.consumeProcessOutput(System.out, System.err)
        proc.waitFor()
    }
}