/*
 * $Id: ServiceConfigurator.java,v 1.5 2007/08/16 11:10:59 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ServiceConfigurator.java,v $
 * Revision 1.5  2007/08/16 11:10:59  marbobo
 * + Bugfixes
 * + Refactorings
 *
 * Revision 1.4  2006/08/10 20:18:37  marbobo
 * + Java_Home wird nun auch richtig gesetzt
 *
 */
package vkc.converter.server;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Properties;

import vkc.util.StringUtils;

/**
 * This class is responsible for configuring the jsl.ini properly. For now it is
 * just setting the classpath according to the included JARs in the lib
 * directory of the converter distribution and the stopport defined in
 * converter.properties.
 * 
 * @created 16.06.2004
 * @author <a href="mailto:andreas.koop@c-lab.de">Andreas Koop</a>
 */
public class ServiceConfigurator {
    public static void main(String[] args) throws Exception {
        if (args.length < 2) {
            System.out.println("Usage: java ServiceConfigurator <jsl.ini.tmpl> <jsl.ini>");
            System.exit(-1);
        }

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));

        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[1])));

        String workDir = System.getProperty("user.dir");

        File libDir = new File(workDir, "lib");
        File[] jars = libDir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                return pathname.getName().toLowerCase().endsWith(".jar");
            }
        });

        String classpath = workDir + "\\";

        for (int i = 0; i < jars.length; i++) {
            classpath += ";" + workDir + "\\lib\\" + jars[i].getName();
        }

        // stopport
        Properties p = new Properties();
        // Falls die Properties im Arbeitsverzeichnis sind,
        // werden sie geladen, sonst diejenigen aus dem vkc/converter/server
        // Verzeichnis
        File f = new File(ConverterServer.PROPERTIES_FILE);

        String propFilename = ConverterServer.PROPERTIES_FILE;

        if (f.exists()) {
            propFilename = "/" + propFilename;
        }

        p.load(ConverterServer.class.getResourceAsStream(propFilename));
        String stopport = p.getProperty("vkc.converter.stopport", "8465");
        String serviceName = p.getProperty("vkc.converter.servicename", "VKCConverterService");
        String jrePath = "";
        String line = null;

        if (args.length > 2) {
            jrePath = args[2];
        }

        while ((line = reader.readLine()) != null) {
            line = StringUtils.replaceToken(line, "${classpath}", classpath);
            line = line.replaceAll("\\$\\{stopport\\}", stopport);
            line = line.replaceAll("\\$\\{service.name\\}", serviceName);
            line = StringUtils.replaceToken(line, "${wrkdir}", workDir);
            line = StringUtils.replaceToken(line, "${java.home}", jrePath);
            writer.write(line);
            writer.write("\r\n");
            writer.flush();
        }

        reader.close();
        writer.close();
    }
}