/**
   * Construct a {@link SubProcessHeavy SubProcessHeavy} instance which when executed will fulfill
   * the given action agenda.
   *
   * <p>
   *
   * @param agenda The agenda to be accomplished by the action.
   * @param outFile The file to which all STDOUT output is redirected.
   * @param errFile The file to which all STDERR output is redirected.
   * @return The SubProcess which will fulfill the agenda.
   * @throws PipelineException If unable to prepare a SubProcess due to illegal, missing or
   *     imcompatable information in the action agenda or a general failure of the prep method code.
   */
  public SubProcessHeavy prep(ActionAgenda agenda, File outFile, File errFile)
      throws PipelineException {
    NodeID nodeID = agenda.getNodeID();

    /* sanity checks */
    File targetScene = null;
    ArrayList<File> sourceScenes = new ArrayList<File>();
    ArrayList<String> mergePatterns = new ArrayList<String>();
    File preBuild = null;
    File postBuild = null;
    File preScene = null;
    File postScene = null;
    {
      DoubleMap<Integer, String, TreeSet<FileSeq>> sources =
          new DoubleMap<Integer, String, TreeSet<FileSeq>>();

      for (String sname : agenda.getSourceNames()) {
        if (hasSourceParams(sname)) {
          FileSeq fseq = agenda.getPrimarySource(sname);
          Integer order = (Integer) getSourceParamValue(sname, "Order");
          addSourceSeq(order, sname, fseq, sources);
        }

        for (FileSeq fseq : agenda.getSecondarySources(sname)) {
          FilePattern fpat = fseq.getFilePattern();
          if (hasSecondarySourceParams(sname, fpat)) {
            Integer order = (Integer) getSecondarySourceParamValue(sname, fpat, "Order");
            addSourceSeq(order, sname, fseq, sources);
          }
        }
      }

      for (Integer order : sources.keySet()) {
        for (String sname : sources.get(order).keySet()) {
          for (FileSeq fseq : sources.get(order).get(sname)) {
            String pattern = null;
            if (fseq.equals(agenda.getPrimarySource(sname)))
              pattern = (String) getSourceParamValue(sname, "MergePattern");
            else {
              FilePattern fpat = fseq.getFilePattern();
              pattern = (String) getSecondarySourceParamValue(sname, fpat, "MergePattern");
            }

            if ((pattern == null) || (pattern.length() == 0))
              throw new PipelineException(
                  "The Merge Pattern for file sequence ("
                      + fseq
                      + ") of source node "
                      + "("
                      + sname
                      + ") was not specified!");

            NodeID snodeID = new NodeID(nodeID, sname);
            File sfile =
                new File(PackageInfo.sProdDir, snodeID.getWorkingParent() + "/" + fseq.getFile(0));

            sourceScenes.add(sfile);
            mergePatterns.add(pattern);
          }
        }
      }

      /* generate the name of the Houdini scene to save */
      {
        FileSeq fseq = agenda.getPrimaryTarget();
        String suffix = fseq.getFilePattern().getSuffix();
        if (!fseq.isSingle() || (suffix == null) || !suffix.equals("hip"))
          throw new PipelineException(
              "The HfsBuild Action requires that the primary target file sequence must "
                  + "be a single Houdini scene file!");

        targetScene =
            new File(PackageInfo.sProdDir, nodeID.getWorkingParent() + "/" + fseq.getFile(0));
      }

      /* command script files */
      {
        String sname = (String) getSingleParamValue("PreBuildScript");
        if (sname != null) {
          FileSeq fseq = agenda.getPrimarySource(sname);
          if (fseq == null)
            throw new PipelineException(
                "Somehow the Pre Build Script node ("
                    + sname
                    + ") was not one of the "
                    + "source nodes!");

          String suffix = fseq.getFilePattern().getSuffix();
          if (!fseq.isSingle() || (suffix == null) || !(suffix.equals("cmd")))
            throw new PipelineException(
                "The HfsBuild Action requires that the source node specified by the Pre "
                    + "Build Script parameter ("
                    + sname
                    + ") must have a single command "
                    + "script (.cmd) as its primary file sequence!");

          NodeID snodeID = new NodeID(nodeID, sname);
          preBuild =
              new File(PackageInfo.sProdDir, snodeID.getWorkingParent() + "/" + fseq.getFile(0));
        }
      }

      {
        String sname = (String) getSingleParamValue("PostBuildScript");
        if (sname != null) {
          FileSeq fseq = agenda.getPrimarySource(sname);
          if (fseq == null)
            throw new PipelineException(
                "Somehow the Post Build Script node ("
                    + sname
                    + ") was not one of the "
                    + "source nodes!");

          String suffix = fseq.getFilePattern().getSuffix();
          if (!fseq.isSingle() || (suffix == null) || !(suffix.equals("cmd")))
            throw new PipelineException(
                "The HfsBuild Action requires that the source node specified by the Post "
                    + "Build Script parameter ("
                    + sname
                    + ") must have a single command "
                    + "script (.cmd) as its primary file sequence!");

          NodeID snodeID = new NodeID(nodeID, sname);
          postBuild =
              new File(PackageInfo.sProdDir, snodeID.getWorkingParent() + "/" + fseq.getFile(0));
        }
      }

      {
        String sname = (String) getSingleParamValue("PreSceneScript");
        if (sname != null) {
          FileSeq fseq = agenda.getPrimarySource(sname);
          if (fseq == null)
            throw new PipelineException(
                "Somehow the Pre Scene Script node ("
                    + sname
                    + ") was not one of the "
                    + "source nodes!");

          String suffix = fseq.getFilePattern().getSuffix();
          if (!fseq.isSingle() || (suffix == null) || !(suffix.equals("cmd")))
            throw new PipelineException(
                "The HfsBuild Action requires that the source node specified by the Pre "
                    + "Scene Script parameter ("
                    + sname
                    + ") must have a single command script "
                    + "(.cmd) as its primary file sequence!");

          NodeID snodeID = new NodeID(nodeID, sname);
          preScene =
              new File(PackageInfo.sProdDir, snodeID.getWorkingParent() + "/" + fseq.getFile(0));
        }
      }

      {
        String sname = (String) getSingleParamValue("PostSceneScript");
        if (sname != null) {
          FileSeq fseq = agenda.getPrimarySource(sname);
          if (fseq == null)
            throw new PipelineException(
                "Somehow the Post Scene Script node ("
                    + sname
                    + ") was not one of the "
                    + "source nodes!");

          String suffix = fseq.getFilePattern().getSuffix();
          if (!fseq.isSingle() || (suffix == null) || !(suffix.equals("cmd")))
            throw new PipelineException(
                "The HfsBuild Action requires that the source node specified by the Post "
                    + "Scene Script parameter ("
                    + sname
                    + ") must have a single command script "
                    + "(.cmd) as its primary file sequence!");

          NodeID snodeID = new NodeID(nodeID, sname);
          postScene =
              new File(PackageInfo.sProdDir, snodeID.getWorkingParent() + "/" + fseq.getFile(0));
        }
      }
    }

    /* create the temporary Houdini command script */
    File hscript = createTemp(agenda, 0644, "cmd");
    try {
      FileWriter out = new FileWriter(hscript);

      if (preBuild != null) out.write("source " + preBuild + "\n");

      /* load the source scenes */
      int wk;
      for (wk = 0; wk < sourceScenes.size(); wk++) {
        File sourceScene = sourceScenes.get(wk);
        String pattern = mergePatterns.get(wk);

        if (preScene != null) out.write("source " + preScene + "\n");

        out.write("mread");
        if (wk > 0) out.write(" -o -m " + pattern);
        out.write(" " + sourceScene + "\n");

        if (postScene != null) out.write("source " + postScene + "\n");
      }

      if (postBuild != null) out.write("source " + postBuild + "\n");

      /* save the file */
      out.write("mwrite " + targetScene + "\n");

      out.close();
    } catch (IOException ex) {
      throw new PipelineException(
          "Unable to write temporary command file ("
              + hscript
              + ") for Job "
              + "("
              + agenda.getJobID()
              + ")!\n"
              + ex.getMessage());
    }

    /* create the wrapper shell script */
    File script = createTemp(agenda, 0755, "bash");
    try {
      FileWriter out = new FileWriter(script);
      out.write("#!/bin/bash\n\n" + "cat " + hscript + " | hscript");
      out.close();
    } catch (IOException ex) {
      throw new PipelineException(
          "Unable to write temporary script file ("
              + script
              + ") for Job "
              + "("
              + agenda.getJobID()
              + ")!\n"
              + ex.getMessage());
    }

    try {
      return new SubProcessHeavy(
          agenda.getNodeID().getAuthor(),
          getName() + "-" + agenda.getJobID(),
          script.getPath(),
          new ArrayList<String>(),
          agenda.getEnvironment(),
          agenda.getWorkingDir(),
          outFile,
          errFile);
    } catch (Exception ex) {
      throw new PipelineException(
          "Unable to generate the SubProcess to perform this Action!\n" + ex.getMessage());
    }
  }
  /**
   * Construct a {@link SubProcessHeavy SubProcessHeavy} instance which when executed will fulfill
   * the given action agenda.
   *
   * <p>
   *
   * @param agenda The agenda to be accomplished by the action.
   * @param outFile The file to which all STDOUT output is redirected.
   * @param errFile The file to which all STDERR output is redirected.
   * @return The SubProcess which will fulfill the agenda.
   * @throws PipelineException If unable to prepare a SubProcess due to illegal, missing or
   *     imcompatable information in the action agenda or a general failure of the prep method code.
   */
  public SubProcessHeavy prep(ActionAgenda agenda, File outFile, File errFile)
      throws PipelineException {
    /* model filenames */
    TreeMap<String, Path> modelPaths = new TreeMap<String, Path>();
    TreeMap<String, Path> animPaths = new TreeMap<String, Path>();

    TreeMap<String, String> nameSpaces = new TreeMap<String, String>();
    TreeMap<String, String> reverseNameSpaces = new TreeMap<String, String>();
    TreeMap<String, Boolean> usesNameSpace = new TreeMap<String, Boolean>();
    TreeMap<String, String> buildTypes = new TreeMap<String, String>();

    for (String sname : agenda.getSourceNames()) {
      if (hasSourceParams(sname)) {
        FileSeq fseq = agenda.getPrimarySource(sname);
        String suffix = fseq.getFilePattern().getSuffix();
        if (fseq.isSingle() && (suffix != null)) {
          if (suffix.equals("ma") || suffix.equals("mb")) {
            Path npath = new Path(sname);
            String type = getSourceStringParamValue(sname, aSceneType);
            String nspace = getSourceStringParamValue(sname, aPrefixName);
            if (nspace == null) {
              nspace = npath.getName();
            }
            if (type.equals(aModel))
              modelPaths.put(sname, new Path(npath.getParentPath(), fseq.getPath(0)));
            else {
              animPaths.put(sname, new Path(npath.getParentPath(), fseq.getPath(0)));
              nspace += "_a";
            }

            boolean useNSpace = false;
            {
              Boolean tf = (Boolean) getSourceParamValue(sname, aNameSpace);
              useNSpace = ((tf != null) && tf);
            }

            String buildType = getSourceStringParamValue(sname, aBuildType);
            if (buildType == null)
              throw new PipelineException(
                  "The value of the "
                      + aBuildType
                      + " source parameter for node "
                      + "("
                      + sname
                      + ") was not set!");

            buildTypes.put(sname, buildType);
            nameSpaces.put(sname, nspace);
            reverseNameSpaces.put(nspace, sname);
            usesNameSpace.put(sname, useNSpace);
          }
        }
      }
    }

    /* the target Maya scene */
    Path targetScene = getMayaSceneTargetPath(agenda);
    String sceneType = getMayaSceneType(agenda);

    /* create a temporary MEL script file */
    File script = createTemp(agenda, "mel");
    try {
      FileWriter out = new FileWriter(script);

      /* provide parameters as MEL variables */
      out.write(genFrameRangeVarsMEL());

      /* rename the current scene as the output scene */
      out.write(
          "// SCENE SETUP\n"
              + "file -rename \""
              + targetScene
              + "\";\n"
              + "file -type \""
              + sceneType
              + "\";\n\n");

      out.write(genUnitsMEL());

      writeInitialMEL(agenda, out);

      /* the model file reference imports */
      TreeMap<String, Path> all = new TreeMap<String, Path>(modelPaths);
      all.putAll(animPaths);
      for (String sname : all.keySet()) {
        Path mpath = all.get(sname);

        String buildType = buildTypes.get(sname);

        out.write(
            "// MODEL: "
                + sname
                + "\n"
                + "print \""
                + buildType
                + " Model: "
                + mpath
                + "\\n\";\n"
                + "file\n");

        String nspace = nameSpaces.get(sname);

        boolean useNSpace = usesNameSpace.get(sname);

        if (buildType.equals(aImport)) {
          out.write("  -import\n");

          if (useNSpace) out.write("  -namespace \"" + nspace + "\"\n");
        } else if (buildType.equals(aReference)) {
          out.write("  -reference\n");

          if (useNSpace) out.write("  -namespace \"" + nspace + "\"\n");
          else out.write("  -renamingPrefix \"" + nspace + "\"\n");
        } else {
          throw new PipelineException(
              "Unknown value for the "
                  + aBuildType
                  + " source parameter for node "
                  + "("
                  + sname
                  + ") encountered!");
        }

        {
          String fname = mpath.getName();
          if (fname.endsWith("ma")) out.write("  -type \"mayaAscii\"\n");
          else if (fname.endsWith("mb")) out.write("  -type \"mayaBinary\"\n");
          else
            throw new PipelineException(
                "Unknown Maya scene format for source file (" + mpath + ")!");
        }

        out.write("  -options \"v=0\"\n" + "  \"$WORKING" + mpath + "\";\n\n");
      }

      writeModelMEL(agenda, out);

      out.write(
          "global proc string removePrefix(string $name)\n"
              + "{\n"
              + "  string $toReturn;\n"
              + "  string $buffer[];\n"
              + "  tokenize($name, \"|\", $buffer);\n"
              + "  string $part;\n"
              + "  for ($part in $buffer) {\n"
              + "    string $buffer2[];\n"
              + "    tokenize($part, \":\", $buffer2);\n"
              + "    if ($toReturn == \"\")\n"
              + "      $toReturn += $buffer2[(size($buffer2) -1)];\n"
              + "    else\n"
              + "      $toReturn += \"|\" + $buffer2[(size($buffer2) -1)];\n"
              + "  }\n"
              + "  return $toReturn;    \n"
              + "}\n");

      out.write(
          "global proc string addPrefix(string $name, string $prefix)\n"
              + "{\n"
              + "  string $toReturn;\n"
              + "  string $buffer[];\n"
              + "  tokenize($name, \"|\", $buffer);\n"
              + "  string $part;\n"
              + "  for ($part in $buffer) {\n"
              + "    if ($toReturn == \"\")\n"
              + "      $toReturn += $prefix + $buffer[(size($buffer) -1)];\n"
              + "    else\n"
              + "      $toReturn += \"|\" + $prefix + $buffer[(size($buffer) -1)];\n"
              + "  }\n"
              + "  return $toReturn;\n"
              + "}\n");

      for (String sname : animPaths.keySet()) {
        String nspace = nameSpaces.get(sname);
        String otherNspace = nspace.substring(0, nspace.length() - 2);

        String modelName = reverseNameSpaces.get(otherNspace);

        if (modelName != null) {
          boolean useNSpace = usesNameSpace.get(sname);
          boolean otherUseNSpace = usesNameSpace.get(modelName);

          String buildType = buildTypes.get(sname);
          String otherBuildType = buildTypes.get(modelName);
          String prefix = generatePrefix(nspace, useNSpace, buildType);
          String otherPrefix = generatePrefix(otherNspace, otherUseNSpace, otherBuildType);

          out.write(
              "{\n"
                  + "  string $crvPrefix = \""
                  + prefix
                  + "\";\n"
                  + "  string $objPrefix = \""
                  + otherPrefix
                  + "\";\n"
                  + "  string $obj = $crvPrefix + \"curveInfo\";\n"
                  + "  if (`objExists $obj`) {\n"
                  + "    int $size = `getAttr -s ($obj + \".cn\")`;\n"
                  + "    int $i = 0;\n"
                  + "    for ($i = 0; $i < $size; $i++) {\n"
                  + "      string $curve = `getAttr  ($obj + \".cn[\" + $i + \"]\")`;\n"
                  + "      string $attr = `getAttr  ($obj + \".an[\" + $i + \"]\")`;\n"
                  + "      $curve =  addPrefixToName($crvPrefix, $curve);\n"
                  + "      $attr  =  addPrefixToName($objPrefix, $attr);\n"
                  + "      print(\"connectAttr -f \" + $curve + \" \" + $attr + \"\\n\");\n"
                  + "      connectAttr -f $curve $attr;   \n"
                  + "    }\n"
                  + "  }\n"
                  + "  else {\n"
                  + "    string $curves[] = `ls -type animCurve ($crvPrefix + \"*\")`;\n"
                  + "    string $curve;\n"
                  + "    for ($curve in $curves) {\n"
                  + "      string $shortName = removePrefix($curve);\n"
                  + "      string $buffer[];\n"
                  + "      tokenize($shortName, \"_\", $buffer);\n"
                  + "      string $attr = $buffer[(size($buffer) -1)];\n"
                  + "      string $name = \"\";\n"
                  + "      int $length = (size($buffer) -1);\n"
                  + "      int $i;\n"
                  + "      for ($i = 0; $i < $length; $i++) {\n"
                  + "        $name += $buffer[$i];\n"
                  + "        if ($i != ($length - 1) )\n"
                  + "          $name += \"_\";\n"
                  + "      }\n"
                  + "      string $channel = addPrefix($name, $objPrefix) + \".\" + $attr;\n"
                  + "      print ($curve + \"\\t\" + $channel + \"\\n\");\n"
                  + "      if (!`getAttr -l $channel`)\n"
                  + "        connectAttr -f ($curve + \".output\") $channel;\n"
                  + "    }\n"
                  + "  }\n"
                  + "}\n");
        }
      }

      /* set the time options */
      out.write(genPlaybackOptionsMEL());

      writeAnimMEL(agenda, out);

      /* save the file */
      out.write(
          "// SAVE\n"
              + "print \"Saving Generated Scene: "
              + targetScene
              + "\\n\";\n"
              + "file -save;\n\n");

      writeFinalMEL(agenda, out);

      out.close();
    } catch (IOException ex) {
      throw new PipelineException(
          "Unable to write temporary MEL script file ("
              + script
              + ") for Job "
              + "("
              + agenda.getJobID()
              + ")!\n"
              + ex.getMessage());
    }

    /* create the process to run the action */
    return createMayaSubProcess(null, script, true, agenda, outFile, errFile);
  }