/*-
 * ========================LICENSE_START=================================
 * org.etsi.osl.cridge
 * %%
 * Copyright (C) 2019 openslice.io
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.etsi.osl.cridge;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;

/**
 * @author ctranoris
 *
 */
@EnableScheduling
@SpringBootApplication
public class CridgeSpringBoot implements CommandLineRunner {

  private static ApplicationContext applicationContext;

  private static final Logger logger =
      LoggerFactory.getLogger(CridgeSpringBoot.class.getSimpleName());


  @Bean
  KubernetesClient kubernetesClient() {
//    System.setProperty(Config.KUBERNETES_KUBECONFIG_FILE , "C:\\Users\\ctranoris\\.kube\\10.10.10.144.config");
//    Config config = Config.autoConfigure(null);
//    return new KubernetesClientBuilder().withConfig(config).build();
     return new KubernetesClientBuilder().build();
  }
  
  
  @Override
  public void run(String... arg0) throws Exception {
    if (arg0.length > 0 && arg0[0].equals("exitcode")) {
      throw new ExitException();
    }

  }

  public static void main(String[] args) throws Exception {

    logger.info("=========== STARTING CRIDGE ==============================");
    applicationContext = new SpringApplication(CridgeSpringBoot.class).run(args);
  }

  class ExitException extends RuntimeException implements ExitCodeGenerator {
    private static final long serialVersionUID = 1L;

    @Override
    public int getExitCode() {
      return 10;
    }
  }


}
