
// Task for creating the distro zip

configurations {
    antaws
}

dependencies {
    antaws "org.springframework.build:org.springframework.build.aws.ant:3.0.3.RELEASE",
            "net.java.dev.jets3t:jets3t:0.6.1"
}

task dist(type: Zip) {
    dependsOn subprojects*.tasks*.matching { task -> task.name == 'assemble' }

    evaluationDependsOn(':docs')

    def zipRootDir = "${project.name}-$version"
    into(zipRootDir) {
        from(rootDir) {
            include '*.txt'
        }
        into('docs') {
            with(project(':docs').apiSpec)
            with(project(':docs:manual').spec)
        }
        into('dist') {
            from coreModuleProjects.collect {project -> project.libsDir }
            from project(':spring-security-samples-tutorial').libsDir
            from project(':spring-security-samples-contacts').libsDir
        }
    }
    doLast {
        ant.checksum(file: archivePath, algorithm: 'SHA1', fileext: '.sha1')
    }
}

task uploadDist(type: UploadDist) {
    archiveFile = dist.archivePath
    shaFile = "${dist.archivePath}.sha1" as File
    archiveName = dist.archiveName
}

class UploadDist extends DefaultTask {
    @InputFile
    File shaFile

    @InputFile
    File archiveFile

    @Input
    String archiveName

    @TaskAction
    def upload() {
        def accessKey = project.s3AccessKey
        def secretKey = project.s3SecretAccessKey
        def version = project.version
        def classpath = project.configurations.antaws

        project.ant {
            taskdef(resource: 'org/springframework/build/aws/ant/antlib.xml', classpath: classpath.asPath)
            s3(accessKey: accessKey, secretKey: secretKey) {
                upload(bucketName: 'dist.springframework.org', file: archiveFile,
                        toFile: releaseType() + "/SEC/${archiveName}", publicRead: 'true') {
                    metadata(name: 'project.name', value: 'Spring Security')
                    metadata(name: 'release.type', value: releaseType())
                    metadata(name: 'bundle.version', value: version)
                    metadata(name: 'package.file.name', value: archiveName)
                }
                upload(bucketName: 'dist.springframework.org', file: shaFile,
                        toFile: releaseType() + "/SEC/${archiveName}.sha1", publicRead: 'true')
            }
        }
    }

    def releaseType() {
        if (project.releaseBuild) {
            'release'
        } else if (project.snapshotBuild) {
            'snapshot'
        } else {
            'milestone'
        }
    }
}