#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <ctype.h>
#include <conio.h>
#include <process.h>
#include <graph.h>
#include <bios.h>
#include <sys\types.h>
#include <sys\stat.h>
#include "gsmenu1.h"
#include "mouse.h"

struct MENUCOLOR mnuAtrib =
{
    _TRED, _TWHITE, _TBLUE, _TLIGHTYELLOW, _TLIGHTBLUE,
    _TBLUE, _TBLUE, _TWHITE, _TBLUE, _TWHITE, _TCYAN,
    TRUE,
    (unsigned char)'Ú', (unsigned char)'¿', (unsigned char)'Ù', (unsigned char)'À',
    (unsigned char)'³', (unsigned char)'Ä'
};

/*struct PASSED passed;*/

struct GSMENU gsmenu;

short rowMid, colMid, ch;
/*short flag;*/

char cmdline[MAXLEN1];
char string[MAXCMDLEN+MAXLEN1];

/*int __far *src;
struct PASSED __far *dst;*/

int off1, off2;

void main(int argc, char **argv)
{
    FILE *f1, *fopen();
    int num,n,LAST,flag;
    char line[201];
    char *df, *bf;
    char dfile[80],bfile[80];
    unsigned long dtime,btime;
    long etime;
    struct _stat buf;

/*    dst = &passed;
    sscanf(argv[1],"%lp",&src);
    _fmemmove(dst,src,sizeof(passed));*/

    df = strrchr(argv[0],'\\');
    if(df != NULL)
    {
         num = df - argv[0] + 1;
         strncpy(dfile,argv[0],num);
         dfile[num-1] = '\\';
         dfile[num] = '\0';
    }
    bf = (char *) getenv( "GSMENUDATA" );
    strcpy(bfile,bf);
    if( bf == NULL )
         strcpy(bfile,"GSMENU.DAT");
    if(df != NULL)
        strcat(dfile,bfile);
    else
        strcpy(dfile,bfile);
    df = strrchr(dfile,'.');
    num = df - dfile + 1;
    strncpy(bfile,dfile,num);
    bfile[num-1] = '.';
    bfile[num] = '\0';
    strcat(bfile,"BIN");
    if(_stat(dfile,&buf) != 0)
    {
        printf("Can't find %s\n",dfile);
        exit(0);
    }
    dtime = buf.st_mtime;
/*  printf("%s last modified %s\n",dfile,ctime(&dtime));*/
    if(_stat(bfile,&buf) == 0)
    {
        btime = buf.st_mtime;
        etime = btime - dtime;
/*      printf("%s last modified %s\n",bfile,ctime(&btime));
        printf("time difference = %ld seconds\n",etime);
        getchar();*/
        if(etime > 0) exit(0);
    }
    if((f1 = fopen( dfile, "r")) == NULL)
    {
        printf("Can't open %s\n",dfile);
        exit(0);
    }
    printf("Updating %s\n",bfile);
    flag=0;
    fgets(line,201,f1);
    getstr(gsmenu.banner1,line,MAXLEN);
    off1 = strlen(gsmenu.banner1)/2;
    fgets(line,201,f1);
    getstr(gsmenu.banner2,line,MAXLEN);
    off2 = strlen(gsmenu.banner2)/2;
    fgets(line,201,f1);
    mnuAtrib.fgBorder = getint(line);
    mnuAtrib.fgNormal = getint(line);
    mnuAtrib.fgSelect = getint(line);
    mnuAtrib.fgNormHilite = getint(line);
    mnuAtrib.fgSelHilite = getint(line);
    fgets(line,201,f1);
    mnuAtrib.bgBorder = getint(line);
    mnuAtrib.bgNormal = getint(line);
    mnuAtrib.bgSelect = getint(line);
    mnuAtrib.bgNormHilite = getint(line);
    mnuAtrib.bgSelHilite = getint(line);
    mnuAtrib.bgScreen = getint(line);
    while(fgets(line,201,f1) != NULL)
    {
        num = getint(line);
        if(num >> MAXMENU)
        {
            printf("Too many menus in %s\n",dfile);
            exit(1);
        }
        if(num == 0)
        {
            if(flag != 0)
            {
                printf("ERROR in %s: Blank line or menu 0 defined twice\n",dfile);
                exit(0);
            }
            flag=1;
        }
        getstr(gsmenu.menu[num].title,line,MAXLEN);
        n=0;
        LAST=0;
        while(LAST == 0 && fgets(line,201,f1) != NULL)
        {
            gsmenu.menu[num].item[n].iHilite = getint(line);
            getstr(gsmenu.menu[num].item[n].achItem,line,MAXLEN);
            if(strlen(gsmenu.menu[num].item[n].achItem) == 0) LAST = 1;
            gsmenu.menu[num].action[n].ret = getint(line);
            gsmenu.menu[num].action[n].menucall = getint(line);
            gsmenu.menu[num].action[n].systemcall = getint(line);
            gsmenu.menu[num].action[n].prompt = getint(line);
            gsmenu.menu[num].action[n].pause = getint(line);
            getstr(gsmenu.menu[num].action[n].command,line,MAXCMDLEN);
            getstr(gsmenu.menu[num].action[n].example,line,MAXLEN1);
/*            getstr(gsmenu.menu[num].action[n].deflt,line,MAXLEN1);*/
            n=n+1;
            if(n >> MAXITEM)
            {
                printf("Too many entries in menu %d",num);
                exit(1);
            }
        }
    }
    fclose(f1);
    if((f1 = fopen( bfile, "wb")) == NULL)
    {
        printf("Can't open %s\n",bfile);
        exit(0);
    }
    fwrite(&mnuAtrib,sizeof(mnuAtrib),1,f1);
    fwrite(&gsmenu,sizeof(gsmenu),1,f1);
    fclose(f1);
}
getint(s)
char s[];
{
    int sign,num;

    while(s[0] == ' ') lshiftstr(s);
    if(s[0] == ',') lshiftstr(s);
    while(s[0] == ' ') lshiftstr(s);
    sign = 1;
    if(s[0] == '-')
    {
        sign = -1;
        lshiftstr(s);
    }
    num = 0;
    while(s[0] >= '0' && s[0] <= '9')
    {
        num = 10 * num + s[0] - '0';
        lshiftstr(s);
    }
    return(num * sign);
}
getstr(str,s,MAX)
char str[],s[];
int MAX;
{
    int i;

    while(s[0] == ' ') lshiftstr(s);
    if(s[0] == ',') lshiftstr(s);
    while(s[0] == ' ') lshiftstr(s);
    i = 0;
    while(s[0] != ',' && s[0] != '\n')
    {
        if(i < (MAX-1))
        {
            str[i] = s[0];
            ++i;
        }
        lshiftstr(s);
    }
    str[i] = '\0';
}
lshiftstr(s)
char s[];
{
    int i;

    i = 0;
    while(s[i] != '\0')
    {
        s[i] = s[i+1];
        ++i;
    }
}

