#include "isinst.h"
#include "depinst.h"
#include "repo.h"
#include "diff.h"

int checkinst(const char *pkg) {
  int found = 0;
  int installed = 0;

  // PACKAGE.txtに存在するかどうかを確認
  char fullpkgname[1024];
  if (findpkgver(pkg, fullpkgname, sizeof(fullpkgname)) == 1) {
    found = 1;
  }

  // /var/db/pkgに存在するかどうかを確認
  Package *instpkg = malloc(MAX_PACKAGES * sizeof(Package));
  int cntinstpkg = 0;
  list_instpkg(instpkg, &cntinstpkg, MAX_PACKAGES);

  for (int i = 0; i < cntinstpkg; i++) {
    if (strcmp(instpkg[i].name, pkg) == 0) {
      installed = 1;
      break;
    }
  }

  free(instpkg);

  // 結果
  if (found == 0) { // 存在しない
    return -1;
  } else {
    if (installed == 0) { // 未インストール
      return 1;
    } else { // インストール済み
      return 0;
    }
  }
}