/*
 * setup.c
 * (C) Peter Salanki 2002
 * This program is copyright, and covered by the Gnu Public License.
 * The Natasha log to mIRC log converter.
 * sorcer@linux.se
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <mysql/mysql.h>
#include <string.h>
#include "../../src/settings.h"

int dbquery (char *query);
void dbconnect (void);
char *escapequery (char *query);

MYSQL mysql, *sock;
MYSQL_RES *res;
MYSQL_ROW row;

void dbconnect (void) {
  if (!(sock = mysql_real_connect(&mysql,MYSQL_SERVER,MYSQL_USER,MYSQL_PASS,MYSQL_DB,MYSQL_PORT,NULL,0)))
  {
    fprintf(stderr,"Couldn't connect to MySQL server!\n%s\n\n", mysql_error(&mysql));
    perror("");     
    exit(1);
  }
}

int dbquery (char *query) {

  if(mysql_query(sock, query))
    {
#ifdef DEBUG
      printf("Query failed (%s)\n", mysql_error(sock));
      printf("Query: %s\n", query);
#endif
      return 0;
    } else {
      res = mysql_store_result(sock);
      return 1;
    }
}

char *escapequery (char *query) {
  
  char *escaped = malloc(2*strlen(query));
  mysql_real_escape_string(&mysql, escaped, query, strlen(query));
  
  return escaped;
}

int main (void) {
  char msg[512];
  char tmp1[512];
  char tmp2[512];
  char tmp3[512];

  printf("Natasha initial setup tool v1.0.\nThis tool will help you setup the MySQL database.\n\n");

  /* Step 1: MySQL connection */
  printf("\tStep 1: MySQL connection\nConnecting to MySQL database with settings defined in settings.h.\n");
  mysql_init(&mysql);
  dbconnect();
  printf("Connection successfull.\n\n");
  
  /* Step 2: Create database structure */
  printf("\tStep 2: Create database structure\nExecuting MySQL query to create database structure. You will have to enter your database password.\n");

  snprintf(msg, 512, "mysql -h %s -p %s -u %s -P %i --password < natasha.sql", MYSQL_SERVER, MYSQL_DB, MYSQL_USER, MYSQL_PORT);
  system(msg);

  printf("Database structure created.\n\n");

  /* Step 3: Add main ARM */
  printf("\tStep 3: Add main ARM\nYour main ARM is the ARM that has op in pubchannel/homechannel. It handles op/voice of techs/helpers, home channel protection, dynamic pubchannel/homechannel welcomes and so on.\n\n");
  

 addarm:
  printf("Enter the nick you want for your main ARM: ");
  gets(tmp1);

  snprintf(msg, 512, "INSERT INTO `arms` (`id`, `nick`, `type`, `sid`) VALUES ('1', '%s', '36', '%s')", tmp1, SID);
  if(dbquery(msg)) printf("Main ARM added successfully.\n\n");
  else { 
    printf("Insert query failed, maybe you entered a faulty nick?\n");
    goto addarm;
  }
    
  /* Step 3: Add main service */
  printf("\tStep 3: Add main service\nData about all the services in a servicenet is stored in a table called services on the main service. This data is used for stats generation, but is not really needed before the servicenet support is done.\n\n");
  
  addservice:
  printf("What is the name of this service (ex. Main service): ");
  gets(tmp1);
  printf("Who is the admin of this service (ex. Sorcer): ");
  gets(tmp2);
  printf("What is the public ip/host of this service (ex. 212.28.87.76): ");
  gets(tmp3);

  snprintf(msg, 512, "INSERT INTO `services` (`name`, `admin`, `host`, `port`, `statsdb_ip`, `statsdb_port`, `statsdb_db`, `statsdb_user`, `statsdb_pass`) VALUES ('%s', '%s', '%s', '%i', '%s', '%i', '%s', '%s', '%s');", tmp1, tmp2, tmp3, 4865, MYSQL_LOGSERVER, LOGMYSQL_PORT, MYSQL_LOGDB, MYSQL_LOGUSER, MYSQL_LOGPASS);
  if(dbquery(msg)) printf("Service added successfully.\n\n");
  else { 
    printf("Insert query failed, maybe you entered some faulty data?\n");
    goto addservice;
  }

  /* Step 4: Add you as Master Technician */ 
  printf("\tStep 4: Add you as Master Technician\nYou will now be added as a Master Technician for this service. You will be added in the admins table aswell, so you can use the web based admin interface.\n\n");
  
  addadmin:
  printf("Enter your Q auth (ex. Sorcer): ");
  gets(tmp1);
  printf("Enter your real name (ex. Peter Salanki): ");
  gets(tmp2);
  printf("Enter the password you want for the web based admin interface (ex. somepass): ");
  gets(tmp3);

  snprintf(msg, 512, "INSERT INTO `users` (`id`, `handle`, `auth`, `lastseen`) VALUES ('1', '%s', '21', '0')", tmp1);
  if(dbquery(msg)) printf("Added into users table.\n");
  else { 
    printf("Insert query failed, maybe you entered some faulty Q auth?\n");
    goto addadmin;
  }

  snprintf(msg, 512, "INSERT INTO `admins` (`uid`, `dispnick`, `password`, `realname`, `comments`) VALUES ('1', '%s', '%s', '%s', 'Service main admin')", tmp1, tmp3, tmp2);
  if(dbquery(msg)) printf("Added into admins table.\n\n");
  else { 
    printf("Insert query failed, maybe you entered some faulty real name/password?\n");
    dbquery("DELETE FROM `users` WHERE `id` = '1'");
    goto addadmin;
  }

  printf("Database setup is complete. Now you can start natasha, and on IRC do /msg <main arm> IDENT to \"log in\".\n");
  
  /* Cleanup */
  mysql_close(sock);
  return 1;
}
