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

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 String DPKG_QUERY = "/usr/bin/dpkg-query";
	private static final String QUERY_FORMAT = "'${status} ${version}\\n'";

	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 = "installed";
		CommandResponse res = CommandFacade.executeCommand(
			DPKG_QUERY, "-W", "-f", QUERY_FORMAT, this.properties.getName()
		);
		
		if (!res.isOK()) {
			return false;
		}
		
		if (res.getOutput().split("\n").length > 1) {
			throw new PackageResourceException("Found more than one package installed to " + res.getCommand());
		}
		
		String[] match = StringHelper.match("^(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S*)$", res.getOutput());
		if (match == null) {
			throw new PackageResourceException("Failed to match dpkg-query " + res.getCommand());
		}
		
		return desiredStatus.equalsIgnoreCase(match[3]);
	}

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

	public PackageProperties getPackageProperties() {
		return this.properties;
	}
}
@@ -38,16 +38,49 @@
 s.pkg;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 import c
@@ -481,121 +481,132 @@
 nal 
-String DPKG_QUERY = %22/usr/bin/dpkg-query%22;%0A%09private static final String QUERY_FORMAT = %22'$%7Bstatus%7D $%7Bversion%7D%5C%5Cn'
+Logger logger = Logger.getLogger(DpkgPackageManager.class);%0A%09%0A%09private static final String DPKG_QUERY = %22/usr/bin/dpkg-query
 %22;%0A%0A
@@ -1093,16 +1093,27 @@
 atus = %22
+install ok 
 installe
@@ -1169,20 +1169,16 @@
 Command(
-%0A%09%09%09
 DPKG_QUE
@@ -1187,30 +1187,10 @@
 , %22-
-W%22, %22-f%22, QUERY_FORMAT
+s%22
 , th
@@ -1212,19 +1212,16 @@
 etName()
-%0A%09%09
 );%0A%09%09%0A%09%09
@@ -1246,178 +1246,167 @@
 %0A%09%09%09
-return false;%0A%09%09%7D%0A%09%09%0A%09%09if (res.getOutput().split(%22%5Cn%22).length %3E 1) %7B%0A%09%09%09throw new PackageResourceException(%22Found more than one package installed to %22 + res.getCommand())
+logger.warn(%22Command %5B %22 + res.getCommand() + %22 %5D returned non zero status: %22 + res.getExitStatusCode() + %22 Error message: %22 + res.getError());%0A%09%09%09return false
 ;%0A%09%09
@@ -1453,46 +1453,38 @@
 ch(%22
-%5E(%5C%5CS+)
+Status:
 %5C%5Cs
-+
 (%5C%5CS+
-)
 %5C%5Cs
-+(
 %5C%5CS+
-)
 %5C%5Cs
-+(
 %5C%5CS
-*)$
++)
 %22, r
@@ -1589,16 +1589,17 @@
 g-query 
+%5B
 %22 + res.
@@ -1601,25 +1601,30 @@
  res.get
-Command()
+Output() + %22%5D%22
 );%0A%09%09%7D%0A%09
@@ -1675,9 +1675,9 @@
 tch%5B
-3
+1
 %5D);%0A

Change query mechanism to find packages.