buildscript {
	repositories {
		jcenter()
		mavenCentral()
	}
	
	dependencies {
		 classpath "com.github.ben-manes:gradle-versions-plugin:0.28.0"
	}
}

/*
task wrapper(type: Wrapper) {
    gradleVersion = '5.6.4'
}
*/

allprojects {
	apply plugin: "com.github.ben-manes.versions"
	repositories {  mavenCentral()  }
	version = '4.1.6-RC'
}

configurations {
	cveCheck
}

dependencies {
	cveCheck 'org.owasp:dependency-check-ant:1.2.5'
}

task checkCVE {
	doLast {
		// Redefine checkCVEAnt Ant task, with the classpath property set to our newly defined
		// cveCheck configuration classpath.
		ant.taskdef(name: 'checkCVEAnt', classname: 'org.owasp.dependencycheck.taskdefs.DependencyCheckTask',
			classpath: configurations.cveCheck.asPath)
	
		// executing checkCVEAnt Task
		ant.checkCVEAnt(
			applicationname: "PDF-AS",
			reportoutputdirectory: (new File(rootDir, 'releases/cvecheck/' + project.name)).toString(),
			reportformat: "ALL") {
			fileset(dir: (new File(rootDir, 'build/alldependencies')).toString()) {
				include(name: '**/**')
			}
		}
	}
}


subprojects {
	apply plugin: 'java'
	apply plugin: 'eclipse'
	apply plugin: 'maven'
	group = 'at.gv.egiz.pdfas'

	configurations {
		deployerJars
		cveCheck
	}
	
	repositories { mavenCentral() }

	dependencies { 
		testCompile 'junit:junit:4.8.2' 
		cveCheck 'org.owasp:dependency-check-ant:1.2.5'
	}

	task sourcesJar(type: Jar, dependsOn: classes) {
	    classifier = 'sources'
	    from sourceSets.main.allSource
	}

	artifacts {
	    archives sourcesJar
	}

	sourceCompatibility = 1.7
	project.ext{
		releaseRepoUrl = "file://${project(':').projectDir}/../mvn-repo/releases"
		snapshotRepoUrl = "file://${project(':').projectDir}/../mvn-repo/snapshots"
		version = version
		pdfasversion = version
		revision = getCheckedOutGitCommitHash()
		//tomcatVersion = '7.0.54';
		tomcatVersion = '8.0.36';
		slf4jVersion = '1.7.30'
		cxfVersion = '3.0.1'
	  }
	
	jar { manifest.attributes provider: 'EGIZ', 'Specification-Version': getCheckedOutGitCommitHash(), 'Implementation-Version': project.version  }
	
	task copyDeps(type: Copy) {
		from configurations.runtime
		into (new File(rootDir, 'build/alldependencies')).toString()
	}
	
	task copyDepsLocal(type: Copy) {
		from configurations.runtime
		into 'build/alldependencies'
	}
		
	rootProject.checkCVE.dependsOn copyDeps
	
	task checkCVELocal {
		doLast {
			// Redefine checkCVEAnt Ant task, with the classpath property set to our newly defined
			// cveCheck configuration classpath.
			ant.taskdef(name: 'checkCVEAnt', classname: 'org.owasp.dependencycheck.taskdefs.DependencyCheckTask',
				classpath: configurations.cveCheck.asPath)
	
			// executing checkCVEAnt Task
			ant.checkCVEAnt(
				applicationname: project.name,
				reportoutputdirectory: (new File(rootDir, 'releases/' + version + '/cvecheck/' + project.name)).toString(),
				reportformat: "ALL") {
					fileset(dir: 'build/alldependencies') {
					include(name: '**/**')
				}
			}
		}
	}
	checkCVELocal.dependsOn copyDepsLocal

	
	uploadArchives {
		repositories.mavenDeployer {
			repository(url: project.releaseRepoUrl)
			snapshotRepository(url: project.snapshotRepoUrl)
		}
	}
	
	task(internalRelease) {
		doLast {
		
		}
	}
	
	internalRelease.dependsOn jar
	internalRelease.dependsOn sourcesJar
	
	gradle.taskGraph.whenReady {taskGraph ->
    	//if (taskGraph.hasTask(release)) {
        //	version = '1.0'
    	//} else {
	    //    version = '1.0-SNAPSHOT'
	    //}
	}
	
	//doFullRelease jar war distZip distTar releases sourcesJar uploadArchives

}

task(doFullRelease) {
	doLast {
		println "done building all distribution stuff for " + project.version
	}
}

task copyLicenses {
	doLast {
   		mkdir("releases/"+ version +"/licenses");
   		def target = project.projectDir.toString() + "/releases/" + version + "/licenses"
		subprojects{
	    	def src=project.projectDir.toString() + "/licenses"
	    	copy{
				from src
				into target
			}
		}
	}
}

task releases(type: Copy) {
    dependsOn copyLicenses
	from ('doc') {
        include '*.pdf'
    }
	
	into "releases/" + version + "/docs"
}


def getCheckedOutGitCommitHash() {
	def gitFolder = "$projectDir/.git/"
	def takeFromHash = 40
	/*
	 * '.git/HEAD' contains either
	 *      in case of detached head: the currently checked out commit hash
	 *      otherwise: a reference to a file containing the current commit hash
	 */
	def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD
	def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd
	// def isRef = head.length > 1     // ref: refs/heads/master
 
	if(isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb
 
	def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master
	refHead.text.trim().take takeFromHash
}
/*
task docs(type: Javadoc) {
	source subprojects.collect {project -> project.sourceSets.main.allJava }
	classpath = files(subprojects.collect {project -> project.sourceSets.main.compileClasspath})
	destinationDir = new File(projectDir, 'docs/full')
}*/