apply from: JAVA_GRADLE
apply from: SAMPLE_GRADLE
apply from: TOMCAT_7_GRADLE
apply plugin: "application"

dependencies {
	compile project(':spring-session-data-gemfire'),
		"org.springframework:spring-web:$springVersion",
		"org.webjars:bootstrap:$bootstrapVersion",
		"org.webjars:webjars-taglib:$webjarsTaglibVersion",
		jstlDependencies

	providedCompile "javax.servlet:javax.servlet-api:$servletApiVersion"

	runtime "org.springframework.shell:spring-shell:1.1.0.RELEASE"

	testCompile "junit:junit:$junitVersion",
		"org.assertj:assertj-core:$assertjVersion"

	integrationTestCompile seleniumDependencies

	integrationTestRuntime "org.springframework.shell:spring-shell:1.1.0.RELEASE"
}


def port
def process

mainClassName = "sample.ServerConfig"

task availablePort() {
	doLast {
		def serverSocket = new ServerSocket(0)
		port = serverSocket.localPort
		serverSocket.close()
	}
}

task runGemFireServer(dependsOn: availablePort) {
	doLast {
		println "Starting GemFire Server on port [$port]..."

		def out = new StringBuilder()
		def err = new StringBuilder()

		String classpath = sourceSets.main.runtimeClasspath.collect { it }.join(File.pathSeparator)
		String gemfireLogLevel = System.getProperty('spring.session.data.gemfire.log-level', 'warning')

		String[] commandLine = [
			'java', '-server', '-ea', '-classpath', classpath,
			//"-Dgemfire.log-file=gemfire-server.log",
			//"-Dgemfire.log-level=config",
			"-Dspring.session.data.gemfire.log-level=" + gemfireLogLevel,
			"-Dspring.session.data.gemfire.port=$port",
			'sample.ServerConfig'
		]

		//println commandLine

		process = commandLine.execute()

		process.consumeProcessOutput(out, err)

		//println 'OUT: ' + out
		//println 'ERR: ' + err
	}
}

integrationTest.doLast {
	println 'Stopping GemFire Server...'
	process?.destroyForcibly()
}

integrationTomcatRun {
	dependsOn runGemFireServer
	doFirst {
		System.setProperty("spring.session.data.gemfire.port", "$port");
	}
}
