/*
 * Copyright 2017-2023 EPAM Systems, Inc. (https://www.epam.com/)
 *
 * 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.
 */

task clean(type: Delete) {
  delete("$rootDir/api/src/main/resources/static/cloud-data-linux.tar.gz")
  delete("$rootDir/api/src/main/resources/static/cloud-data-win64.zip")
  delete("$project.rootDir/cloud-pipeline-webdav-client/out/")
}

task buildLinux(type: Exec) {
  group = 'build'
  doFirst {
    delete("$rootDir/api/src/main/resources/static/cloud-data-linux.tar.gz")
  }
  environment "CP_CLOUD_DATA_SOURCES_DIR": "$project.rootDir/cloud-pipeline-webdav-client/"
  environment "CLOUD_DATA_APP_VERSION": "$version"
  commandLine "bash", "$project.rootDir/cloud-pipeline-webdav-client/build_linux.sh"
  doLast {
    copy {
      from("$project.rootDir/cloud-pipeline-webdav-client/out/cloud-data-linux.tar.gz")
      into("$project.rootDir/api/src/main/resources/static/")
    }
  }
}

task buildWin(type: Exec) {
  group = 'build'
  doFirst {
    delete("$rootDir/api/src/main/resources/static/cloud-data-win64.zip")
  }
  environment "CP_CLOUD_DATA_SOURCES_DIR": "$project.rootDir/cloud-pipeline-webdav-client/"
  environment "CLOUD_DATA_APP_VERSION": "$version"
  commandLine "bash", "$project.rootDir/cloud-pipeline-webdav-client/build_win.sh"
  doLast {
    copy {
      from("$project.rootDir/cloud-pipeline-webdav-client/out/cloud-data-win64.zip")
      into("$project.rootDir/api/src/main/resources/static/")
    }
  }
}

task buildCli(type: Exec) {
  group = 'build'
  doFirst {
    delete("$rootDir/api/src/main/resources/static/cloud-data-cli-linux")
    delete("$rootDir/api/src/main/resources/static/cloud-data-cli-macos")
    delete("$rootDir/api/src/main/resources/static/cloud-data-cli-win.exe")
  }
  environment "CP_CLOUD_DATA_SOURCES_DIR": "$project.rootDir/cloud-pipeline-webdav-client/"
  environment "CLOUD_DATA_APP_VERSION": "$version"
  commandLine "bash", "$project.rootDir/cloud-pipeline-webdav-client/build_cli.sh"
  doLast {
    copy {
      from("$project.rootDir/cloud-pipeline-webdav-client/dist-cli/")
      include("cloud-data-cli-*")
      into("$project.rootDir/api/src/main/resources/static/")
    }
  }
}
