/////
//
// File : absec_chown.c
// Author : Jean-Luc Cyr
// Date : 2018-10
//
// Description: tool to change ownership from mysql database
//

#include <stdio.h>
#include <string.h>
#include <mysql.h>

////////////////////////////////////////////////////////////////
// mysql call
int mysql_update(char *url, int gid)
{
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;

   char *server = "localhost";
   char *user = "jlcyr";
   char *password = "password"; /* set me first */
   char *database = "absec";

   conn = mysql_init(NULL);

   /* Connect to database */
   if (!mysql_real_connect(conn, server,
         user, password, database, 0, NULL, 0)) {
      printf("%s\n", mysql_error(conn));
      return(0);
   }

    char query[256];
    sprintf(query, "update urls set gid=%d where url='%s'", gid, url); // where url='%s'", r->uri);
    //printf("update urls set gid=%d where url='%s'\r\n", gid, url); // where url='%s'", r->uri);
    /* send SQL query */
    //if (mysql_query(conn, "show tables")) {
    if (mysql_query(conn, query)) {
      printf("%s\n", mysql_error(conn));
      return(-1);
    }

   mysql_close(conn);
   return(0);
}

void main(int argc, char** argv)
{
    int perms = 0;
    switch (argc) {
      default:
        printf("Usage: abchgrp <uid> <url>\r\n\r\n");
        exit(1);
      break;
      case 3:
        printf("Updating group ownership on %s to %d\r\n", argv[2], atoi(argv[1]));
        mysql_update(argv[2], atoi(argv[1]));
      break;
    }
}