#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include "gsmenu.h"

struct PASSED passed =
{
    0,0,"",
    0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0
};


void main(int argc, char **argv)
{
    char *args[2];
    struct PASSED __far *ptr;
    char *df,dfile[80],efile[80];
    int num,status;

    ptr = &passed;
    df = strrchr(argv[0],'\\');

    if(df != NULL)
    {
         num = df - argv[0] + 1;
         strncpy(dfile,argv[0],num);
         dfile[num-1] = '\\';
         dfile[num] = '\0';
         strcpy(efile,dfile);
    }
    if(df != NULL)
        strcat(efile,"gsmenu0.exe");
    else
        strcpy(efile,"gsmenu0.exe");

    if(_spawnl( _P_WAIT, efile , args[0], NULL ) == -1)
    {
        printf("%s not found\n",dfile);
        perror("");
        exit(0);
    }

    if(df != NULL)
        strcat(dfile,"gsmenu1.exe");
    else
        strcpy(dfile,"gsmenu1.exe");

    _putenv("PROMPT=$p$g(EXIT to return to menu)");

    while (TRUE)
    {
    sprintf(args[1],"%lp",ptr);
        _flushall;
/*    printf("in gsmenu %s\n",args[1]);
    printf("in gsmenu %s\n",args[1]);*/
        if(status=_spawnl( _P_WAIT, dfile , args[0], args[1], NULL ) != 0)
        {
            if(status == -1) printf("%s not found\n",dfile);
            perror("");
            exit(0);
        }
/*        printf("command: %s\n",passed.cmdline);*/
        if (strcmp(passed.cmdline,"exit") == 0) exit(0);
        system(passed.cmdline);
        strcpy(passed.cmdline,"");
        if( passed.pause == 1 )
        {
             printf("Hit any key to return to the menu");
             _getch();
        }
        if(_spawnl( _P_WAIT, efile , args[0], NULL ) == -1)
        {
            printf("%s not found\n",dfile);
            perror("");
            exit(0);
        }
    }
}


