/*
   * (non-Javadoc)
   *
   * @see
   * org.ngrinder.script.service.IScriptValidationService#validateScript(org
   * .ngrinder.model.User, org.ngrinder.model.IFileEntry, boolean,
   * java.lang.String)
   */
  @Override
  public String validateScript(
      User user, IFileEntry scriptIEntry, boolean useScriptInSVN, String hostString) {
    FileEntry scriptEntry = cast(scriptIEntry);
    try {
      checkNotNull(scriptEntry, "scriptEntity should be not null");
      checkNotEmpty(scriptEntry.getPath(), "scriptEntity path should be provided");
      if (!useScriptInSVN) {
        checkNotEmpty(scriptEntry.getContent(), "scriptEntity content should be provided");
      }
      checkNotNull(user, "user should be provided");
      // String result = checkSyntaxErrors(scriptEntry.getContent());

      ScriptHandler handler = scriptHandlerFactory.getHandler(scriptEntry);
      String result = handler.checkSyntaxErrors(scriptEntry.getPath(), scriptEntry.getContent());
      if (result != null) {
        return result;
      }
      File scriptDirectory = config.getHome().getScriptDirectory(user);
      FileUtils.deleteDirectory(scriptDirectory);
      scriptDirectory.mkdirs();
      ProcessingResultPrintStream processingResult =
          new ProcessingResultPrintStream(new ByteArrayOutputStream());
      handler.prepareDist(
          0L, user, scriptEntry, scriptDirectory, config.getSystemProperties(), processingResult);
      if (!processingResult.isSuccess()) {
        return new String(processingResult.getLogByteArray());
      }
      File scriptFile = new File(scriptDirectory, FilenameUtils.getName(scriptEntry.getPath()));

      if (useScriptInSVN) {
        fileEntryService.writeContentTo(user, scriptEntry.getPath(), scriptDirectory);
      } else {
        FileUtils.writeStringToFile(
            scriptFile,
            scriptEntry.getContent(),
            StringUtils.defaultIfBlank(scriptEntry.getEncoding(), "UTF-8"));
      }
      int timeout =
          Math.max(
              config
                  .getSystemProperties()
                  .getPropertyInt(
                      NGrinderConstants.NGRINDER_VALIDATION_TIMEOUT,
                      LocalScriptTestDriveService.DEFAULT_TIMEOUT),
              10);
      File doValidate =
          localScriptTestDriveService.doValidate(
              scriptDirectory,
              scriptFile,
              new Condition(),
              config.isSecurityEnabled(),
              hostString,
              timeout);
      List<String> readLines = FileUtils.readLines(doValidate);
      StringBuffer output = new StringBuffer();
      String path = config.getHome().getDirectory().getAbsolutePath();
      for (String each : readLines) {
        if (!each.startsWith("*sys-package-mgr")) {
          each = each.replace(path, "${NGRINDER_HOME}");
          output.append(each).append("\n");
        }
      }
      return output.toString();
    } catch (IOException e) {
      LOG.error("Error while distributing files on {} for {}", user, scriptEntry.getPath());
      LOG.error("Error details ", e);
    }
    return StringUtils.EMPTY;
  }