/*
** Shell program for Accolade's MEAN-18 golf game.
** Written by Dick Stout, Sysop, Computing Technology BBS
**                               (619) 375-2306
**
** This is a totally new version of the MEAN-18 shell.  It uses the same
** technique and the same basic logic as the previous version 2.4 M18.EXE,
** which was written in QuickBASIC.  This version is written in Microsoft's
** QuickC version 1.0.  This is my first attempt at a "C" program, so I
** wouldn't hold this program up as a shining example of good "C" code. The
** source code is donated to the public domain, but please don't just steal
** it and call it your own without acknowledging from where it came.  I
** am working on a super version of MEAN-18 shell, but it will probably be
** a couple of months from now (3/8/88), before I will get it up and running,
** and maybe longer before I release it.  If anyone has some thoughts on what
** they would like to see in a MEAN-18 shell program, drop me a comment on
** my BBS and let me know about it.
*/

#include <stdio.h>
#include <math.h>
#include <io.h>
#include <stdlib.h>
#include <dos.h>
#include <graph.h>
#include <ctype.h>
#include <string.h>

#define M18_VERSION     "M18.EXE v3.0 by Dick Stout using Microsoft QuickC v1.0"
#define MAX_COURSES      99

/* Define a string array to hold the course file names */

char  course_list[MAX_COURSES][13];
char  *course_ptr[MAX_COURSES];
int   course_count = 0;

/* ==== MEAN-18 main shell program, by Dick Stout ==== */

main()

{
check_for_golf();
get_courses();
change_to_m10();
display_menu();
choose_course();
system("golf");
}

/* === Check the disk for the GOLF.EXE file === */

check_for_golf()
{
if ((access("GOLF.EXE",0)))
	{
	printf("GOLF.EXE must be in the same directory as M18.EXE\n");
	printf("Please check your directory and try again!\n");
	exit(1);
	}
}


/* Get all the golf course files on the disk */

get_courses()

{
	struct   find_t  c_file;

/*
	Check for  MEAN-18 course file in current directory
	and if you find one, put it into the course list.  If
	you don't find any, issue an error message and exit.
*/

	if((_dos_findfirst ("*.M1?", _A_NORMAL, &c_file)) == 0)
		{
		strcpy (course_list[course_count], c_file.name);
		course_ptr[course_count] = course_list[course_count];
		}
	else
		{
		printf("\nNO MEAN-18 course files were found, please check your present\n");
		printf("directory for course files with a .M10 or .M18 extension.\n");
		exit(2);
		}

/*
	Find the rest of the MEAN-18 course files and put them
	into the course list.
*/

	while ((_dos_findnext(&c_file) == 0)&&(course_count < MAX_COURSES))
		{
		strcpy (course_list[++course_count], c_file.name);
		course_ptr[course_count] = course_list[course_count];
		}
	sort_string(course_ptr, course_count+1);
 }

/*
	Display a menu of all the courses available
*/

display_menu()
{

struct rccoord rcoord;
int   index, nlength, column = 2;
char  copy[9], buffer[15];
char  *cname, *p;
long  bg = 1;

	_setvideomode(_TEXTC80);
	_settextcolor(11);
	_setbkcolor(bg);
	_clearscreen( _GCLEARSCREEN );

	_outtext("              ");
	_outtext(M18_VERSION);
	_outtext("\n\n");
	_outtext("                       Menu of available MEAN-18 Golf Courses\n");
	_outtext("  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

	rcoord = _gettextposition();

	for ( index  = 0; index <= course_count; index++)
		{
		nlength = (strlen(course_ptr[index]) - 4);
		cname = strncpy(copy,course_ptr[index],nlength);
		copy[nlength] = '\0';
		cname = strcpy(p,"  \0");
		cname = strcat(p, itoa(index+1, buffer, 10));
		cname = strcat(p," - ");
		cname = strcat(p,copy);
		if (((index+1)%5) && (index < course_count))
			{
			_settextposition(rcoord.row,column+1);
			_outtext(cname);
			column += 15;
			}
		else
			{
			if (index == 9)
				column--;
			_settextposition(rcoord.row,column+1);
			_outtext(cname);
			rcoord.row++;
			if (index < 9)
				column = 2;
			else
				column = 1;
			}
		}
}


/* Select the golf course you wish to play */

choose_course()
{
struct rccoord rcoord;
int   ch, count = 0, c, length;
char  choice[4], *new_name, *tmp_str, *m18;

	m18 = "M18";
	_outtext("\n\n    Enter the number of the course you wish to play or Q to quit - ");
	rcoord = _gettextposition();
	while (( ch = getch()) != 13 )
		{
		switch (ch)
			{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
				{
				if ((count == 0) & (ch == 48))
					break;
				choice[count++] = (char) ch;
				choice[count] = '\0';
				_settextposition( rcoord.row, rcoord.col );
				_outtext(choice);
				break;
				}
			case 'Q':
			case 'q':
				exit(0);
			case '\010':
				choice[--count] = '\x20';
				_settextposition( rcoord.row, rcoord.col );
				_outtext(choice);
				choice[count] = '\0';
				_settextposition( rcoord.row, rcoord.col );
				_outtext(choice);
				break;
			}
		}
	if (count == 0)
		exit(0);
	c = atoi(choice);
	if (c > (course_count+1))
		c=course_count + 1 ;
	change_extension(course_ptr[c-1], m18);

}

/*
	Search all the course names for .M18 extensions and
	if found, change the extension to .M10.
*/

change_to_m10()
{
int   index, length;
char  *new_name,*tmp_str, *m10;

	m10 = "M10";
	for ( index  = 0; index <= course_count; index++)
		{
		if (strstr(course_ptr[index], "M18") != 0)
			change_extension(course_ptr[index], m10);
		}
}



change_extension(char *old_name, char  *new_ext)
{

char  *tmp_str, *new_name;
int   length;

	length = strlen(old_name)-3;
	strncpy(tmp_str, old_name, length);
	tmp_str[length] = '\0';
	new_name = strcat(tmp_str, new_ext);
	rename (old_name, new_name);
	strcpy(old_name, new_name);

}


sort_string(strings, num)
char  *strings[];
int   num;
{
	char  *temp;
	int top, seek;

	for ( top = 0; top < num-1; top++)
		for( seek = top +1; seek < num; seek++)
			if (strcmp(strings[top],strings[seek]) > 0)
				{
				temp = strings[top];
				strings[top] = strings[seek];
				strings[seek] = temp;
				}
}

/* -------------------- THE END --------------------*/
