/* $Revision Header *** Header built automatically - do not edit! ***********
 *
 *	(C) Copyright 1992 by Metalworx
 *
 *	Name .....: Install.c
 *	Created ..: Sun 12-Jul-92 12:00
 *	Revision .: 0
 *
 *	Date		Author		Comment
 *	=========	========	====================
 *	12-Jul-92	Mtwx		Created this file!
 *
 * $Revision Header ********************************************************/
 #define REVISION 0

/* ------------------------------- includes ----------------------------- */

#include <stdio.h>
#include <string.h>
#include <exec/memory.h>
#include <libraries/dosextens.h>
#include <intuition/intuitionbase.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/intuition.h>

#include <mtwx_defs.h>

#include "install_defs.h"
#include "install.h"

/* ------------------------------- defines ------------------------------ */

/* ------------------------------- global references -------------------- */

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct Screen *FirstScreen;
struct Window *Window1;

long	  conwin;
static char Ver[80];

/* ------------------------------- external references ------------------ */

extern char ProgId[],Version[];

/* ------------------------------- prototypes --------------------------- */

void Menu_Auswertung(USHORT Menu_Nr);
void main(void);
void Open_All(void);
void Close_All(BOOL Status);
BOOL PrepareInstallation(struct InstallStruct *);

extern BOOL InstallChemest(struct InstallStruct *, struct Screen *);

/* ------------------------------- routines ----------------------------- */

void Menu_Auswertung(USHORT Menu_Nr)
{
  USHORT    Menu, MenuItem, SubItem;
  struct MenuItem *MenuPunkt;

  Menu = MENUNUM(Menu_Nr);
  MenuItem = ITEMNUM(Menu_Nr);
  SubItem = SUBNUM(Menu_Nr);

  switch (Menu)
  {
    case 0:			       /* Menue 1 */
      {
	switch (MenuItem)
	{
	  case 0:
	  case 1:
	  case 2:
	  case 3:
	  case 4:
	    break;
	}
	break;
      }
    case 1:			       /* Menue 2 */
      {
	switch (MenuItem)
	{
	  case 0:
	  case 1:
	    break;
	}
	break;
      }
    case 2:			       /* Menue 3 */
      {
	switch (MenuItem)
	{
	  case 0:
	  case 1:
	    break;
	}
	break;
      }
    case 3:			       /* Menue 4 */
      {
	switch (MenuItem)
	{
	  case 0:
	    break;
	}
	break;
      }
  }
}

void main(void)
{
  struct WNachricht RumpfMsg;
  struct InstallStruct InstallStruct;

  Open_All();
  Shadowbox(Window1,2,1,5,15,271,89," Executables ",1,2);
  Shadowbox(Window1,2,1,5,110,255,77," Documentation ",1,2);
  Shadowbox(Window1,2,1,320,15,255,41," Examples ",1,2);
  Shadowbox(Window1,2,1,320,62,255,64," Libraries ",1,2);
  Shadowbox(Window1,2,1,320,132,255,25," LHA (archive extractor) ",1,2);
  Shadowbox(Window1,2,1,435,163,195,23," Click OK to start... ",1,2);

  FOREVER
  {
    RumpfMsg=get_simpleinputs(Window1);
    switch (RumpfMsg.Klasse)
    {
      case CLOSEWINDOW:
	Close_All(OK);
      case GADGETUP:
	switch(RumpfMsg.GadgetID)
	{
	  case OK_GADGET:
	    if(PrepareInstallation(&InstallStruct))
	      if(InstallChemest(&InstallStruct,FirstScreen))
		Close_All(OK);
	    break;
	  case ABBRUCH_GADGET:
	    Close_All(OK);
	    break;
	  default:
	    break;
	}
	break;
      case MENUPICK:
	Menu_Auswertung(RumpfMsg.Code);
	break;
    }
  }
}

/***********************************************
 * Funktion: Library, Screen und Window öffnen *
 ***********************************************/

