package commons.cmd;
import java.io.File;
import basics.application.Config;
import basics.filesystem.file.FileUtl;
import basics.utl.Empty;
import commons.application.GLOBAL;
import commons.application.Application;
import commons.application.core.Command;
import commons.application.ctx.Context;
import commons.utl.CtxUtl;
public class pl extends Command
{

private static final String ARGS = "[<sub-profile-name>], loading main profile (without arg) or given sub profile from source file. ";

public void process () throws Exception
{
   String args = ctx.consume(GLOBAL.CTX_ENVKEY_ARG0, "");
   boolean main = Empty.is(args);
   String profile = main ? CtxUtl.mainProfileFile(ctx) : CtxUtl.subProfileFile(ctx, args);
   Config cfg = null;
   if (!FileUtl.exists(profile))
   {
      Application.ui().print(profile + " not found. Creating a new Context.");
      cfg = new Config();
   }
   else
   {
      cfg = new Config(new File(profile));
   }
   // the attributes for a main context are going into the root context
   // while the subctx is stored in the current ctx with the key passed in as
   // args.
   if (main) CtxUtl.initCtx(cfg, ctx.getRoot());
   else CtxUtl.link(ctx, args, CtxUtl.initCtx(cfg, new Context()));
   // Manager.ui().put(ctx.toString());
}

public void signContract ()
{}

public String description ()
{
   return implName() + " loads a profile from the workspace: " + ARGS;
}
}
