package com.github.aureliano.achmed.os.pkg;

import org.apache.log4j.Logger;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.PackageResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.resources.properties.PackageProperties;

public class DpkgPackageManager implements IPackageManager {
	
	private static final Logger logger = Logger.getLogger(DpkgPackageManager.class);
	
	private static final String DPKG_QUERY = "/usr/bin/dpkg-query";

	protected PackageProperties properties;
	
	public DpkgPackageManager() {
		super();
	}

	public CommandResponse install() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public CommandResponse uninstall() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public String latest() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}
	
	public boolean isInstalled() {
		final String desiredStatus = "install ok installed";
		CommandResponse res = CommandFacade.executeCommand(DPKG_QUERY, "-s", this.properties.getName());
		
		if (!res.isOK()) {
			logger.warn("Command [ " + res.getCommand() + " ] returned non zero status: " + res.getExitStatusCode() + " Error message: " + res.getError());
			return false;
		}
		
		String[] match = StringHelper.match("Status:\\s(\\S+\\s\\S+\\s\\S+)", res.getOutput());
		if (match == null) {
			throw new PackageResourceException("Failed to match dpkg-query [" + res.getOutput() + "]");
		}
		
		return desiredStatus.equalsIgnoreCase(match[1]);
	}

	public void setPackageProperties(PackageProperties properties) {
		this.properties = properties;
	}

	public PackageProperties getPackageProperties() {
		return this.properties;
	}
}
@@ -1246,70 +1246,30 @@
 %0A%09%09%09
-logger.warn(%22Command %5B %22 + res.getCommand() + %22 %5D returned
+// query exits with
  no
-n
  zer
@@ -1280,77 +1280,35 @@
 atus
-: %22 + res.getExitStatusCode() + %22 Error message: %22 + res.getError());
+ when package is not found.
 %0A%09%09%09

Remove annoying warning.