/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you 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.
 */

apply plugin: 'elasticsearch.build'

Map<String, String> vagrantEnvVars = [
        'VAGRANT_CWD'           : "${project.projectDir.absolutePath}",
        'VAGRANT_VAGRANTFILE'   : 'Vagrantfile',
        'VAGRANT_PROJECT_DIR'   : "${project.projectDir.absolutePath}"
]

String box = "krb5kdc"

List<String> defaultPrincipals = [ "elasticsearch" ]

task update(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
    command 'box'
    subcommand 'update'
    boxName box
    environmentVars vagrantEnvVars
}

task up(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
    command 'up'
    args '--provision', '--provider', 'virtualbox'
    boxName box
    environmentVars vagrantEnvVars
    dependsOn update
}

task addDefaultPrincipals {
    dependsOn up
}

for (String principal : defaultPrincipals) {
    Task addTask = project.tasks.create("addPrincipal#${principal}", org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
        command 'ssh'
        args '--command', "sudo bash /vagrant/src/main/resources/provision/addprinc.sh $principal"
        boxName box
        environmentVars vagrantEnvVars
        dependsOn up
    }
    addDefaultPrincipals.dependsOn(addTask)
}

task halt(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
    command 'halt'
    boxName box
    environmentVars vagrantEnvVars
}

task destroy(type: org.elasticsearch.gradle.vagrant.VagrantCommandTask) {
    command 'destroy'
    args '-f'
    boxName box
    environmentVars vagrantEnvVars
    dependsOn halt
}

thirdPartyAudit.enabled = false
unitTest.enabled = false

// installKDC uses tabs in it for the Kerberos ACL file.
// Ignore it for pattern checking.
forbiddenPatterns {
    exclude "**/installkdc.sh"
}