#include <string.h>
#include <sys/types.h>
#include <sys/stat.h> 
#include <fcntl.h>
#include "rest_proxy_conf.h"

void rest_proxy_load_conf_ipport(const char* buffer,
											   const char* key,
											   unsigned int* ip,
											   char* ip_str,
											   unsigned short* port)
{
	char buffer_ip[16] = {0};
	// char buffer_port[4] = {0};
	char *pos = NULL;
	char *pos_ip_start	= NULL;
	char *pos_ip_end	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos)
	{
		pos_ip_start = strstr(pos, "=");
		if(NULL != pos_ip_start)
		{
			pos_ip_end = strstr(pos_ip_start + 1, ":");
			if(NULL != pos_ip_end)
			{
				memcpy(buffer_ip, pos_ip_start + 1, (pos_ip_end - 1) - (pos_ip_start + 1) + 1);
				if(ip_str)
				{
					strcpy(ip_str, buffer_ip);
				}
				*ip = inet_addr(buffer_ip);
				*port = atoi(pos_ip_end + 1);
			}
		}
	}
}

void rest_proxy_load_conf_str(const char* buffer, const char* key, char* val)
{
	char *pos = NULL;
	char *pos_val_start	= NULL;
	/* first address is start of the line */
	pos = strstr(buffer, key);
	if(NULL != pos && buffer == pos && buffer[strlen(key)] == '=')
	{
		pos_val_start = strstr(pos, "=");

		/* remove \r or \n which first appear */
		pos = pos_val_start;
		while(pos)
		{
			if(*pos == '\r' || *pos == '\n' || *pos == '\0')
				break;
			pos++;
		}
		if(pos)
			*pos = '\0';
		
		if(NULL != pos_val_start)
		{
			strcpy(val, pos_val_start + 1);
		}
	}
}
void rest_proxy_load_conf_int(const char* buffer, const char* key, int* val)
{
	char tmp_buf[64] = {0};
	rest_proxy_load_conf_str(buffer, key, tmp_buf);
	if(tmp_buf[0] != '\0')
	{
		*val = atoi(tmp_buf);
	}
}
int rest_proxy_load_cnf(s_restproxy_conf *conf)
{
#define BUFFER_LEN 1024
	FILE*  fd;
	char buffer[BUFFER_LEN] = {0};
	
	if(NULL == conf->cnf_file)
	{
		conf->cnf_file = REST_PROXY_DEFAULT_CONF_FILE;
	}

	/* Create input file descriptor */
	fd = fopen (conf->cnf_file, "r");
	if (fd == NULL)
	{
		perror ("open");
		return 2;
	}

	/* Copy process */
	while((fgets(buffer, BUFFER_LEN, fd)) != NULL)
	{
		if(buffer[0] == '#')
			continue;
		rest_proxy_load_conf_ipport(buffer, "localIPPort",	&conf->local_ip, NULL, &conf->local_port);
		
		rest_proxy_load_conf_ipport(buffer, "queryIPPort",	&conf->query_ip, conf->query_ip_str, &conf->query_port);

        rest_proxy_load_conf_ipport(buffer, "rechargeIPPort",	&conf->recharege_ip, conf->recharge_ip_str, &conf->recharge_port);
    
#ifdef TEST_RESTPROXY
        rest_proxy_load_conf_str(buffer, "test_msisdn", conf->msisdn);
		rest_proxy_load_conf_str(buffer, "test_msisdn_in", conf->msisdn_in);
        rest_proxy_load_conf_str(buffer, "test_pin", conf->pin);
#endif
        rest_proxy_load_conf_str(buffer, "username", conf->username);
        rest_proxy_load_conf_str(buffer, "password", conf->password);
        rest_proxy_load_conf_str(buffer, "subsystem", conf->subsystem);
        rest_proxy_load_conf_str(buffer, "customerIP", conf->customer_ip);

		rest_proxy_load_conf_str(buffer, "url_walletbalance", conf->url_fetchwalletbalance);
		rest_proxy_load_conf_str(buffer, "url_rechargevoucher", conf->url_recharge);

		rest_proxy_load_conf_str(buffer, "url_walletbalance_getscriberid", conf->url_fetchwalletbalance_getsubsid);
		rest_proxy_load_conf_str(buffer, "url_rechargevoucher_getscriberid", conf->url_recharge_getsubsid);
		rest_proxy_load_conf_int(buffer, "query_topup_getscriberid_switch", &conf->b_fetch_subscribid);

		rest_proxy_load_conf_str(buffer, "url_trans_getsubsid_sender", 		conf->url_trans_getsubsid_sender);
		rest_proxy_load_conf_str(buffer, "url_trans_getsubsid_receiver", 	conf->url_trans_getsubsid_receiver);
		rest_proxy_load_conf_str(buffer, "url_trans", 						conf->url_trans);
		
		memset(buffer, 0x00, BUFFER_LEN);
	}
	
	fclose (fd);
	return 0;
}