apply plugin : 'de.qaware.seu.as.code.git'
apply plugin : LifecycleBasePlugin

configurations {
    antBuildEnv
}


repositories {
    // TODO: Still referring back to lib directory, because the lib cannot be found on the 'net.
    flatDir {
        dirs "${rootProject.projectDir}/lib"
    }
}

dependencies {
    antBuildEnv antContribDep
    antBuildEnv antlrDep
    antBuildEnv osgiBndDep
    antBuildEnv osgiBindexDep

    if(project.properties.forceRepoIndexAnt) {
        antBuildEnv "biz.aQute.bnd:org.osgi.impl.bundle.repoindex.ant:${forceRepoIndexAnt}", {
            force = true
        }
    } else {
        antBuildEnv osgiBndAntDep
    }
}

ext {
    githubOrgOrUser = project.properties.ceylonGitHubOrgOrUser ?: 'ceylon'
    localRepoMainDir  = file("${linkedProjectRootDir}/ceylon")
    localRepoFolder   = file("${linkedProjectRootDir}/${githubRepoName}")
//    localRepoBuildDir = file("${buildDir}/build-${githubRepoName}")
    localRepoExists   = file("${localRepoFolder}/.git").exists()
    localRepoDistDir  = file("${localRepoMainDir}/dist/dist")
    localOsgiDir  = file("${localRepoMainDir}/dist/osgi")
}

git {
    linkedProject {
        url "https://github.com/${githubOrgOrUser}/${githubRepoName}.git"
        directory localRepoFolder
        branch 'HEAD'
        singleBranch false

        options {
            pull {
                rebase = true
            }
        }
    }
}

task setup {
    group "Git Lifecycle"
    description "Clones a new repo for ${githubRepoName}"
    dependsOn 'gitCloneLinkedProject'
}

task update {
    group "Git Lifecycle"
    description "Updated ${githubRepoName} to latest version"
    dependsOn localRepoExists ? 'gitPullLinkedProject' : 'gitCloneLinkedProject'
}

task status {
    group "Git Lifecycle"
    description "Shows git status for ${githubRepoName}"
    dependsOn 'gitStatusLinkedProject'
    enabled = localRepoExists
}


// TODO: task setup-admins

clean {
    delete = []

    if (localRepoFolder.exists()) {
        dependsOn 'cleanAntBuild'
    }
}


task createAntBuildEnvironment( type : Copy ) {
    into {localRepoMainDir}

    from configurations.antBuildEnv, {
        into 'lib'
    }

//    from distDir, {
//        into 'dist/dist'
//    }
//
//    from osgiDistDir, {
//        into 'dist/osgi/build/dist'
//    }

    if(!localRepoExists) {
        dependsOn setup
    }

    dependsOn ':dist:createAntEnvironmentForLinkedProjects'
}

task invokeAntBuild( type : GradleBuild ) {
    dir = localRepoFolder
    buildFile = file('ant-wrapper.gradle')
    tasks = ['antPublish']
    startParameter.projectProperties+= [
        RUN_ANT_BUILD  : 1,
//        ceylonBuildDir : localRepoBuildDir,
        ceylonMainDir  : localRepoMainDir,
        ceylonDistDir  : localRepoDistDir,

        ceylonAntJar   : "${repoLibDir}/ceylon-ant.jar",
        antProjectDir  : localRepoFolder
    ]

    mustRunAfter update, setup
    dependsOn createAntBuildEnvironment

    startParameter.projectCacheDir = new File(
        gradle.startParameter.projectCacheDir ?
            "${gradle.startParameter.projectCacheDir}" : "${rootProject.projectDir}/.gradle",
        project.name.replace(":", "_")
    )
}

task cleanAntBuild( type : GradleBuild ) {
    dir = invokeAntBuild.dir
    buildFile = invokeAntBuild.buildFile
    tasks = [ 'antClean']
    startParameter = invokeAntBuild.startParameter
}

build {
    dependsOn invokeAntBuild
}

task publishInternal {
    dependsOn build
}