/************************************************************************/
/*																																			*/
/*							Q U O T E R . C - Quote-of-the-Day Program							*/
/*																																			*/
/************************************************************************/

/************************************************************************
 *					 
 *  Author:  T_Koschate
 *  Date:		 August, 1990
 *
 ************************************************************************/

#include <os2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

char quotestr[25 * 81];

void main(int argc, char *argv[])
{
	FILE *fp;
	long quotenum, quotectr;
	char qline[81];
	time_t currtime;
	int startpoint;
	HAB hab;
	HMQ	    hmq;
	int exit_code;

	if (NULL == (fp = fopen("quotes.bbs","r")))
		{
		strcpy(quotestr,"Unable to open QUOTES.BBS");
		exit_code = 1;
		}
	else
		{
		if (argc == 2)
			quotenum = atol(argv[1]);
		else
			{
			time(&currtime);
			srand((unsigned int)currtime % RAND_MAX);
			quotenum = (long)rand();
			}

		quotectr = 1L;
		while (quotectr < quotenum)
			{
			while (TRUE)
				{
				if (NULL == fgets(qline,sizeof(qline),fp))				/* Ran out of quotes */
					{
					rewind(fp);
					quotenum = quotenum % quotectr;
					quotectr = 1;
					continue;
					}
				if (!strcmp(qline,"\n"))
					{
					quotectr++;
					break;
					}
				}
			}

		startpoint = 0;
		while (TRUE)
			{
			if (NULL == fgets(qline,sizeof(qline),fp))				/* Ran out of quotes */
				break;
			if (strcmp(qline,"\n"))
				{
				strcpy(&quotestr[startpoint],qline);
				startpoint += strlen(qline);
				}
			else
				break;
			}
		fclose(fp);
		exit_code = 0;
		}	

  hab = WinInitialize (0);
  hmq = WinCreateMsgQueue (hab, 0);

	WinMessageBox(HWND_DESKTOP,HWND_DESKTOP,quotestr,
		"Quote of the Day",0,MB_OK|MB_NOICON);

	WinDestroyMsgQueue (hmq);
	WinTerminate (hab);

	exit(exit_code);
}
