/*
   * (non-Javadoc)
   *
   * @see com.samsung.sec.dexter.core.plugin.IDexterPlugin#init()
   */
  @Override
  public void init() {
    initFolders();
    copyCppcheckRunModule();
    cppcheck.initCheckerConfig();
    cppcheck.initBaseCommand();

    if (DexterUtil.getOS() == DexterUtil.OS.LINUX || DexterUtil.getOS() == DexterUtil.OS.MAC) {
      checkCppcheckPermission();
    }
  }
  private void copyCppcheckRunModule() {
    String dexterHome = DexterConfig.getInstance().getDexterHome();
    if (Strings.isNullOrEmpty(dexterHome)) {
      throw new DexterRuntimeException(
          "Can't initialize Cppcheck plugin, because the dexter_home is not initialized");
    }

    // copy %DEXTER_HOME%/bin/cppcheck
    String zipFilePath = dexterHome;
    String cppcheckPath = "";

    if (DexterUtil.getOS() == DexterUtil.OS.WINDOWS) {
      zipFilePath += "/temp/cppcheck-windows_" + PLUGIN_VERSION + ".zip";
      cppcheckPath = "/cppcheck-windows.zip";
    } else { // LINUX or MAC
      if (DexterUtil.getBit() == DexterUtil.BIT._32) {
        zipFilePath += "/temp/cppcheck-linux_" + PLUGIN_VERSION + "_32.zip";
        cppcheckPath = "/cppcheck-linux-32.zip";
      } else {
        zipFilePath += "/temp/cppcheck-linux_" + PLUGIN_VERSION + "_64.zip";
        cppcheckPath = "/cppcheck-linux-64.zip";
      }
    }

    final File file = new File(zipFilePath);
    if (!file.exists()) {
      final InputStream is = getClass().getResourceAsStream(cppcheckPath);
      if (is == null) {
        throw new DexterRuntimeException("can't find cppcheck.zip file: " + cppcheckPath);
      }

      try {
        FileUtils.copyInputStreamToFile(is, file);
        DexterUtil.unzip(zipFilePath, CPPCHECK_HOME_DIR);
      } catch (Exception e) {
        throw new DexterRuntimeException(e.getMessage(), e);
      } finally {
        try {
          is.close();
        } catch (IOException e) {
          // do nothing
        }
      }
    }
  }
  private void initFolders() {
    final String dexterHome = DexterConfig.getInstance().getDexterHome();
    final String tempFolder = dexterHome + DexterUtil.FILE_SEPARATOR + "temp";

    DexterUtil.createDirectoryIfNotExist(tempFolder);

    CPPCHECK_HOME_DIR =
        dexterHome + DexterUtil.FILE_SEPARATOR + "bin" + DexterUtil.FILE_SEPARATOR + "cppcheck";

    if (new File(CPPCHECK_HOME_DIR).exists() == false) {
      throw new DexterRuntimeException("There is no cppcheck home folder : " + CPPCHECK_HOME_DIR);
    }
  }
  public void createResultFileFullPath() {
    assert Strings.isNullOrEmpty(getProjectName()) == false;
    assert Strings.isNullOrEmpty(getProjectFullPath()) == false;
    assert Strings.isNullOrEmpty(getSourceFileFullPath()) == false;

    this.resultFileFullPath =
        DexterConfig.getInstance().getDexterHome()
            + "/"
            + DexterConfig.RESULT_FOLDER_NAME
            + "/daemon/";

    if (sourceFileFullPath.startsWith("\\") || sourceFileFullPath.startsWith("/")) {
      this.resultFileFullPath += sourceFileFullPath;
    } else if (sourceFileFullPath.indexOf(":") == 1) {
      this.resultFileFullPath += sourceFileFullPath.substring(3);
    }

    this.resultFileFullPath = DexterUtil.refinePath(this.resultFileFullPath);
  }
 public void setResultFileFullPath(String resultFileFullPath) {
   this.resultFileFullPath = DexterUtil.refinePath(resultFileFullPath);
 }
 public void setSourceFileFullPath(String sourceFileFullPath) {
   this.sourceFileFullPath = DexterUtil.refinePath(sourceFileFullPath);
 }
 @Override
 public void writeXml2ResultFilePrefix(final File file) throws IOException {
   Files.append(
       "<dexter-result created=\"" + DexterUtil.currentDateTime() + "\">\n", file, Charsets.UTF_8);
 }