/*gcc y.c -L/isip/data/research/cavs/util/mysql/lib/mysql -lmysqlclient */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <math.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <mysql.h>

#define HOSTNAME "localhost"
#define USERNAME "dialog"
#define PASSWORD "pass123"
#define DATABASE_NAME "flatroute"

#if 0
Example compiling:
gcc -I/usr/local/mysql/include clear_tables.c  -L/usr/local/mysql/lib -lmysqlclient -lz -lcrypt -lm -o clear_tables
Add on: "-g" for debug.

Example running:
./campus campus_ref.text
Four tables:
address- use 'id'
campusname- use 'id'
route- use 'sid' or 'did'
place- use 'id'
#endif

int run_query(char *query, MYSQL *in_sql);

int main(int argc,char* argv[]) {

  if (argc != 5 && argc != 9) {
    printf("Usage: %s <0/1> <0/1> <0/1> <0/1> [user] [pass] [host] "
	   "[database]\n", argv[0]);
    printf("  Represents in respective order:\n");
    printf("      Clean 'address' table\n");
    printf("      Clean 'campusname' table\n");
    printf("      Clean 'route' table\n");
    printf("      Clean 'place' table\n");
    printf("  1 = CLEAN the table\n");
    printf("  0 = DO NOT clean (basically anything but '1')\n");
    printf(" NOTE: Either include ALL or NONE of user\\pass\\etc\n");
    exit(0);
  }

  int do_address = atoi(argv[1]);
  int do_campusname = atoi(argv[2]);
  int do_route = atoi(argv[3]);
  int do_place = atoi(argv[4]);

  MYSQL *myData;
  //MYSQL_RES *res;
  //MYSQL_ROW row;
  //char SQLQuery[2048];
  //int j,k,x;
  //int exist;
  //unsigned int num_fields;
  //unsigned int i;
  //MYSQL_FIELD *fields;

  //int counter = 0;

  //FILE *fid;
  //char building[120];
  //char name[120];
  //char city[120];
  //char telephone[120];
  //char distance[20];
  //char refname[120];
  //char line[120], directions[120];
  //char filename[256];
  //char buff [2000];

  //int  i_direction = 0, start_point = 0, len = 0, row_count = 0;
  //row_count = 0;

  myData = mysql_init((MYSQL*)0);

  char *user = USERNAME;
  char *pass = PASSWORD;
  char *host = HOSTNAME;
  char *database = DATABASE_NAME;

  if (argc == 9) {
    user = argv[5];
    pass = argv[6];
    host = argv[7];
    database = argv[8];
  }
  
  if (mysql_real_connect
      (myData,
       //"isip221.isip.msstate.edu","gao","qaz1234","",0,NULL,0)) {
       //HOSTNAME, USERNAME, PASSWORD,"",0,NULL,0)) {
       host, user, pass, "", 0, NULL, 0)) {	 
	 //mysql_select_db(myData,"flatroute");
	 //mysql_select_db(myData,DATABASE_NAME);
	 mysql_select_db(myData, database); 
	 
	 printf("Succesfully connected ... \n\n\n");
       }
  else {
    printf("Unable to connect....\n");
    exit(0);
  }
     
  if (do_address == 1) {
    run_query("delete from address where id != 100000", myData);
  }

  if (do_campusname == 1) {
    run_query("delete from campusname where id != 100000", myData);
  }

  if (do_route == 1) {
    run_query("delete from route where sid != 100000 or did != 100000", 
	      myData);
  }

  if (do_place == 1) {
    run_query("delete from place where id != 100000", myData);
  }
  
  
  return 0;
}

int run_query(char *query, MYSQL *in_sql) {
  int x = mysql_real_query(in_sql, query, strlen(query));
  //printf("x returned %i\n",x);
  // Returns 0 on successful so..
  if (x != 0) {
    printf("Error executing [%s]. Attempting next query\n", query);
  }
  else {
    printf("Query [%s] executed successfully!\n", query);
  }

  return 0;
}
  
