package br.com.elotech.websaude.odontograma.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableAutoConfiguration
@SpringBootApplication
public class Application {

    public static String args[] = new String[3];
    protected static String versao = null;

    public static void main(String[] args) {
        
        if(!available(9300)){
            System.out.println("Porta em uso!!!");
            System.exit(0);
        }
        
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileInputStream file = null;
                String everything;
                while (true) {
                    if (versao == null) {

                        try {
                            if (new File((System.getProperty("user.dir") + "/VERSAO")).exists())
                                file = new FileInputStream((System.getProperty("user.dir") + "/VERSAO"));
                            else if(new File((System.getProperty("user.dir") + "/../VERSAO")).exists())
                                file = new FileInputStream((System.getProperty("user.dir") + "/../VERSAO"));
                            
                            try {
                                everything = IOUtils.toString(file);
                                versao = everything;
                            } catch (IOException ex) {
                                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                            } finally {
                                try {
                                    file.close();
                                } catch (IOException ex) {
                                    Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                                }
                            }
                        } catch (FileNotFoundException ex) {
                            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }


                    try {
                        if (new File((System.getProperty("user.dir") + "/VERSAO")).exists())
                                file = new FileInputStream((System.getProperty("user.dir") + "/VERSAO"));
                            else if(new File((System.getProperty("user.dir") + "/../VERSAO")).exists())
                                file = new FileInputStream((System.getProperty("user.dir") + "/../VERSAO"));
                            
                        try {
                            everything = IOUtils.toString(file);
                            if (!versao.equals(everything)) {
                                System.exit(0);
                            }
                        } catch (IOException ex) {
                            Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                        } finally {
                            try {
                                file.close();
                            } catch (IOException ex) {
                                Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    } catch (FileNotFoundException ex) {
                        Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(Application.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }).start();

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

    public static boolean available(int port) {
        if (port < 90 || port > 9999)
            throw new IllegalArgumentException("início porta inválido: " + port);

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
        } finally {
            if (ds != null)
                ds.close();
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                }
            }
        }

        return false;
    }

}
