/*======================================================================*
 *            Program to view system MENU files.                        *
 *                                                                      *
 * Data layout is:  T/C/P filename.ext [%parm] description of this game *
 *                  T/C/P filename.ext [%parm] description of this game *
 *                  T/C/P filename.ext [%parm] description of this game *
 *                                                                      *
 *======================================================================*/

#version        $50
#reserve        6
#copyright      "Menu.cmd       Copyright K-products"



#address        $7000

#include        runtime.h
#include        bbs_pro.h

#define         MAXBUFF     7358
#define         DISK2       2

char    pathname [40];
char    menuname [40];

char    tempstr [255];
int     top_of_screen;                  /* entry at top now     */

char    menu_title [40];
int     menu_entry [99];                /* ptrs to each line    */


char    buffer [MAXBUFF];



main()
{
    get_path_name();                    /* build path from fn_ptr   */
    if (load_data_file())
	process_menu();
}



process_menu()
{
    char option [5];
    char another_page;
    byte num, x;

    top_of_screen = 0;
    while (1) {
	another_page = plot_page();
	get_string (option, 2, 0, 0); 
	if (isdigit (option [1])) {             /* made a selection */
	    num = valb (option);
	    if (num < 1 || num > 99 or empty (num-1))
		error ("That's not a valid selection...");
	    else
		run_file (num - 1);
	 }
	else {
	    case (upcase (option [1])) {
		'Q' :   {   cr_lf();
			    return; 
			}
		'P' :   {   if (top_of_screen > 9)
				top_of_screen = top_of_screen - 10;
			    else
				error ("You are at the top of the menu...");
			}
		'N' :   {   if (another_page)
				top_of_screen = top_of_screen + 10;
			    else
				error ("You are at the end of the menu..."); 
			}
		else:   error ("That's not a valid selection...");
	    }
	}
    }
}




plot_page()
{
    char *cp;
    int x, y;
    echo (125);
    cr_lf();
    bar();
    center();
    x = 36 - length (menu_title);
    x = x / 2;
    for (y = 0; y < x; ++y)
	echo (' ');
    echose (menu_title);
    bar();
    for (x = top_of_screen; x < top_of_screen+10; ++x) {
	center();
	echo (' ');
	cp = menu_entry [x];
	if (*cp == $9b)                         /* empty entry?     */
	    echose ("--");
	else {
	    if (x < 9) echo (' ');
	    echob (x + 1);
	    echos (".  ");
	    cp = cp + 11;                       /* point to desc    */
	    if (*cp = '%'){                     /* check for parm to dosshell */
	      while (*cp != 32){
	       ++cp;
	      }
	     ++cp;
	    }
	    for (y = 0; y < 30; ++y) {          /* max desc is 30   */
		echo (*cp);
		if (*cp == $9b)
		    break;
		++cp;
	    }
	    if (y == 30) cr_lf();               /* line overflowed  */
	}
    }
    bar();
    cp = menu_entry [top_of_screen + 10];
    center();
    echos ("  ");
    if (top_of_screen > 9)
	echos ("P)rev  ");
    if (*cp != $9b && top_of_screen < 90)
	echos ("N)ext  ");
    echos ("Q)uit  #) Select: ");
    if (*cp == $9b || top_of_screen == 90)      /* end of list? */
	return (0);
    return (1);
}




bar()
{
    int x;
    center();
    for (x=0; x < 37; ++x)
	echo (18);                  /* "-"  */
    cr_lf();
}



center()
{
    if (u_vw = 80)
	echos ("                    ");
}



error (char *s)
{
    char temp [5];
    cr_lf();
    echos (s);
    get_string (temp, 1, 0, 1);
}



