/*
 * Copyright 2015 the original author or authors.
 *
 * 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.
 */

plugins {
    id 'play'
}

repositories {
    jcenter()
    maven {
        name "typesafe-maven-release"
        url "https://repo.typesafe.com/typesafe/maven-releases"
    }
    ivy {
        name "typesafe-ivy-release"
        url "https://repo.typesafe.com/typesafe/ivy-releases"
        layout "ivy"
    }
}

dependencies {
    play 'commons-lang:commons-lang:2.6'
    playTest "com.google.guava:guava:17.0"
}

model {
    components {
        play {
            binaries.all { binary ->
                tasks.create("addCopyrightToPlay${binary.name.capitalize()}Assets", AddCopyrights) { copyrightTask ->
                    source "raw-assets"
                    copyrightFile = project.file('copyright.txt')
                    destinationDir = project.file("${buildDir}/play${binary.name.capitalize()}/addCopyRights")

                    // Hook this task into the binary
                    binary.assets.addAssetDir destinationDir
                    binary.assets.builtBy copyrightTask
                }
            }
        }
    }
}

class AddCopyrights extends SourceTask {
    @InputFile
    File copyrightFile

    @OutputDirectory
    File destinationDir

    @TaskAction
    void generateAssets() {
        String copyright = copyrightFile.text
        getSource().files.each { File file ->
            File outputFile = new File(destinationDir, file.name)
            outputFile.text = "${copyright}\n${file.text}"
        }
    }
}
