/* ======================================================================== */
/* = Programmname    : KPlayer V1.00                                      = */
/* =                                                                      = */
/* ======================================================================== */
/* = Autor           : written in 1992 by Andreas Ralph Kleinert.         = */
/* =                   PUBLIC DOMAIN.                                     = */
/* ======================================================================== */
/* = Funktion        : Spielt MED-Module ab.                              = */
/* =                                                                      = */
/* ======================================================================== */
/* = Letztes Update  : 14.03.1992                                         = */
/* =                                                                      = */
/* ======================================================================== */
/* = Bemerkungen     : Benoetigt medplayer.library V1.11 oder hoeher.     = */
/* =                                                                      = */
/* ======================================================================== */
/* = Compiler        : SAS/C V5.10a/b                                     = */
/* =                                                                      = */
/* ======================================================================== */

#include <stdlib.h>     /* SAS/C Standard-Includes                */
#include <stdio.h>
#include <string.h>

#include <exec/types.h> /* Standard-Types                         */

#include <proto/dos.h>  /* Protos for System-Calls                */
#include <proto/exec.h>

#include "MEDPlayerLibrary/libproto.h"

                        /* We want to use 'medplayer.library' ... */

#define CSI 0x9b /* 0x9b (hex) = 155 (dec.) = 233 (oct) */

#define N (NULL)

char entry1_text  [] = "\n\2331;32;40mKPlayer V1.00\2330;31;40m (PUBLIC DOMAIN)";
char entry2_text  [] = "\2330;32;40mwritten in 1992 by Andreas Ralph Kleinert.\nAndreas R. Kleinert, Grube Hohe Grethe 23, 5900 Siegen 1, Germany.";
char entry3_text  [] = "Plays MED-Modules (\42MMD0\42-Files).\nUSAGE : \2330;33;40mKPlayer\2330;31;40m [RUN <ModuleName> | REMOVE | STOP | CONT | SET <Tmp>] [QUIET]\2330;31;40m\n";

char ver_text     [] = "$VER: KPlayer V1.00";

char nomed_text      [] = "You need 'medplayer.library' V1+ !\n";

char nochannels_text [] = "#200 : The Channels are busy at the moment, I think !\n";
char nomod_text      [] = "#201 : I can't load this module ! Check FileName, -Type and Memory !\n";

/* GLOBAL ACCESSED VARIABLES */

struct MEDPlayerBase *MEDPlayerBase = N;

struct MMD0          *actmod        = N;
long                  quiet         = FALSE;

/* FUNKTIONSDEKLARATIONEN */

void __regargs Leave(char *endtext, long code);

void CXBRK(void);
void CXBRK(void)
{
 Leave("\n*** BREAK ***\n", 0);
}

void main(long argc, char **argv)
{
 extern APTR _ONBREAK; /* We want to break in the way WE want ... */

 _ONBREAK = CXBRK;     /* ^                                       */

 if(!stricmp(argv[3], "QUIET")) quiet = TRUE;

 MEDPlayerBase = (struct MEDPlayerBase *) OpenLibrary("medplayer.library", 1);
 if(!MEDPlayerBase) Leave(nomed_text, 100);

 if((argv[1][0]=='?')||(argc>=5))
  {
   if(!quiet) puts(entry1_text);
   if(!quiet) puts(entry2_text);
   Leave(entry3_text, 0);
  }

 if(!quiet) puts(entry1_text);
 if(!quiet) puts("\n");

 if(!stricmp(argv[1], "RUN"))
  {
   if(GetPlayer(0)) Leave(nochannels_text, 200);

   actmod = LoadModule(argv[2]);
   if(!actmod) Leave(nomod_text, 201);

   PlayModule(actmod);
  }

 if(!stricmp(argv[1], "REMOVE"))
  {
   StopPlayer();

   UnLoadModule(GetCurrentModule());

   FreePlayer();
  }

 if(!stricmp(argv[1], "STOP")) StopPlayer();

 if(!stricmp(argv[1], "CONT")) ContModule(GetCurrentModule());

 if(!stricmp(argv[1], "SET")) SetTempo(atol(argv[2]));

 Leave(N, 0);
}

void __regargs Leave(char *endtext, long code)
{
 if(endtext) if(!quiet) puts(endtext);

 if(MEDPlayerBase) CloseLibrary(MEDPlayerBase);

 exit(code);
}
