//Compile and publish an update
// Your ssh key-auth passphrase for the Anathema Update Server will be prompted for, or can be entered at the command line like so:
// gradlew uploadUpdate -PanathemaUsername=MyPassphraseHere

def updateLocation = "wwwroot/updates3"

task prepareUpdate << {
  def command = "mkdir -p ~/${updateLocation}/${versionString}"
  runSshExecOnBuildServer(command)
}

task copyFilesForUpdate(type: Copy) {
  destinationDir buildDir
  into("update") {
    from("${buildDir}/dependencies")
    from("${buildDir}/plugins")

  }
}

task buildFileListForUpdate << {
  def updateFolder = new File(buildDir, "update")
  def contentFile = new File(updateFolder, "content")
  String[] containedFiles = updateFolder.list();
  containedFiles.each {
    if(contentFile.size() > 0){
      contentFile.append("\n")
    }
    contentFile.append(it)
  }
}

task uploadUpdate << {
  def host     = "butatopanto.de"
  def dir      = "/home/anathema/${updateLocation}"
  def keyfile  = findKeyFile()
  def passphrase = usePropertyOrPrompt('anathemaPassphrase', "Anathema SSH Key Passphrase")
  def connectString = "anathema@${host}:${dir}/${versionString}"
  scp(connectString, keyfile, passphrase) {
    ant.fileset(dir:"${buildDir}/update")
  }
}

task finishUpdate << {
  def command = "echo ${versionString} >> ~/${updateLocation}/availableVersions"
  runSshExecOnBuildServer(command)
}

task publishAsUpdate

publishAsUpdate.group = 'Release'
publishAsUpdate.description = "Builds & uploads the update artifacts to Anathema's update server. [Requires registered credentials]"

copyFilesForUpdate.dependsOn "copyAnathemaModules", "copyExternalDependencies"
buildFileListForUpdate.dependsOn copyFilesForUpdate
uploadUpdate.dependsOn prepareUpdate, buildFileListForUpdate
finishUpdate.dependsOn uploadUpdate
publishAsUpdate.dependsOn finishUpdate