#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wgt4.h>
#include <io.h>
#include <process.h>




int oldmode;
int newmode;
block dummy;
block copy;
block laserjet;
block dotprinter;
color scpalette[256];
FILE *fp;

int GetConfigString (void) {

	char filename[15];


	chdir("DRVR");
	fp = fopen ("MIDI.CFG", "r");
	if (fp == NULL) {
		return NULL;
	}

	fscanf(fp,"%s",filename);
	fclose (fp);
	chdir("..");

	if((filename[0]=='N')&&(filename[1]=='O')) return 0;
	else return 1;
}

void main()
{
  int advance;
  int stopp,fln;
  int laser,musiccard;
  int menu,setup;
  int m;
  oldmode=0;
  newmode=0;

  laser=0;    /*laser=0 dot matrix used, laser=1 laser printer used*/
  setup=0;
  stopp=0;
  menu=0;
  if((fp=fopen("temp\\mode.out","r"))==NULL){
  oldmode = wgetmode ();        /* Preserve our original video mode */
  vga256();
  fclose(fp);
  }
  if((fp=fopen("temp\\mode.out","r"))!=NULL){
	fscanf(fp,"%d",&oldmode);
	fclose(fp);
	vga256();
	}
      setlib("graphics\\setup.lib");
  newmode=wgetmode();
  wcls(0);
  m=minit();

  if(m<1) {
		wsetmode(oldmode);
		clrscr();
		printf("No mouse or driver present.  Mouse needed.\n\n");
		setlib("NULL");
		exit(0);
	  }

msetbounds(0,0,319,199);
	dummy=wloadpcx256("setup.pcx",scpalette);
	wsetpalette(0,255,scpalette);
	wputblock(0,0,dummy,0);
	wfreeblock(dummy);
	copy=wnewblock(176,53,233,71);
	laserjet=wnewblock(44,91,134,148);
	dotprinter=wnewblock(178,96,285,141);

	fp=fopen("data\\world.cfg","r");
	    fscanf(fp,"%d",&musiccard);
	    fscanf(fp,"%d",&laser);
	fclose(fp);
 musiccard=GetConfigString();
  wtextcolor(255);
  if(musiccard==1) wouttextxy(190,60,NULL,"ON");
  if(musiccard==0) wouttextxy(190,60,NULL,"OFF");
  if(laser==1)  {
		wouttextxy(84,115,NULL,"ON");
		wouttextxy(230,115,NULL,"OFF");
		}
  if(laser==0)  {
		wouttextxy(84,115,NULL,"OFF");
		wouttextxy(230,115,NULL,"ON");
		}

mon();      /*turn mouse on*/
	while(stopp==0){
		if(but==1){
			noclick();
			if((mx>176)&&(mx<233)&&(my>53)&&(my<71)){
				moff();
				wputblock(176,53,copy,0);
				  if(musiccard==1)
					wouttextxy(190,60,NULL,"ON");
				  if(musiccard==0)
					wouttextxy(190,60,NULL,"OFF");
				mon();
				}
			if((mx>44)&&(mx<134)&&(my>91)&&(my<148)){
				moff();
				wputblock(44,91,laserjet,0);
				wputblock(178,96,dotprinter,0);
				if(laser==1) laser=0;
				else laser=1;
				  if(laser==1){
					wouttextxy(84,115,NULL,"ON");
					wouttextxy(230,115,NULL,"OFF");
					}
				  if(laser==0){
					wouttextxy(84,115,NULL,"OFF");
					wouttextxy(230,115,NULL,"ON");
					}
				  mon();
				  }
			if((mx>178)&&(mx<285)&&(my>96)&&(my<141)){
				moff();
				wputblock(44,91,laserjet,0);
				wputblock(178,96,dotprinter,0);
				if(laser==1) laser=0;
				else laser=1;
				  if(laser==1){
					wouttextxy(84,115,NULL,"ON");
					wouttextxy(230,115,NULL,"OFF");
					}
				  if(laser==0){
					wouttextxy(84,115,NULL,"OFF");
					wouttextxy(230,115,NULL,"ON");
					}
				  mon();
				  }
			if((mx>247)&&(mx<296)&&(my>52)&&(my<71)){
							setup=1;/*MUSIC*/
							stopp=1;/*SET CARD*/
							}

			if((mx>128)&&(mx<189)&&(my>173)&&(my<193)){
							 menu=1; /*MENU*/
							 stopp=1;
							 }
		}
	}
moff();         /*turn mouse off*/


	mdeinit();
	wfreeblock(copy);
	wfreeblock(laserjet);
	wfreeblock(dotprinter);

	setlib(" ");
	fp=fopen("data\\world.cfg","w");
	    fprintf(fp,"%d\n",musiccard);
	    fprintf(fp,"%d\n",laser);
	fclose(fp);
wsetmode(newmode);
if(setup==1) {  chdir("bin");
		wsetmode(oldmode);
		spawnv(P_WAIT,"msetup",NULL);
		chdir("..");
		wsetmode(newmode);
		execv("setup",NULL);
		}

if(menu==1) {
	  if((fp=fopen("temp\\mode.out","r"))==NULL){
		wsetmode(oldmode);
		fclose(fp);
		}

	 execv("start.exe",NULL);
	 }
exit(0);
}