void	  Open_All(void)
{
  conwin = OpenRevWin("CON:120/79/400/80/InstallChemest");
  if (conwin == -1)
  {
    puts("Console-Window läßt sich nicht öffnen!");
    Close_All(NOK);
  }
  if (!(IntuitionBase = (struct IntuitionBase *)
     OpenLibrary("intuition.library",0L)))
  {
    WrConWin(conwin, "Fehler beim Öffnen der intuition.library!\n");
    Close_All(NOK);
  }

  if (!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0L)))
  {
    WrConWin(conwin, "Keine Graphics Library gefunden!\n");
    Close_All(NOK);
  }
  sprintf(Ver,"%s V%s - © 1992 Metalworx",ProgId,Version);
  InstallNewScreen.DefaultTitle=(UBYTE *)Ver;
  if (!(FirstScreen = (struct Screen *)
	OpenScreen(&InstallNewScreen)))
  {
    WrConWin(conwin,"Screen hat keine Zeit!\n");
    Close_All(NOK);
  }

  SetUpNewWindow.Screen = FirstScreen;

  if (!(Window1 = (struct Window *)
	OpenWindow(&SetUpNewWindow)))
  {
    WrConWin(conwin,"Window will nicht aufgehen!\n");
    Close_All(NOK);
  }
}

/***************************************
 * Funktion: Alles geöffnete schließen *
 ***************************************/

void	  Close_All(BOOL Status)
{
  if(IntuitionBase && Status==NOK)
    Delay(100L);
  if (Window1)
  {
/*    ClearMenuStrip(Window1);*/
    CloseWindow(Window1);
  }
  if (FirstScreen)
    CloseScreen(FirstScreen);
  if(GfxBase)
    CloseLibrary((struct Library *)GfxBase);
  if(IntuitionBase)
    CloseLibrary((struct Library *)IntuitionBase);
  if (conwin)
    close(conwin);
  exit(Status);
}


/*##########################################################################
# PrepareInstallation():  füllt InstallStruct mit den ausgewählten Werten  #
#									   #
#   Übergabe: struct InstallStruct *InstallStruct - Pointer auf die zu	   #
#						    füllende Struktur	   #
#   Rückgabe: BOOL TRUE (1)  - wenn alles OK ist                           #
#		   FALSE (0) - wenn ein Fehler aufgetreten ist             #
##########################################################################*/
BOOL PrepareInstallation(struct InstallStruct *InstallStruct)
{
  BOOL rwert=TRUE;
  char dname[120];

  Check_Pathname(ExecStrGadgetSIBuff);
  InstallStruct->ExecPath=ExecStrGadgetSIBuff;
  InstallStruct->ExecGerman=ExecBoolGerman.Flags & SELECTED;
  InstallStruct->ExecEnglish=ExecBoolEnglish.Flags & SELECTED;
  InstallStruct->ExecFPU=ExecBoolFPU.Flags & SELECTED;
  InstallStruct->ExecIEEE=ExecBoolIEEE.Flags & SELECTED;
  InstallStruct->ExecUtil=ExecBoolUtil.Flags & SELECTED;

  Check_Pathname(DocStrGadgetSIBuff);
  InstallStruct->DocPath=DocStrGadgetSIBuff;
  InstallStruct->DocGerman=DocBoolGerman.Flags & SELECTED;
  InstallStruct->DocEnglish=DocBoolEnglish.Flags & SELECTED;
  InstallStruct->DocUtil=DocBoolUtil.Flags & SELECTED;
  InstallStruct->DocQuicktrans=DocBoolQuicktrans.Flags & SELECTED;

  Check_Pathname(ExamplesStrGadgetSIBuff);
  InstallStruct->ExamplesPath=ExamplesStrGadgetSIBuff;
  InstallStruct->Examples=ExamplesBool.Flags & SELECTED;

  Check_Pathname(LibsStrGadgetSIBuff);
  InstallStruct->LibsPath=LibsStrGadgetSIBuff;
  InstallStruct->LibsArp=LibsBoolArp.Flags & SELECTED;
  InstallStruct->LibsReq=LibsBoolReq.Flags & SELECTED;
  InstallStruct->LibsQuicktrans=LibsBoolQuicktrans.Flags & SELECTED;

  Check_Pathname(LHAStrGadgetSIBuff);
  InstallStruct->LHAPath=LHAStrGadgetSIBuff;
  RefreshGadgets(&ExecStrGadget,Window1,NULL);

  strcpy(dname,LHAStrGadgetSIBuff);
  strcat(dname,"LhA");
  if(!TestFile(dname))
  {
    Messagewin(FirstScreen,"Error","LhA not found in specified path!",-1);
    rwert=FALSE;
  }
  return rwert;
}

