def signJarProperties = file('signjar.properties')

Properties props = new Properties()
if (signJarProperties.exists())
{
    props.load(new FileInputStream(signJarProperties))
}

task signViewerJar(type: Exec) {
    onlyIf { props['signjar.cardpass'] }
    def freeplaneViewerJar = files(viewerJar).getSingleFile()
	commandLine 'jarsigner', '-keystore',  'NONE', '-tsa', props['signjar.tsaurl'],
		'-storetype', 'PKCS11', '-providerClass', 'sun.security.pkcs11.SunPKCS11',
		'-providerArg', props['signjar.cfg'], '-storepass',  props['signjar.cardpass'], 
		'-certchain', props['signjar.certchain'],
		freeplaneViewerJar, props['signjar.owner']
    dependsOn viewerJar
}

task signSvgJar(type: Exec) {
	onlyIf { props['signjar.cardpass'] }
	def svgJar = file('lib/kitfox-svg-salamander-1.1.1-p1.jar')
	commandLine 'jarsigner', '-keystore',  'NONE', '-tsa', props['signjar.tsaurl'],
		'-storetype', 'PKCS11', '-providerClass', 'sun.security.pkcs11.SunPKCS11',
		'-providerArg', props['signjar.cfg'], '-storepass',  props['signjar.cardpass'], 
		'-certchain', props['signjar.certchain'],
		svgJar, props['signjar.owner']
}

task doNotSignViewerJar {
    onlyIf { !props['signjar.cardpass'] }
    doLast {
        println('No ' + rootDir.path + '/freeplane/signjar.properties found')
        println('Applet is NOT signed!')
        println('(see ' + rootDir.path + '/freeplane/signjar-example.properties)')
    }
    dependsOn viewerJar
}

// exactly one of three is always enabled (see above)!
copyOSGiJars.dependsOn signViewerJar
copyOSGiJars.dependsOn doNotSignViewerJar
