import org.apache.tools.ant.filters.ReplaceTokens

apply plugin: 'jetty'

def keystore = "$rootDir/samples/certificates/server.jks"
def password = 'password'

configurations {
	casServer
}
dependencies {
	casServer "org.jasig.cas:cas-server-webapp:4.0.0@war"
}

task casServerOverlay(type: Sync) {
	def war = configurations.casServer.resolve().toArray()[0]
	def warName = war.name.replace('.war','-custom')
	def overlayDir = file('src/main/webapp')
	def explodedWar = file("$buildDir/tmp/${warName}")
	ext.customWar = file("$buildDir/tmp/${warName}.war")
	ext.tokens = [logLevel: 'INFO']

	inputs.files(war, overlayDir)
	inputs.property('tokens',{tokens})
	outputs.files (customWar,explodedWar,file("$buildDir/tmp/expandedArchives"))

	from zipTree(war)
	from (overlayDir) {
		filter(ReplaceTokens,tokens: tokens)
	}
	into explodedWar

	doLast {
		if(customWar.exists()) {
			customWar.delete()
		}
		ant.zip(destfile: customWar, baseDir: explodedWar)
	}
}

casServerOverlay.metaClass.setLogLevel { level ->
	tokens['logLevel'] = level
}

task casServer (type: org.gradle.api.plugins.jetty.JettyRunWar, dependsOn: 'casServerOverlay') {
	contextPath = "/cas"
	connectors = [casServer.class.classLoader.loadClass('org.mortbay.jetty.security.SslSocketConnector').newInstance()]
	connectors[0].port = 9443
	connectors[0].keystore = connectors[0].truststore = keystore
	connectors[0].keyPassword = connectors[0].trustPassword = password
	connectors[0].wantClientAuth = true
	connectors[0].needClientAuth = false
	webApp = casServerOverlay.customWar

	inputs.file casServerOverlay.customWar

	doFirst() {
		System.setProperty('javax.net.ssl.trustStore', keystore)
		System.setProperty('javax.net.ssl.trustStorePassword', password)
		System.setProperty('java.naming.factory.url.pkgs','org.mortbay.naming')
		System.setProperty('java.naming.factory.initial','org.mortbay.naming.InitialContextFactory')
	}
}
