/**
 *  ---------
 * |.##> <##.|  SmartCard-HSM Support Scripts
 * |#       #|
 * |#       #|  Copyright (c) 2011-2012 CardContact Software & System Consulting
 * |'##> <##'|  Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de)
 *  ---------
 *
 * Consult your license package for usage terms and conditions.
 *
 * @fileoverview Configure Personalization Server
 */


// Source configuration from /etc/sc-hsm-srca/configuration.js

var Config = {};
try	{
	load("/etc/sc-hsm-persoserver/configuration.js");
} catch (e) {
	GPSystem.trace("Not sourcing /etc/sc-hsm-persoserver/configuration.js: " + e);
}

ApplicationServer	= require('scsh/srv-cc1/ApplicationServer').ApplicationServer;

if (Config.serverURL) {
	ApplicationServer.instance.setServerURL(Config.serverURL);
}

function handleRequest(req, res) {
	ApplicationServer.instance.handleRequest(req, res);
}

function performCardUpdate(session, pathInfo) {
	ApplicationServer.instance.performCardUpdate(session, pathInfo);
}


load("persoservicegui.js");
load("persoservice.js");
load("persomanager.js");


// Create an application factory that can create application instances from profiles
var af = new ApplicationFactory(new Crypto());

// Add application profiles to repository
af.addApplicationProfile("profiles/ap_jcop_cardmanager_default_key.xml");
af.addApplicationProfile("profiles/ap_apdutest.xml");

af.addKeyProfile("profiles/kp_jcop_default_s-mac.xml");
af.addKeyProfile("profiles/kp_jcop_default_s-enc.xml");
af.addKeyProfile("profiles/kp_jcop_default_dek.xml");

var cardprofile = GPSystem.mapFilename("profiles/cp_card.xml", GPSystem.CWD);

var pm = new PersoManager(af, cardprofile);

var userdir = GPSystem.mapFilename("", GPSystem.USR);
print("Looking for jobs.js in " + userdir + "/jobs");

var jobfn = GPSystem.mapFilename("jobs/jobs.js", GPSystem.AUTO);

if (jobfn == null) {
	var jobfn = GPSystem.mapFilename("apps/persoserver/jobs/jobs.js", GPSystem.AUTO);
}

if (jobfn == null) {
	var userdir = GPSystem.mapFilename("", GPSystem.USR);
	GPSystem.trace("No job definition found in " + userdir + "/jobs/jobs.js");
}

var service = new PersoService();

if (jobfn) {
	load(jobfn);
}

ApplicationServer.instance.registerService("ps", service.getServicePort(), service, service);

print("Configuration complete...");
