package org.jasig.cas.web;

import lombok.val;
import java.io.File;
import java.net.URL;
import java.io.IOException;
import java.util.Properties;
import java.io.FileInputStream;
import me.chyxion.emtc.EmTomcat;
import lombok.extern.slf4j.Slf4j;
import java.io.FileNotFoundException;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.config.Configurator;

/**
 * @author Donghuang
 * @date Jul 24, 2021 15:15:58
 */
@Slf4j
public class CasWebServer {

    static final String ARG_LOGGING_CONFIG_NAME = "--logging.config=";
    static final String ARG_SPRING_CONFIG_NAME = "--spring.config.location=";
    static final String ARG_SERVER_PORT = "--server.port=";
    static final String PROP_SPRING_CONFIG_NAME = "spring.config.location";
    static final String PROP_SERVER_PORT_NAME = "server.port";

    public static void main(final String[] args) throws IOException {
        Integer port = null;
        for (val arg : args) {
            if (arg.startsWith(ARG_LOGGING_CONFIG_NAME)) {
                val loggingConfig = arg.substring(ARG_LOGGING_CONFIG_NAME.length());
                if (StringUtils.isNotBlank(loggingConfig)) {
                    val configFile = new File(loggingConfig);
                    if (!configFile.isFile()) {
                        throw new FileNotFoundException(
                            "Log4j2 config [" + loggingConfig + "] is not a valid file");
                    }

                    Configurator.initialize(null, loggingConfig);
                    log.info("Logging config [{}] loaded.", loggingConfig);
                    continue;
                }
            }

            if (arg.startsWith(ARG_SPRING_CONFIG_NAME)) {
                val springConfig = arg.substring(ARG_SPRING_CONFIG_NAME.length());
                if (StringUtils.isNotBlank(springConfig)) {
                    System.setProperty(PROP_SPRING_CONFIG_NAME, springConfig);
                    continue;
                }
            }

            if (arg.startsWith(ARG_SERVER_PORT)) {
                val serverPort = arg.substring(ARG_SERVER_PORT.length());
                if (StringUtils.isNotBlank(serverPort)) {
                    log.info("Server port [{}] got from command line.", serverPort);
                    port = Integer.parseInt(serverPort);
                    continue;
                }
            }
        }

        if (port == null) {
            val springConfig = System.getProperty(PROP_SPRING_CONFIG_NAME);
            if (StringUtils.isNotBlank(springConfig)) {
                log.info("Load spring config [{}].", springConfig);
                try (val fin = new File(springConfig).isFile() ?
                        new FileInputStream(springConfig) :
                        new URL(springConfig).openStream()) {

                    val props = new Properties();
                    props.load(fin);
                    val serverPort = props.getProperty(PROP_SERVER_PORT_NAME);
                    if (StringUtils.isNotBlank(serverPort)) {
                        log.info("Server port [{}] got from spring config.", serverPort);
                        port = Integer.parseInt(serverPort);
                    }
                }
            }
        }

        EmTomcat.run(port != null ? port : 8080);
    }
}
