package eu.artemis.shield.discovery.slpdaemon.impl;

import java.net.InetAddress;

public class slpdaemon {
   
  public static String _version = Const.MAJOR + "." + Const.MINOR;
  
  private static SlpDaemonCommandLine sdcm = null;
  
  private static da f = null;
  
  static int width = 600;
  static int height = 350;
  //static String scope = "default";
      
  public static void main(String args[])  
  {      
    init();
    if (parse(args))
      {
        //Start the SLP Daemon
        startDaemon();                            
      }
      else
      {
        printInfo();        
      }        
  }      
  
  public static void stop()
  {  
        f.actionExit();            
        da.append("SLP Service Registry v" + _version + " stopped");
  }

  private static void init()
  {    
    /*
     * STANDARD CONFIGURATION
     */
    
    /*
    Const.DEBUG_ENABLED = false;
    
    Const.IP_LINKLOCAL = true;  // (IPv4) 169.254.0.0/16
    Const.IP_SITELOCAL = true;  // (IPv4) 10.0.0.0/8 or 172.10.0.0/16 or 192.168.0.0/15
    Const.IP_LOOPBACK = false;  // (IPv4) 127.0.0.0/8
    Const.IP_GLOBAL = false;    // (IPv4) all the others
    
    //Const.IPv6_ENABLED = false;
  
    Const.GUI_ENABLED = true;
      Const.OSGI_BUNDLE = false;
    Const.SERVLET_ENABLED = false;    
    
    Const.USE_JDBC = false;      
    */
    
    Const.FEDERATION_ENABLED = false;      
  }

  private static void printInfo()
  {
     System.out.println("\npSHIELD Project - slpdeamon v" + _version);
     System.out.println("Author: Vincenzo Suraci\n");
     System.out.println("USAGE: slpdeamon [-load] [-save] [-debug] [-gui] [-servlet] [-ipv4] [-nomcast] [-nomesh] [-port port_number] [-global/site/link/lo] [-scope scope] [-dbase database] [-sum summary] [-etrp type]\n");     
     System.out.println("debug   - enables debug information");
     System.out.println("nogui   - disables slpdaemon gui");     
     System.out.println("silent  - disables the slpdaemon messages on the console");
     System.out.println("secure  - enables authentication and authorization");
     //System.out.println("servlet- enables slpdaemon servlet");     
     System.out.println("ipv4    - forces the slpdaemon to bind an ipv4 address");     
     System.out.println("ipv6    - forces the slpdaemon to bind an ipv6 address");
     System.out.println("port    - forces the slpdaemon to use the specified port_number");        
     System.out.println("global  - forces the slpdaemon to use a global address");
     System.out.println("site    - (default) slpdaemon uses a site local address");
     System.out.println("link    - forces the slpdaemon to use a link local address");
     System.out.println("lo      - forces the slpdaemon to use a loopback address");
     System.out.println("jdbc    - enables the slpdaemon to use a SQL database to store services");
     System.out.println("scope   - forces the slpdaemon to use the specified scope");
     System.out.println("dbase   - forces the slpdaemon to use the specified database");
     System.out.println("sum     - forces the slpdaemon to use the specified summary");
     System.out.println("nomcast - forces the slpdaemon to not use multicast messages");
     System.out.println("nomesh  - forces the slpdaemon to not use mesh capabilities");
     System.out.println("load    - forces the slpdaemon to load previously saved services");
     System.out.println("save    - forces the slpdaemon to save registered services when exiting");
     System.out.println("etrp    - forces the slpdaemon to used the specified type of anti entropy algorithm");
     
  }
  
