+struct pkgloc *find_atleast_package(char *pkgstr, struct search_ctl *opt, int stopifinstalled) {
+ char *latest = 0;
+ char *installed = 0, *found = 0;
+ char *pkgfile = 0;
+ struct pkgloc *pkg = 0;
+ int rv;
+ unsigned int i;
+
+ if (opt->localdb) {
+ installed = zpm_findpkg_range(opt->zpmdb, pkgstr, 0, "status = 'installed'", 0);
+ if (installed) {
+ latest = installed;
+ pkgfile = opt->zpmdb->path;
+ if (stopifinstalled) {
+ pkg = malloc(sizeof *pkg);
+ pkg->id = strdup(latest);
+ pkg->file = pkgfile;
+ pkg->installed = 1;
+ return pkg;
+ }
+ }
+ if (!opt->onlylocalinstalled) {
+ found = zpm_findpkg_range(opt->zpmdb, pkgstr, 0, NULL, 0);
+ if (found) {
+ rv = zpm_vercmp(found, latest);
+ if (rv == 1) {
+ latest = found;
+ pkgfile = opt->zpmdb->path;
+ }
+ }
+ }
+ }
+
+ for (i = 0; i < opt->repos.gl_pathc; i++) {
+ if (!opt->matchallpkgfile
+ && !strstr(opt->repos.gl_pathv[i], pkgstr)
+ && !strstr(opt->repos.gl_pathv[i], ".repo")
+ ) {
+ continue;
+ }
+ found = checkfile(pkgstr, opt->repos.gl_pathv[i], 1);
+ if (found) {
+ rv = zpm_vercmp(found, latest);
+ if (rv == 1) {
+ latest = found;
+ pkgfile = strdup(opt->repos.gl_pathv[i]);
+ }
+ }
+ }
+
+ if (latest && pkgfile) {
+ pkg = malloc(sizeof *pkg);
+ pkg->id = strdup(latest);
+ pkg->file = pkgfile;
+ pkg->installed = 0;
+ if (installed) {
+ pkg->installed = !strcmp(latest, installed);
+ }
+ }
+
+ return pkg;
+}
+