#! /bin/sh /usr/share/dpatch/dpatch-run
## 50-opkg-repo.dpatch by Jonathan Wiltshire <debian@jwiltshire.org.uk>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Module for the opkg (OpenMoko) distribution
## DP: Note: depends on other patches like getopts, fetchdoc etc

@DPATCH@
diff -urNad whohas-0.23~/program/whohas whohas-0.23/program/whohas
--- whohas-0.23~/program/whohas	2009-03-11 19:18:11.000000000 +0000
+++ whohas-0.23/program/whohas	2009-03-11 19:27:35.000000000 +0000
@@ -66,6 +66,7 @@
 my $ubuntu_bool		= 1;
 my $gentoo_bool		= 1;
 my $fedora_bool		= 1;
+my $opkg_bool		= 1;
 
 my @thrs;
 my $here = 0;
@@ -115,6 +116,7 @@
 	$ubuntu_bool            = 0;
 	$gentoo_bool            = 0;
 	$fedora_bool            = 0;
+	$opkg_bool		= 0;
 	@option_distro = split(/,/,join(',',@option_distro));
 	for my $distro (@option_distro) {
 		if (            $distro =~ /archlinux/i) {      $arch_bool              = 1;
@@ -131,8 +133,9 @@
 		} elsif (       $distro =~ /ubuntu/i) {         $ubuntu_bool            = 1;
 		} elsif (       $distro =~ /gentoo/i) {         $gentoo_bool            = 1;
 		} elsif (       $distro =~ /fedora/i) {         $fedora_bool            = 1;
+		} elsif (	$distro =~ /opkg/i) {		$opkg_bool		= 1;
 		} else {
-			warn "Repository or distribution specified has not been recognised. You specified: $distro. Recognised options are: archlinux, debian, fedora, fink, freebsd, gentoo, macports, netbsd, openbsd, opensuse, slackware, sourcemage, ubuntu\n";
+			warn "Repository or distribution specified has not been recognised. You specified: $distro. Recognised options are: archlinux, debian, fedora, fink, freebsd, gentoo, macports, netbsd, openbsd, opensuse, slackware, sourcemage, ubuntu, opkg\n";
 		}
 	}
 }
@@ -187,6 +190,9 @@
 		if ($fedora_bool == 1) {
 			$thrs[$here++] = threads->new(\&fedora,		$ARGV[0]);
 		}
+		if ($opkg_bool == 1) {
+			$thrs[$here++] = threads->new(\&opkg,		$ARGV[0]);
+		}
 		foreach (@thrs) {
 			$_->join;
 		}
@@ -238,6 +244,9 @@
 	if ($ubuntu_bool == 1) {
 		&ubuntu(	$ARGV[0]);
 	}
+	if ($opkg_bool == 1) {
+		&opkg(		$ARGV[0]);
+	}
 }
 
 sub fedora {
@@ -1287,6 +1296,32 @@
 	return();
 }
 
+sub opkg {
+	use XML::Simple qw(:strict);
+
+	my $query = "http://www.opkg.org/api.php?action=search-package&q=".$_[0];
+	my $packages = XMLin(fetchdoc($query), ForceArray =>1, KeyAttr => {}, );
+	my @names;
+	my @versions;
+	my @dates;
+	my @sizes;
+	my @repos;
+	my @urls;
+	my $now = 0;
+
+	foreach my $package (@{$packages->{package}}) {
+		push @names,    $package->{name}->[0];
+		push @versions, $package->{version}->[0];
+		push @repos,    $package->{status}->[0]->{content};
+		push @sizes,    "";
+		push @urls,     "http://www.opkg.org/package_".$package->{id}->[0].".html";
+		push @dates,    $package->{timestamp}->[0];
+	}
+	for (my $i = 0; $i < @names; $i++) {
+		&pretty_print($cols,@columns,"opkg",$names[$i],$versions[$i],$sizes[$i],$dates[$i],$repos[$i],$urls[$i]);
+	}
+	return ();
+}
 
 sub template_query {
 	my $query = "url";
diff -urNad whohas-0.23~/usr/share/man/man1/whohas.1 whohas-0.23/usr/share/man/man1/whohas.1
--- whohas-0.23~/usr/share/man/man1/whohas.1	2009-03-11 19:18:11.000000000 +0000
+++ whohas-0.23/usr/share/man/man1/whohas.1	2009-03-11 19:29:38.000000000 +0000
@@ -7,7 +7,7 @@
 whohas [\fI\-\-no\-threads\fP] [\fI\-\-shallow\fP] [\fI\-\-strict\fP] [\fI\-d Dist1[,Dist2[,Dist3 etc.]]\fP] \fIpkgname\fP
 .SH "DESCRIPTION"
 .LP 
-whohas is a command line tool to query package lists from the Arch, Debian, Fedora, Gentoo, openSUSE, Slackware (and linuxpackages.net), Source Mage, Ubuntu, FreeBSD, NetBSD, OpenBSD, Fink and MacPorts distributions.
+whohas is a command line tool to query package lists from the Arch, Debian, Fedora, Gentoo, openSUSE, Slackware (and linuxpackages.net), Source Mage, Ubuntu, FreeBSD, NetBSD, OpenBSD, Fink, MacPorts and Opkg (embedded) distributions.
 .SH "OPTIONS"
 .LP 
 .TP 
@@ -21,7 +21,7 @@
 Return only packages whose name exactly matches \fIpkgname\fP.
 .TP 
 \fB\-d Dist1[,Dist2[,Dist3 etc.]]\fR
-Queries only for packages for the listed distributions. Recognised values for Dist1, Dist2, etc. are "archlinux", "debian", "fedora", "fink", "freebsd", "gentoo", "macports", "netbsd", "openbsd", "opensuse", "slackware", "sourcemage", and "ubuntu".
+Queries only for packages for the listed distributions. Recognised values for Dist1, Dist2, etc. are "archlinux", "debian", "fedora", "fink", "freebsd", "gentoo", "macports", "netbsd", "openbsd", "opensuse", "slackware", "sourcemage", "ubuntu", and "opkg".
 .TP 
 \fBpkgname\fR
 Package name to query for
