//////////////////////////////////////////////////////
// readmenucfg.c
//
//  Lee Juhyun
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mainlib.h"
#include "varlib.h"

extern 	menu_t 		*menudata;
extern	shortkey_t	*shortkey;

void read_menu_cfg(void);

extern error_read_menu(void);

void read_menu_cfg(void) {

	FILE *fp;
	char *lasts, *lasts2;
	char *next_token;
	char *next_token_key;
	char line[INS_MAX];
	char *templine;
	int n_submenu[255];
	int count, i = 0;
	int index;
	int altkey = 609;
	int key;
	int totshortcut = 0;
	char temppath[1024];


	for (i =0 ; i < 255; i++)
		n_submenu[i] = 0;

	strcpy(temppath, "/etc/lls/llsmenucfg");

	fp = fopen(temppath, "r");

	menudata = (menu_t *)malloc(sizeof(menu_t));
	
	while(fgets(line, 255, fp) != NULL) {
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if ((char)templine[0] == '#') continue;
		if ((strcmp(templine, "SHORTCUTCFG\n") == 0) || ((strcmp(templine, "END\n") == 0))) break;
		n_submenu[index = atoi(next_token = strtok_r(templine, "::", &lasts))]++;
	}

	menudata->n_submenu = (int *)malloc(sizeof(int) * n_submenu[0]);
	menudata->submenu = (menuitem_t **)malloc(sizeof(menuitem_t *) * n_submenu[0]);

	for (i = 0 ; i < n_submenu[0]; i++) {
		*(menudata->submenu + i) = (menuitem_t *)malloc(sizeof(menuitem_t) * (n_submenu[i] + 1));
	}

	rewind(fp);
	while(1) {
		fgets(line, STRING_MAX, fp);
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if (strcmp(templine, "MENUCFG\n") == 0) break;
	}
	
	while(fgets(line, 255, fp) != NULL) {
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if ((char)templine[0] == '#') continue;
		if ((strcmp(templine, "SHORTCUTCFG\n") == 0) || ((strcmp(templine, "END\n") == 0))) break;
		count = menudata->n_submenu[index = atoi(next_token = strtok_r(templine, "::", &lasts))]++;
		menudata->submenu[index][count].p_key = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(menudata->submenu[index][count].p_key, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		menudata->submenu[index][count].key = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(menudata->submenu[index][count].key, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		menudata->submenu[index][count].name = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(menudata->submenu[index][count].name, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		menudata->submenu[index][count].desc = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(menudata->submenu[index][count].desc, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		menudata->submenu[index][count].desc2 = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(menudata->submenu[index][count].desc2, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		if (next_token != NULL) {
			menudata->submenu[index][count].ins = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
			strcpy(menudata->submenu[index][count].ins, next_token);
		}
	}
	
	rewind(fp);
	while(1) {
		fgets(line, 255, fp);
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if (strcmp(templine, "SHORTCUTCFG\n") == 0) break;
	}


	while(fgets(line, 255, fp) != NULL) {
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if ((char)templine[0] == '#') continue;
		if ((strcmp(templine, "MENUCFG\n") == 0) || ((strcmp(templine, "END\n") == 0))) break;
		totshortcut++;
	}
	shortkey = (shortkey_t *)calloc(totshortcut + 1, sizeof(shortkey_t));
	
	rewind(fp);
	while(1) {
		fgets(line, 255, fp);
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if (strcmp(templine, "SHORTCUTCFG\n") == 0) break;
	}

	i = 0;
	while(fgets(line, 255, fp) != NULL) {
		templine = (char *)malloc(sizeof(char) * (strlen(line)));
		strcpy(templine, line);
		if ((char)templine[0] == '#') continue;
		if ((strcmp(templine, "MENUCFG\n") == 0) || ((strcmp(templine, "END\n") == 0))) break;
		next_token = strtok_r(line, "::", &lasts);
		shortkey[i].flag = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(shortkey[i].flag, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		shortkey[i].ins = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(shortkey[i].ins, next_token);
		next_token = strtok_r(NULL, "::", &lasts);
		
		templine = (char *)malloc(sizeof(char) * (strlen(next_token) + 1));
		strcpy(templine, next_token);
		sscanf(next_token, "(%s)", templine);

		next_token_key = strtok_r(templine, "+", &lasts2);
		
		if (strcmp(next_token_key, "alt") == 0) {
			key = altkey;
			next_token_key = strtok_r(NULL, "+", &lasts2);
			shortkey[i].shortkey = key + (char)next_token_key[0]; 
		}
		else {
			key = 0;
			shortkey[i].shortkey = key + (char)next_token_key[0]; 
		}
		
		i++;
	}
	free(templine);
}
