#! /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@
--- whohas-0.23.orig/program/whohas
+++ whohas-0.23/program/whohas
@@ -72,6 +72,7 @@
 my $ubuntu_bool		= 1;
 my $gentoo_bool		= 1;
 my $fedora_bool		= 1;
+my $opkg_bool		= 1;
 
 my @thrs;
 my $here = 0;
@@ -122,6 +123,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;
@@ -139,8 +141,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, cygwin, 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, cygwin, debian, fedora, fink, freebsd, gentoo, macports, netbsd, openbsd, opensuse, slackware, sourcemage, ubuntu, opkg\n";
 		}
 	}
 }
@@ -198,6 +201,9 @@
 		if ($cygwin_bool == 1) {
 			$thrs[$here++] = threads->new(\&cygwin,		$ARGV[0]);
 		}
+		if ($opkg_bool == 1) {
+			$thrs[$here++] = threads->new(\&opkg,		$ARGV[0]);
+		}
 		foreach (@thrs) {
 			$_->join;
 		}
@@ -252,6 +258,9 @@
 	if ($ubuntu_bool == 1) {
 		&ubuntu(	$ARGV[0]);
 	}
+	if ($opkg_bool == 1) {
+		&opkg(		$ARGV[0]);
+	}
 }
 
 sub fedora {
@@ -633,6 +642,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 search_by_name { # versions, sizes, dates, repos, urls
 	my ($p1,$p2,$p3,$p4,$p5,$p6,$search) = @_;
--- whohas-0.23.orig/usr/share/man/man1/whohas.1
+++ whohas-0.23/usr/share/man/man1/whohas.1
@@ -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, Mandriva, openSUSE, Slackware (and linuxpackages.net), Source Mage, Ubuntu, FreeBSD, NetBSD, OpenBSD, Fink, MacPorts and Cygwin distributions.
+whohas is a command line tool to query package lists from the Arch, Debian, Fedora, Gentoo, Mandriva, openSUSE, Slackware (and linuxpackages.net), Source Mage, Ubuntu, FreeBSD, NetBSD, OpenBSD, Fink, MacPorts, Opkg (embedded) and Cygwin 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", "cygwin", "debian", "fedora", "fink", "freebsd", "gentoo", "mandriva", "macports", "netbsd", "openbsd", "opensuse", "slackware", "sourcemage", and "ubuntu".
+Queries only for packages for the listed distributions. Recognised values for Dist1, Dist2, etc. are "archlinux", "cygwin", "debian", "fedora", "fink", "freebsd", "gentoo", "mandriva", "macports", "netbsd", "openbsd", "opensuse", "slackware", "sourcemage", "ubuntu", and "opkg".
 .TP 
 \fBpkgname\fR
 Package name to query for
