package edu.tamu.tcat.account.apacheds.test.application;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;

import edu.tamu.tcat.account.apacheds.LdapHelperAdFactory;
import edu.tamu.tcat.account.apacheds.LdapHelperReader;

/**
 * This class controls all aspects of the application's execution
 */
public class Application implements IApplication
{

   /*
    * (non-Javadoc)
    * @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
    */
   public Object start(IApplicationContext context) throws Exception
   {
      List<String> args = Arrays.asList(Platform.getCommandLineArgs());
      if (args.contains("-h"))
      {
         printHelp();
      }
      else if (!args.contains("-f"))
      {
         System.out.println("ldap configuration file must be specified");
         printHelp();
      }
      else if (!args.contains("-u"))
      {
         System.out.println("user distinguished name must be specified");
         printHelp();
      }
      else
      {
         int index = args.indexOf("-f");
         LdapHelperReader helper = createLdap(args.get(index + 1));
         index = args.indexOf("-u");
         String user = args.get(index + 1);
         if (args.contains("testUser"))
         {
            helper.checkValidUser(user);
            System.out.println("User [" + user + "] is a valid user");
         }
         else if (args.contains("testPassword"))
         {
            index = args.indexOf("-p");
            helper.checkValidPassword(user, args.get(index + 1));
            System.out.println("User [" + user + "] has a valid credentials");
         }
         else if (args.contains("groups"))
         {
            for (String g : helper.getGroupNames(user))
               System.out.println("User [" + user + "] is in group [" + g + "]");
         }
         else if (args.contains("attribute"))
         {
            index = args.indexOf("-a");
            for (Object a : helper.getAttributes(user, args.get(index + 1)))
               System.out.println("User [" + user + "] is in group [" + a + "]");
         }
      }
      return IApplication.EXIT_OK;
   }

   private LdapHelperReader createLdap(String string) throws IOException
   {
      try (InputStream is = new FileInputStream(string))
      {
         Properties p = new Properties();
         p.load(is);
         return new LdapHelperAdFactory().buildReader(p);
      }
   }

   /*
    * (non-Javadoc)
    * @see org.eclipse.equinox.app.IApplication#stop()
    */
   public void stop()
   {
      // nothing to do
   }

   private void printHelp()
   {
      System.out.println("Options are:");
      System.out.println("\t-f <configurationFile>");
      System.out.println("\t-u <user distinguished name>");
      System.out.println("\t<testUser {does this user exist} | ");
      System.out.println("\t\ttestPassword {does this user password combination validate} | ");
      System.out.println("\t\tgroups {get the groups of user} | ");
      System.out.println("\t\tattribute {get the specified attributes for this user} >");
      System.out.println("\t[-p <user password>]");
      System.out.println("\t[-a <attribute id>]");

      //TODO add mutate

   }
}

@@ -2199,70 +2199,507 @@
     
-for (Object a : helper.getAttributes(user, args.get(index + 1)
+String attr = args.get(index + 1);%0A            for (Object a : helper.getAttributes(user, attr))%0A               System.out.println(%22User %5B%22 + user + %22%5D is has attribute %5B%22+attr+%22%5D value %5B%22 + a + %22%5D%22);%0A         %7D%0A         else if (args.contains(%22matches%22))%0A         %7B%0A            index = args.indexOf(%22-a%22);%0A            String att = args.get(index + 1);%0A            index = args.indexOf(%22-v%22);%0A            String val = args.get(index + 1);%0A            for (Object a : helper.getMatches(null, att, val
 ))%0A 
@@ -2746,36 +2746,53 @@
 %22 + 
-user
+a
  + %22%5D 
-is in group
+has attribute %5B%22+att+%22%5D value
  %5B%22 + 
-a
+val
  + %22
@@ -3690,32 +3690,125 @@
  of user%7D %7C %22);%0A
+      System.out.println(%22%5Ct%5Ctmatches %7Bget the users matching an attribute value pair%7D %7C %22);%0A
       System.out
@@ -3983,16 +3983,60 @@
  id%3E%5D%22);
+%0A      System.out.println(%22%5Ct%5B-v %3Cvalue%3E%5D%22);
 %0A%0A      

add matches to command line ldap configuration testing tool