/* (non-Javadoc)
   * @see org.codehaus.mojo.appassembler.daemon.script.ScriptGenerator#createBinScript(java.lang.String, org.codehaus.mojo.appassembler.model.Daemon, java.io.File, java.lang.String)
   */
  public void createBinScript(
      String platformName, Daemon daemon, File outputDirectory, String binFolder)
      throws DaemonGeneratorException {
    Platform platform = Platform.getInstance(platformName);

    InputStream in = null;

    FileWriter out = null;

    try {
      in = getClass().getResourceAsStream(platformName + "BinTemplate");

      if (in == null) {
        throw new DaemonGeneratorException(
            "Internal error: could not find template for platform '" + platformName + "'.");
      }
      InputStreamReader reader = new InputStreamReader(in);

      Map context = new HashMap();
      context.put("MAINCLASS", daemon.getMainClass());
      context.put("CLASSPATH", platform.getClassPath(daemon));
      context.put("EXTRA_JVM_ARGUMENTS", platform.getExtraJvmArguments(daemon.getJvmSettings()));
      context.put("APP_NAME", daemon.getId());
      context.put("ENV_SETUP", platform.getEnvSetup(daemon));
      context.put("REPO", daemon.getRepositoryName());
      context.put("LICENSE_HEADER", getLicenseHeader(platform, daemon));
      if (platform.isShowConsoleWindow(daemon)) {
        context.put("JAVA_BINARY", "java");
      } else {
        context.put("JAVA_BINARY", "start /min javaw");
      }

      String appArguments = platform.getAppArguments(daemon);
      if (appArguments != null) {
        context.put("APP_ARGUMENTS", appArguments + " ");
      } else {
        context.put("APP_ARGUMENTS", "");
      }

      String interpolationToken = platform.getInterpolationToken();
      InterpolationFilterReader interpolationFilterReader =
          new InterpolationFilterReader(reader, context, interpolationToken, interpolationToken);

      // Set the name of the bin file
      String programName = "";

      if (daemon.getId() == null || daemon.getId().trim().equals("")) {
        // Get class name and use it as the filename
        StringTokenizer tokenizer = new StringTokenizer(daemon.getMainClass(), ".");
        while (tokenizer.hasMoreElements()) {
          programName = tokenizer.nextToken();
        }

        programName = programName.toLowerCase();
      } else {
        programName = daemon.getId();
      }

      File binDir = new File(outputDirectory, binFolder);
      FileUtils.forceMkdir(binDir);
      File binFile = new File(binDir, programName + platform.getBinFileExtension());

      out = new FileWriter(binFile);
      getLogger()
          .debug(
              "Writing shell file for platform '"
                  + platform.getName()
                  + "' to '"
                  + binFile.getAbsolutePath()
                  + "'.");

      IOUtil.copy(interpolationFilterReader, out);
    } catch (FileNotFoundException e) {
      throw new DaemonGeneratorException("Failed to get template for bin file.", e);
    } catch (IOException e) {
      throw new DaemonGeneratorException("Failed to write bin file.", e);
    } finally {
      IOUtil.close(out);
      IOUtil.close(in);
    }
  }