package bamboo;

import bamboo.app.Bamboo;
import bamboo.core.Config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.io.IOException;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-h":
                case "--help":
                    System.out.println("Options:\n" +
                            "  -p port  Web server port\n" +
                            "  -b addr  Web server bind address\n" +
                            "  -c path  Web server context path");
                    return;
                case "-c":
                    System.setProperty("server.servlet.context-path", args[++i]);
                    break;
                case "-p":
                    System.setProperty("server.port", args[++i]);
                    break;
                case "-b":
                    System.setProperty("server.address", args[++i]);
                    break;
                default:
                    System.err.println("Ignoring unknown option: " + args[i]);
                    break;
            }
        }

        copyEnvToProperty("OIDC_URL", "spring.security.oauth2.client.provider.oidc.issuer-uri");
        copyEnvToProperty("OIDC_CLIENT_ID", "spring.security.oauth2.client.registration.oidc.client-id");
        copyEnvToProperty("OIDC_CLIENT_SECRET", "spring.security.oauth2.client.registration.oidc.client-secret");

        SpringApplication.run(Application.class, args);
    }

    private static void copyEnvToProperty(String env, String property) {
        String value = System.getenv(env);
        if (value != null && !value.isBlank()) {
            System.setProperty(property, value);
        }
    }

    @Bean
    public Bamboo getBamboo() throws IOException {
        return new Bamboo(new Config(System.getenv()), true);
    }
}

@@ -1307,24 +1307,83 @@
 %0A        %7D%0A%0A
+        if (System.getenv(%22LOGIN_OIDC_URL%22) != null) %7B%0A    
         copy
@@ -1393,24 +1393,30 @@
 ToProperty(%22
+LOGIN_
 OIDC_URL%22, %22
@@ -1473,32 +1473,36 @@
 -uri%22);%0A        
+    
 copyEnvToPropert
@@ -1496,32 +1496,38 @@
 yEnvToProperty(%22
+LOGIN_
 OIDC_CLIENT_ID%22,
@@ -1589,32 +1589,36 @@
 t-id%22);%0A        
+    
 copyEnvToPropert
@@ -1620,16 +1620,22 @@
 operty(%22
+LOGIN_
 OIDC_CLI
@@ -1712,16 +1712,372 @@
 ecret%22);
+%0A        %7D else %7B%0A            copyEnvToProperty(%22OIDC_URL%22, %22spring.security.oauth2.client.provider.oidc.issuer-uri%22);%0A            copyEnvToProperty(%22OIDC_CLIENT_ID%22, %22spring.security.oauth2.client.registration.oidc.client-id%22);%0A            copyEnvToProperty(%22OIDC_CLIENT_SECRET%22, %22spring.security.oauth2.client.registration.oidc.client-secret%22);%0A        %7D
 %0A%0A      

Allow using separate realms for logins and doss