package commons.cmd;
import java.io.File;
import basics.filesystem.file.FileUtl;
import basics.unexpected.Failure;
import basics.utl.Empty;
import basics.utl.SUtl;
import basics.utl.SysUtl;
import commons.application.GLOBAL;
import commons.application.Application;
import commons.application.core.Command;
import commons.utl.CtxUtl;
public class env extends Command
{

private static final String ARGS = "ARGS: ini<folder1,folder2,..>;set:<key1>=<val>,<key2>=<val>,..;unset:<k1>,<k2>,.. ";

public void process () throws Exception
{
   Application.ui().print("Home: " + CtxUtl.userHome(ctx));
   String args = ctx.consume(GLOBAL.CTX_ENVKEY_ARG0, "");
   String init = ctx.consume("init", "");
   String doset = ctx.consume("set", "");
   String unset = ctx.consume("unset", "");
   if (!Empty.is(args) || !Empty.is(init))
   {
      if (!FileUtl.exists(CtxUtl.userHome(ctx)))
         if (!FileUtl.mkdir(CtxUtl.userHome(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.userHome(ctx));
      if (!FileUtl.exists(CtxUtl.xmlSpace(ctx)))
         if (!FileUtl.mkdir(CtxUtl.xmlSpace(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.xmlSpace(ctx));
      if (!FileUtl.exists(CtxUtl.htmlSpace(ctx)))
         if (!FileUtl.mkdir(CtxUtl.htmlSpace(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.htmlSpace(ctx));
      if (!FileUtl.exists(CtxUtl.storageHome(ctx)))
         if (!FileUtl.mkdir(CtxUtl.storageHome(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.storageHome(ctx));
      if (!FileUtl.exists(CtxUtl.inboxSpace(ctx)))
         if (!FileUtl.mkdir(CtxUtl.inboxSpace(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.inboxSpace(ctx));
      if (!FileUtl.exists(CtxUtl.scriptSpace(ctx)))
         if (!FileUtl.mkdir(CtxUtl.scriptSpace(ctx)))
            throw new Failure("Init failed, can't create " + CtxUtl.scriptSpace(ctx));
   }
   if (!Empty.is(init))
   {
      String[] folders = SUtl.split(init);
      String home = CtxUtl.userHome(ctx);
      for (String folder : folders)
      {
         File subfolder = new File(home, folder);
         if (FileUtl.exists(subfolder))
            continue;
         if (!FileUtl.mkdir(subfolder))
            Application.ui().print("Can't create " + folder + ", full path: " + subfolder.toString());
         else
            SysUtl.trace("Created " + subfolder);

      }
   }
   if (!Empty.is(doset))
   {
      String p[] = SUtl.split(doset, ",");
      for (String e : p)
      {
         String kv[] = SUtl.split(e, "=");
         ctx.env().put(kv[0], kv[1]);
      }
   }
   if (!Empty.is(unset))
   {
      String p[] = SUtl.split(unset, ",");
      for (String e : p)
         ctx.env().remove(e);
   }
   if (Empty.is(init) && Empty.is(doset) && Empty.is(unset))
   {
      Application.ui().print("Constants: " + engine.constantsToString());
      Application.ui().print(ctx.env().toString());
   }
}

public void signContract ()
{}

public String description ()
{
   return implName() + " reports the engine's environment state, and sets environment variables (i.e. DEBUG): " + ARGS;
}
}
