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

#define CONF_SQL "INSERT INTO t_rms_rc_server_index (rc_server_seq, rcid, name, ip, port, type, reg_datetime, update_datetime, monitor_yn, deleted_yn, etc1, etc2, etc3, raid_stat, raid_update_updatetime) VALUES ('','%s','%s','%s','%s','%s',now() ,now(), 'Y','N','%s','%s','0',0, now());\n"

int main()
{
	int devide(char **, const char *, char);
	char *get_serv_type(char *);
	char *get_serv_order(char *);

	FILE *ifp;
	char delim1 = '=';
	char delim2 = ':';
	char delim3 = '_';
	char strbuff[1024];
	char s_strbuff[512];
	char ss_strbuff[256];
	char *basket[1024];
	char *s_basket[512];
	char *ss_basket[256];
	int i, num, snum, ssnum;
	long leng;
	
	ifp = fopen("./sh_statusd.conf", "r");
	memset(strbuff, 0, 1024);

	while ( fgets(strbuff, 1024, ifp) )
	{
		leng = strlen(strbuff);
		strbuff[leng - 1] = '\0';
		// parsing to delim '='
		num = devide(basket, strbuff, delim1);
		
		if (strbuff[0] == '#' || strcmp(basket[0], "SERV"))
			continue;

		// parsing to delim ':'
		strncpy(s_strbuff, basket[1], 512);
		snum = devide(s_basket, s_strbuff, delim2);
		
		// parsing to delim '_'
		strncpy(ss_strbuff, s_basket[0], 256);
		ssnum = devide(ss_basket, ss_strbuff, delim3);
		
		// making sql(MySQL) string
		fprintf(stdout, CONF_SQL, ss_basket[0], s_basket[0], s_basket[1], s_basket[2], s_basket[3], get_serv_type(s_basket[3]), get_serv_order(ss_basket[0]));
		memset(strbuff, 0, 1024);
		memset(s_strbuff, 0, 512);
		memset(ss_strbuff, 0, 256);
	}

	for (i = 0; i < num; i++)
		free(basket[i]);
	
	/*
	for (i = 0; i < snum; i++)
		free(s_basket[i]);
	
	for (i = 0; i < ssnum; i++)
		free(ss_basket[i]);
	*/
	exit(1);
}

char *get_serv_type(char *stype)
{
	static char *type;
	
	type = (char *)malloc(2);
	
	if (!strcmp(stype, "RC"))
		strncpy(type, "A", 2);
	else if (!strcmp(stype, "STAT"))
		strncpy(type, "B", 2);
	else if (!strcmp(stype, "RTS"))
		strncpy(type, "C", 2);
	else if (!strcmp(stype, "RCTS"))
		strncpy(type, "D", 2);
	else if (!strcmp(stype, "RCDB"))
		strncpy(type, "E", 2);

	return type;
}

char *get_serv_order(char *rcid)
{
	static char *order;

	order = (char *)malloc(3);

	if (!strcmp(rcid, "TIME"))
		strncpy(order, "13", 3);
	else if (!strcmp(rcid, "NCSOFT"))
		strncpy(order, "12", 3);
	else if (!strcmp(rcid, "OLSOFT"))
        strncpy(order, "11", 3);
	else if (!strcmp(rcid, "INCOK"))
        strncpy(order, "10", 3);
	else if (!strcmp(rcid, "FREEZONE"))
       strncpy(order, "9", 3);
	else if (!strcmp(rcid, "JJANGHARD"))
        strncpy(order, "8", 3);
	else if (!strcmp(rcid, "KTINSIDE"))
        strncpy(order, "7", 3);
	else if (!strcmp(rcid, "EVENT"))
        strncpy(order, "6", 3);
	else if (!strcmp(rcid, "ORGEL"))
        strncpy(order, "5", 3);
	else if (!strcmp(rcid, "RC004"))
        strncpy(order, "4", 3);
	else if (!strcmp(rcid, "RC003"))
        strncpy(order, "3", 3);
	else if (!strcmp(rcid, "SORIBADA"))
        strncpy(order, "2", 3);
	else if (!strcmp(rcid, "0UDISK"))
        strncpy(order, "1", 3);

	return order;
}

int devide(char **val, const char *str, char delim)
{
	char *p;
	int i, j;

	val[0] = (char *)malloc(512);
	memset(val[0], 0, 512);

	for (p=str, i=0, j=0; *p != '\0'; p++)
	{
		if (*p == delim)
		{
			*(val[i] + j) = '\0';
			j = 0;
			i++;
			val[i] = (char *)malloc(sizeof(char) * 2 + 1);
			memset(val[i], 0, sizeof(char)*2+1);
			continue;
		}
		else
		{
			*(val[i] + j++) = *p;
		}
	}
	*(val[i] + j) = '\0';
	return (i+1);
}
