apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'
apply plugin: 'jetty'

jar {
    manifest {
        attributes 'Implementation-Title': 'PDF-AS-WEB'
    }
}

repositories {
	mavenLocal()
    mavenCentral()
}

configurations { providedCompile }

sourceSets.main.compileClasspath += configurations.providedCompile
sourceSets.test.compileClasspath += configurations.providedCompile
sourceSets.test.runtimeClasspath += configurations.providedCompile

dependencies {
	compile project (':pdf-as-lib')
	compile project (':pdf-as-moa')
	compile project (':signature-standards:sigs-pkcs7detached')
	compile project (':signature-standards:sigs-pades')
	compile project (':pdf-as-pdfbox')
	compile project (':pdf-as-web-status')
	compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1'
	compile group: 'opensymphony', name: 'sitemesh', version: '2.4.2'
	compile "commons-codec:commons-codec:1.9"
	compile 'org.apache.commons:commons-lang3:3.3.2'
	compile 'org.apache.cxf:cxf-rt-transports-http:3.0.1'
	compile 'org.apache.cxf:cxf-rt-frontend-jaxws:3.0.1'
	providedCompile 'javax.servlet:javax.servlet-api:3.0.1'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

task downloadTomcat << {
	String url  = "http://repo1.maven.org/maven2/org/apache/tomcat/tomcat/##VERSION##/tomcat-##VERSION##.zip"
	String filename = project.buildDir.toString() + "/tomcat-##VERSION##.zip";
	
	url = url.replaceAll("##VERSION##", project.tomcatVersion);
	filename = filename.replaceAll("##VERSION##", project.tomcatVersion);
	
	println "Tomcat Version: " + url
	
	println "Tomcat file: " + filename
	
	def f = new File(filename)
	if (!f.exists()) {
		println "Downloading Tomcat ..."
		new URL(url).withInputStream{ i -> f.withOutputStream{ it << i }}
	}
}

task extractTomcat(dependsOn: downloadTomcat, type: Copy) {
	
	String filename = project.buildDir.toString() + "/tomcat-##VERSION##.zip";
	filename = filename.replaceAll("##VERSION##", project.tomcatVersion);
	
	inputs.file filename
	
	String targetDir = project.buildDir.toString() + "/tomcat-##VERSION##";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	def zipFile = file(filename)
	def outputDir = file(targetDir)
 
	from zipTree(zipFile)
	into outputDir
}

task cleanWebAppsInTomcat(dependsOn: extractTomcat) << {
	String targetDir = project.buildDir.toString() + "/tomcat-##VERSION##/apache-tomcat-##VERSION##/webapps/";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	def webappDir = new File(targetDir);
	println "Removing: " + webappDir.toString()
	def result = webappDir.deleteDir()  // Returns true if all goes well, false otherwise.
	println result.toString()
	
	assert result
	
	webappDir.mkdirs()
}

task putTemplateIntoTomcat(dependsOn: cleanWebAppsInTomcat, type: Copy) {
	String source = project.projectDir.toString() + "/";
	
	String targetDir = "build/tomcat-##VERSION##/apache-tomcat-##VERSION##";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	from 'src/main/assembly/tomcat'
	into targetDir
}

task putConfigIntoTomcat(dependsOn: putTemplateIntoTomcat, type: Copy) {
	String source = "../pdf-as-lib/build/resources/main/config/config.zip";
	
	String targetDir = "build/tomcat-##VERSION##/apache-tomcat-##VERSION##/conf/pdf-as";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	from zipTree(source)
	into targetDir
}

task putWebConfigIntoTomcat(dependsOn: putConfigIntoTomcat, type: Copy) {
	String targetDir = "build/tomcat-##VERSION##/apache-tomcat-##VERSION##/conf/pdf-as";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	from 'src/main/configuration/'
	into targetDir
}

task injectPdfAsWebApp(dependsOn: putWebConfigIntoTomcat, type: Copy) {
	//war.execute();
	
	String targetDir = project.buildDir.toString() + "/tomcat-##VERSION##/apache-tomcat-##VERSION##/webapps/";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	from war.outputs
	into targetDir
	rename '.*.war', 'pdf-as-web.war'
}

injectPdfAsWebApp.dependsOn war

task buildTomcat(dependsOn: injectPdfAsWebApp, type: Zip) {
	String targetDir = project.buildDir.toString() + "/tomcat-##VERSION##/apache-tomcat-##VERSION##";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	String archive = "apache-tomcat-##VERSION##-pdf-as-web-##PVERSION##.zip";
	archive = archive.replaceAll("##VERSION##", project.tomcatVersion);
	archive = archive.replaceAll("##PVERSION##", project.version);
	
	from targetDir
	archiveName archive
	destinationDir project.buildDir
}

task buildTomcatTar(dependsOn: injectPdfAsWebApp, type: Tar) {
	String targetDir = project.buildDir.toString() + "/tomcat-##VERSION##/apache-tomcat-##VERSION##";
	targetDir = targetDir.replaceAll("##VERSION##", project.tomcatVersion);
	
	String archive = "apache-tomcat-##VERSION##-pdf-as-web-##PVERSION##.tar";
	archive = archive.replaceAll("##VERSION##", project.tomcatVersion);
	archive = archive.replaceAll("##PVERSION##", project.version);
	
	from targetDir
	archiveName archive
	destinationDir project.buildDir
}

task releases(dependsOn: buildTomcat, type: Copy) {
	String archive = project.buildDir.toString() + "/apache-tomcat-##VERSION##-pdf-as-web-##PVERSION##.zip";
	archive = archive.replaceAll("##VERSION##", project.tomcatVersion);
	archive = archive.replaceAll("##PVERSION##", project.version);
	
	String tararchive = project.buildDir.toString() + "/apache-tomcat-##VERSION##-pdf-as-web-##PVERSION##.tar";
	tararchive = tararchive.replaceAll("##VERSION##", project.tomcatVersion);
	tararchive = tararchive.replaceAll("##PVERSION##", project.version);
	
	from war.outputs
	from archive
	from tararchive
	into rootDir.toString() + "/releases/" + version
}

task releaseConfig(type: Copy) {
	from 'src/main/configuration/pdf-as-web.properties'
	into rootDir.toString() + "/releases/" + version + "/cfg"
}

releases.dependsOn jar
releases.dependsOn sourcesJar
releases.dependsOn war
releases.dependsOn releaseConfig
releases.dependsOn buildTomcatTar