+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "sqlite3.h"
+#include "zpm.h"
+
+int zpm_script_hash(struct zpm *zpm, char *pkgstr, char *phase, char *hash) {
+ char *pkgid;
+ char *template = "select hash from scripts_pkgid where pkgid = %q and phase = %q";
+ sqlite3_stmt *st;
+
+ pkgid = zpm_findpkg(zpm, pkgstr);
+
+ st = zpm_dbquery(zpm, template, pkgid, phase);
+ free(pkgid);
+
+ if (!st) {
+ return 0;
+ }
+
+ switch (sqlite3_step(st)) {
+ case SQLITE_ROW:
+ strncpy(hash, (const char *) sqlite3_column_text(st, 0),
+ ZPM_HASH_STRLEN);
+ hash[ZPM_HASH_STRLEN] = 0;
+ break;
+ default: zpm->error = 1; /* fall through */
+ case SQLITE_DONE:
+ sqlite3_finalize(st);
+ return 0; break; /* not found */
+ }
+
+ sqlite3_finalize(st);
+ return 1;
+}