plugins {
    id 'com.citi.helm' version '2.0.0'
    id 'com.citi.helm-releases' version '2.0.0'
}

group = 'com.company.product'

helm {
    namespace = 'product-customer'
    downloadClient {
        enabled = true
        version = '3.13.0'
    }
    lint {
        // This values file will be used for ALL lint configurations in ALL charts
        valueFiles.from 'values.yaml'
    }
    charts {
        authService {
            chartName = 'product-auth-service-test'
            chartVersion = '2.1.36'
            sourceDir = file('product-auth-service-test')
        }
        customerCreatorJob {
            chartName = 'product-customer-creator'
            chartVersion = '4.8.59'
            //chartVersion = project.version
            sourceDir = file('product-customer-creator')
        }
        database {
            chartName = 'product-database'
            chartVersion = '14.5.0'
            sourceDir = file('product-database')
        }
        productServer {
            chartName = 'product-server'
            chartVersion = '4.8.59'
            //chartVersion = project.version
            sourceDir = file('product-server')
        }
        productRemotestarter {
            chartName = 'product-remotestarter'
            chartVersion = '0.18.6'
            //chartVersion = project.version
            sourceDir = file('product-remotestarter')
        }
    }
    releases {
        authservice {
            from charts.authService
            //valueFiles.from 'values.yaml'
            wait = true
            waitForJobs = true
            mustUninstallAfter 'productserver', 'productremotestarter'
        }
        database {
            from charts.database
            //valueFiles.from 'values.yaml'
            wait = true
            waitForJobs = true
            mustUninstallAfter 'productserver', 'productremotestarter'

        }
        customercreatorjob {
            from charts.customerCreatorJob
            //valueFiles.from 'values.yaml'
            wait = true
            waitForJobs = true
            mustInstallAfter 'database'
            mustUninstallAfter 'productserver', 'productremotestarter', 'database'
        }
        productserver {
            from charts.productServer
            //valueFiles.from 'values.yaml'
            wait = true
            waitForJobs = true
            mustInstallAfter 'database'
            mustUninstallAfter 'productremotestarter'
        }
        productremotestarter {
            from charts.productRemotestarter
            //valueFiles.from 'values.yaml'
            wait = true
            waitForJobs = true
            mustInstallAfter 'authservice', 'customercreatorjob', 'database', 'productserver'
        }
    }
}

tasks.helmUninstall.outputs.upToDateWhen{false}
tasks.helmUninstallFromDefault.outputs.upToDateWhen{false}

helm.releases.all {
    valuesDir '.'
}

task uninstall_authservice(type: Exec) {
    group = "helm/manual uninstalling"
    executable 'helm'
    args (['-n', "${helm.namespace.get()}", 'uninstall', 'authservice', "--ignore-not-found", "--wait"  ])
    ignoreExitValue true
}
task uninstall_customercreatorjob(type: Exec) {
    group = "helm/manual uninstalling"
    executable 'helm'
    args (['-n', "${helm.namespace.get()}", 'uninstall', 'customercreatorjob', "--ignore-not-found", "--wait" ])
    ignoreExitValue true
}
task uninstall_productserver(type: Exec) {
    group = "helm/manual uninstalling"
    executable 'helm'
    args (['-n', "${helm.namespace.get()}", 'uninstall', 'productserver', "--ignore-not-found", "--wait"  ])
    ignoreExitValue true
}
task uninstall_remotestarter(type: Exec) {
    group = "helm/manual uninstalling"
    executable 'helm'
    args (['-n', "${helm.namespace.get()}", 'uninstall', 'productremotestarter', "--ignore-not-found", "--wait" ])
    ignoreExitValue true
}
task uninstall_database(type: Exec) {
    group = "helm/manual uninstalling"
    executable 'helm'
    args (['-n', "${helm.namespace.get()}", 'uninstall', 'database', "--ignore-not-found", "--wait" ])
    ignoreExitValue true
}
task uninstall_all(){
    group = "helm/manual uninstalling"
    dependsOn( uninstall_remotestarter )
    dependsOn( uninstall_productserver )
    dependsOn( uninstall_authservice )
    dependsOn( uninstall_customercreatorjob )
    dependsOn( uninstall_database )
}
tasks.uninstall_productserver.mustRunAfter( uninstall_remotestarter )
tasks.uninstall_authservice.mustRunAfter( uninstall_remotestarter, uninstall_productserver)
tasks.uninstall_database.mustRunAfter( uninstall_remotestarter, uninstall_productserver)
tasks.uninstall_customercreatorjob.mustRunAfter( uninstall_database, uninstall_productserver, uninstall_remotestarter)


task deleteEtcDirs(type: Exec) {
    group = "kubectl"
    executable 'kubectl'
    args (['-n', "${helm.namespace.get()}", 'delete', 'pvc', 'customer-dir', 'etc'])
    ignoreExitValue true
}
task deletek8scustomerCreatorJobs(type: Exec) {
    group = "kubectl"
    executable 'kubectl'
    args (['-n', "${helm.namespace}", 'delete', 'job', 'product-customer-creator', 'product-customer-creator-init'])
    ignoreExitValue true
}
task deleteCustomerCreatorJobs(){
    group = "kubectl"
    dependsOn( uninstall_customercreatorjob )
    dependsOn( deleteEtcDirs )
    dependsOn( deletek8scustomerCreatorJobs )
}
tasks.deleteEtcDirs.mustRunAfter( uninstall_customercreatorjob )
tasks.deletek8scustomerCreatorJobs.mustRunAfter( uninstall_customercreatorjob )

task meowmeow(){
    dependsOn( "server:build" )
}