  private static boolean parse(String[] args)
  {
    for (int i = 0; i < args.length; i++)
    {      
      if (args[i].toLowerCase().equals("-debug"))
      {
        Const.DEBUG_ENABLED = true;
      }
      else if (args[i].toLowerCase().equals("-link"))
      {
        Const.IP_LINKLOCAL = true;
        Const.IP_SITELOCAL = false;
        Const.IP_LOOPBACK = false;
        Const.IP_GLOBAL = false;
      }    
      else if (args[i].toLowerCase().equals("-global"))
      {
        Const.IP_LINKLOCAL = false;
        Const.IP_SITELOCAL = false;
        Const.IP_LOOPBACK = false;
        Const.IP_GLOBAL = true;
      }    
      else if (args[i].toLowerCase().equals("-site"))
      {
        Const.IP_LINKLOCAL = false;
        Const.IP_SITELOCAL = true;
        Const.IP_LOOPBACK = false;
        Const.IP_GLOBAL = false;
      
      else if (args[i].toLowerCase().equals("-lo"))
      {
        Const.IP_LINKLOCAL = false;
        Const.IP_SITELOCAL = false;
        Const.IP_LOOPBACK = true;
        Const.IP_GLOBAL = false;
      }
      else if (args[i].toLowerCase().equals("-ipv4"))
      {
        Const.IPv6_ENABLED = false;
      }
      else if (args[i].toLowerCase().equals("-ipv6"))
      {
        Const.IPv6_ENABLED = true;
      }    
      else if (args[i].toLowerCase().equals("-nogui"))
      {
        Const.GUI_ENABLED = false;
      }
      else if (args[i].toLowerCase().equals("-silent"))
      {
        Const.CONSOLE_ENABLED = false;
      }
      else if (args[i].toLowerCase().equals("-secure"))
      {
        Const.SERVICE_REGISTRATION_AUTH_REQUIRED = true;
        Const.USER_AUTH_REQUIRED = true;
      }
      else if (args[i].toLowerCase().equals("-servlet"))
      {
        Const.SERVLET_ENABLED = true;
      }
      else if (args[i].toLowerCase().equals("-jdbc"))
      {
        Const.USE_JDBC = true;
      }
      else if (args[i].toLowerCase().equals("-scope"))
      {
        Const.SCOPE = args[++i];
      }
      else if (args[i].toLowerCase().equals("-dbase"))
      {
        Const.DBASE = args[++i];
      }      
      else if (args[i].toLowerCase().equals("-summary"))
      {
        Const.SUMMARY = args[++i];
      }
      else if (args[i].toLowerCase().equals("-nomesh"))
      {
        Const.MESH_ENHANCED_ENABLED = false;
      }
      else if (args[i].toLowerCase().equals("-nomcast"))
      {
        Const.MULTICAST_ENABLED = false;
      }
      else if (args[i].toLowerCase().equals("-load"))
      {
        Const.LOAD_DATA_ENABLED = true;
      }
      else if (args[i].toLowerCase().equals("-save"))
      {
        Const.SAVE_DATA_ENABLED = true;
      }
      else if (args[i].toLowerCase().equals("-port"))
      {
        try
        {
          i++;
          Const.port = Integer.parseInt(args[i]);
        }
        catch (Exception e)  
        {
          return false;
        }
      }else if (args[i].toLowerCase().equals("-etrp"))
      {
        try
        {
          i++;
          Const.ETRP_TYPE = Integer.parseInt(args[i]);
        }
        catch (Exception e)  
        {
          return false;
        }
      }
      else
      {
        return false;
      }      
    }
    return true;
  }

  private static void startDaemon()
  {  
     
    try 
    {
        /*boolean mcast = true;
        boolean mesh_enhanced = true;
        boolean loaddata = false;
        boolean savedata = false;
        int     etrpType; */

        // set InetAddress for this DA
        InetAddress ia = Util.getLocalInetAddress();
                              
        /*if (ia == null){
          ia = InetAddress.getLocalHost();
          System.out.println("Bound to localhost");            
        }*/
        // set scope for this DA
        /*String scope = System.getProperty("eu.artemis.shield.discovery.slpdaemon.scope");
        if (scope == null) scope = Const.defaultScope;

        // database file
        String dbase = System.getProperty("eu.artemis.shield.discovery.slpdaemon.dbase");
        if (dbase == null) dbase = Const.defaultDbase;

        // summary file
        String summary = System.getProperty("eu.artemis.shield.discovery.slpdaemon.summary");
        if (summary == null) summary = Const.defaultSummary;

        // multicast DAAdvert or not?
        String s = System.getProperty("eu.artemis.shield.discovery.slpdaemon.mcast");
        if (s != null && s.equalsIgnoreCase("no")) mcast = false;

        // carry "mesh-enhnaced" attribute keyword in DAAdvert or not?
        s = System.getProperty("eu.artemis.shield.discovery.slpdaemon.mesh");
        if (s != null && s.equalsIgnoreCase("no")) mesh_enhanced = false;

        // use selective or complete anti-entropy
        String s = System.getProperty("eu.artemis.shield.discovery.slpdaemon.mode");
        if (s != null && s.equalsIgnoreCase("complete")) {
            etrpType = Const.complete;
        } else {
            etrpType = Const.selective;
        }*/

        // load data or not?
        /*s = System.getProperty("eu.artemis.shield.discovery.slpdaemon.load");
        if (s != null && s.equalsIgnoreCase("yes")) loaddata = Const.LOAD_DATA_ENABLED;

        // save data or not?
        s = System.getProperty("eu.artemis.shield.discovery.slpdaemon.save");
        if (s != null && s.equalsIgnoreCase("yes")) savedata = Const.SAVE_DATA_ENABLED;*/
        
        f = new da(ia);
        
        sdcm = new SlpDaemonCommandLine(f);
        sdcm.start();
        
        da.append("SLP Service Registry v" + _version + " started");                
       
    
    catch (Exception e
    {
      da.append(e);                       
    }    
  }          
}