/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sonde;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author stoulouse
 */
public class Main {

    private static final String scriptSuppressionReleves = ResourceBundle.getBundle("config.config").getString("scriptSuppressionReleves");
    private static final String scriptRelevesProcParapheur = ResourceBundle.getBundle("config.config").getString("scriptRelevesProcParapheur");
    private static final String scriptRelevesProc = ResourceBundle.getBundle("config.config").getString("scriptRelevesProc");
    private static final String scriptRelevesBp = ResourceBundle.getBundle("config.config").getString("scriptRelevesBp");
    private static final String scriptRelevesBDDMysql = ResourceBundle.getBundle("config.config").getString("scriptRelevesBDDMysql");
    private static final String scriptRelevesBDDPostgres = ResourceBundle.getBundle("config.config").getString("scriptRelevesBDDPostgres");
    private static final String scriptRelevesRam = ResourceBundle.getBundle("config.config").getString("scriptRelevesRam");
    private static final String scriptRelevesDisk = ResourceBundle.getBundle("config.config").getString("scriptRelevesDisk");
    private static final String scriptKillPid = ResourceBundle.getBundle("config.config").getString("scriptKillPid");
    private static final String scriptPid = ResourceBundle.getBundle("config.config").getString("scriptPid");
    private static ServerSocket socket;
    private static InputStream monStream;
    private static ThreadSonde sondes;
    private static String fichierRelevesRam = ResourceBundle.getBundle("config.config").getString("fichierRelevesRam");
    private static String fichierRelevesProc = ResourceBundle.getBundle("config.config").getString("fichierRelevesProc");
    private static String fichierRelevesBdd = ResourceBundle.getBundle("config.config").getString("fichierRelevesBdd");
    private static String fichierRelevesBP = ResourceBundle.getBundle("config.config").getString("fichierRelevesBP");
    private static String fichierRelevesBlocsLus = ResourceBundle.getBundle("config.config").getString("fichierRelevesBlocsLus");
    private static String fichierRelevesBlocsEcris = ResourceBundle.getBundle("config.config").getString("fichierRelevesBlocsEcris");
    private static String fichierPid = ResourceBundle.getBundle("config.config").getString("fichierPid");
    private static Socket client;
    private static TypeBDD bdd;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws InterruptedException, IOException {


        creerBash("/scriptsSh/bddMysql.sh", scriptRelevesBDDMysql);
        creerBash("/scriptsSh/bddPostgres.sh", scriptRelevesBDDPostgres);
        creerBash("/scriptsSh/bp.sh", scriptRelevesBp);
        creerBash("/scriptsSh/disk.sh", scriptRelevesDisk);
        creerBash("/scriptsSh/procParapheur.sh", scriptRelevesProcParapheur);
        creerBash("/scriptsSh/proc.sh", scriptRelevesProc);
        creerBash("/scriptsSh/ram.sh", scriptRelevesRam);
        creerBash("/scriptsSh/killPid.sh", scriptKillPid);
        creerBash("/scriptsSh/pid.sh", scriptPid);
        creerBash("/scriptsSh/supp.sh", scriptSuppressionReleves);

        try {
            socket = new ServerSocket(12345);
            while (true) {
                client = socket.accept();
                monStream = client.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(monStream));
                String command = reader.readLine();
                if (command.equals("startParapheur")) {
                    startParapheur();
                } else if (command.equals("start")) {
                    start();
                } else if (command.equals("stop")) {
                    stop();
                } else if (command.equals("getReleveRam")) {
                    getproc(fichierRelevesRam);
                } else if (command.equals("getReleveProc")) {
                    getproc(fichierRelevesProc);
                } else if (command.equals("getReleveBdd")) {
                    getproc(fichierRelevesBdd);
                } else if (command.equals("getReleveBp")) {
                    getproc(fichierRelevesBP);
                } else if (command.equals("getReleveDiskLu")) {
                    getproc(fichierRelevesBlocsLus);
                } else if (command.equals("getReleveDiskEcrit")) {
                    getproc(fichierRelevesBlocsEcris);
                } else if (command.equals("bdd" + TypeBDD.MySQL.toString())) {
                    bdd = TypeBDD.MySQL;
                } else if (command.equals("bdd" + TypeBDD.Postgres.toString())) {
                    bdd = TypeBDD.Postgres;
                }

                monStream.close();
                client.close();

            }
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private static void startParapheur() {
        executeBash(scriptSuppressionReleves);
        executeBash(scriptRelevesProcParapheur);
        sondes = new ThreadSonde(bdd);
        sondes.start();

    }

    private static void start() {
        executeBash(scriptSuppressionReleves);
        executeBash(scriptRelevesProc);
        sondes = new ThreadSonde(bdd);
        sondes.start();

    }

    private static void stop() {
        killProc();
        if (sondes != null) {
            sondes.interrupt();
        }

    }

    private static void executeBash(String pathFichier) {
        try {
            Runtime.getRuntime().exec("sh +x " + pathFichier);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void killProc() {
        try {
            Runtime.getRuntime().exec("sh +x " + scriptPid);
            Thread.currentThread().sleep(1000);
            FileReader filepid = new FileReader(fichierPid);
            BufferedReader pidbuff = new BufferedReader(filepid);

            int pid = -1;
            String file;

            while ((file = pidbuff.readLine()) != null) {
                pid = Integer.parseInt(file);
                Runtime.getRuntime().exec("sh +x " + scriptKillPid + " " + pid);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void getproc(String fichierReleve) {
        try {
            InputStream file = new FileInputStream(fichierReleve);
            OutputStream clientOut = client.getOutputStream();
            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = file.read(buffer)) >= 0) {
                clientOut.write(buffer, 0, readed);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static void creerBash(String inputFile, String outputFile) {

        try {
            InputStream file = Main.class.getResourceAsStream(inputFile);
            OutputStream fileOut;

            fileOut = new FileOutputStream(outputFile);

            byte[] buffer = new byte[1024];
            int readed;
            while ((readed = file.read(buffer)) >= 0) {
                fileOut.write(buffer, 0, readed);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