load_data_file()
{
    byte x, y;
    char *cp;

    echo (125);
    cr_lf();
    echos ("Please wait; loading data...");
    MIOsuspend();
    if (open (DISK2, menuname, 4, 0) != OK) {      /* open input file?  */
	MIOresume();
	echose("Unable to open GAMES.MNU file!");
	return (0);
    }
    setblock (buffer, sizeof(buffer), $9b);
    blockread (DISK2, buffer, sizeof(buffer));
    if (IOresult (DISK2) != 136) {
	MIOresume();
	echose("GAMES.MNU file is too big!");
	return (0);
    }
    close (DISK2);
    MIOresume();
    cp = buffer;
    menu_title [0] = 0;                     /* title is in 1st line */
    y = 1;
    while (*cp != $9b) {
	if (y < 31) {
	    menu_title [y] = *cp;
	    menu_title [0] = length(menu_title) + 1;
	    ++y;
	}
	++cp;
    }
    ++cp;
    for (x = 0; x < 99; ++x) {
	menu_entry [x] = cp;                /* point to this line   */
	while (*cp != $9b)                  /* move to end of line  */
	    ++cp;
	++cp;
    }
    return (1);
}


empty (int x)
{
    char *cp;
    cp = menu_entry [x];
    if (*cp == $9b)
	return (1);
    return (0);
}


run_file (int x)
{
    byte y;
    char *cp, *p_cp, type;
    cr_lf();
    strcpy (tempstr, pathname);
    cp = menu_entry [x];
    type = upcase(*cp);
    p_cp = cp + 11;
    ++cp;
    ++cp;
    y  = length (tempstr);
    while (*cp != ' ') {
	++y;
	tempstr [y] = locase(*cp++);
	tempstr [0] = length(tempstr) + 1;
    }
    if (type == 'T') {
	if (u_vw == 40)
	    strcat (tempstr, ".40");
	else {
	    strcat (tempstr, ".80");
	    MIOsuspend();
	    if (open (DISK2, tempstr, 4, 0) != OK) {      /* .80 there? */
		y = length (tempstr) - 1;
		tempstr [y] = '4';
	    }
	    close (DISK2);
	    MIOresume();
	}
	showfile (tempstr);
	error ("Press <return> to continue...");
     }
    else {
	strcat (tempstr, ".cmd");
	parm1 [0] = 0;
	parm2 [0] = 0;
	if (type == 'P') {                          /* need parms?  */
	   if (*p_cp = '%'){
	       parm1[0] = 1;
	       parm1[1] = '-';
	       strcat(parm1, pathname);
	       strcat(parm1, ">");
	       y = parm1[0];
	       ++p_cp;
	       while (*p_cp != 32){
		parm1[y] = *p_cp;
		++y;
		++p_cp;
	       }
	       parm1 [0] = y-1;
	    }
	    else {
	       echos ("Parameter 1: ");
	       get_string (parm1, 38, 0, 1);
	       if (length (parm1) > 0) {
		  echos ("Parameter 2: ");
		  get_string (parm2, 38, 0, 1);
	       }
	   }
	}
	cr_lf();
	echos ("Spawning: ");
	cp = menu_entry [x] + 2;
	while (*cp != ' ')
	    echo (locase (*cp++));
	echo (' ');
	echos (parm1);
	echo (' ');
	echose(parm2);
	overlay (tempstr);
    }
}


get_path_name()
{
    char thefile [16];
    byte y;
    char *cp;
#asm {
		lda             $86                             ; load "fn_ptr"
		sta             .cp
		lda             $87
		sta             .cp+1
	}
    while (*cp && *cp != $9b)
	++cp;
    while (*cp != ':' && *cp != '<' && *cp != '>')
	--cp;
    cp = cp + 3;                                        /* past "M_"    */
    strcpy (pathname,"Dx:>pro>data>");
    pathname [2] = s_datadr;
    thefile [0] = 0;
    y  = 0;
    while (*cp != '.') {                                /* go till '.'  */
	++y;
	thefile [y] = locase(*cp++);
	thefile [0] = length(thefile) + 1;
    }
    strcat (pathname, thefile);
    strcat (pathname, ">");
    strcpy (menuname, pathname);
    strcat (menuname, thefile);
    strcat (menuname, ".MNU");
}
