#include <sqlite3.h>
#include <stdio.h>
#include "user.h"

sqlite3 *userDB;
char *err;
int numrows = 0;

static int user_callback(void *none, int argc, char **argv, char **col){
  // we found the user!
  numrows++;
  return 0;
}

int user_checklogin(char *login, char *password) {
  char sql[256];
  int rc;

  numrows = 0;

  if (sqlite3_open("data/users.wou", &userDB)) {
    printf("error: %s\n", sqlite3_errmsg(userDB));
    sqlite3_close(userDB);
    return -1;
  }
  sprintf(sql, "SELECT * FROM users WHERE login='%s' AND password='%s'", login, password);
  rc = sqlite3_exec(userDB, sql, user_callback, NULL, &err);
  if (rc!=SQLITE_OK){
    printf("SQL error: %s\n", err);
    sqlite3_free(err);
  }
  sqlite3_close(userDB);
  if (numrows == 0) return 0;
  return 1;
}
