#include "LSoundPlayer.h"
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Module.h"
#include <unistd.h>
#include "Player.h"
#include <string.h>
#include <termios.h>

Player	        *theplayer=NULL;
Module	  	*themodule=NULL;
int		filehandle=-1;
BYTE		*memptr=NULL;
bool		stereo=true;
char		device[80]="/dev/dsp";
struct termios  term;

void quit()
{
  delete theplayer;
  delete themodule;
  delete[] memptr;
  if (filehandle!=-1)
    close(filehandle);
  term.c_lflag|=ICANON;
  tcsetattr(0,TCSANOW,&term);
}


bool ParseCmdLine(int argc, char **argv)
{
  short i=1;

  if (argc==2)
    return true;

  while(i<argc-1)
  {
    if (strcmp(argv[i],"-mono")==0 || strcmp(argv[i],"-MONO")==0)
      stereo=false;
    else if (strcmp(argv[i],"-device")==0 || strcmp(argv[i],"-DEVICE")==0)
    {
      strncpy(device,argv[i+1],79);
      device[79]=0;
      i++;
    } else
      return false;
    i++;
  }
  if (i==argc)
    return false;
  return true;
}


int main(int argc, char **argv)
{
  struct stat		filestat;
  char			ch=0;

  printf("\nBSPlay for Linux V0.1Beta002 (C)1999 by Brian Postma");
  printf("\n----------------------------------------------------\n\n");

  if (tcgetattr(0,&term)<0)
  {
    printf("Error getting terminal state\n");
    return -1;
  }
  term.c_lflag&=~ICANON;
  if (tcsetattr(0,TCSANOW,&term)<0)
  {
    printf("Error setting terminal state\n");
    return -1;
  }
  if (!ParseCmdLine(argc,argv))
  {
    printf("Usage: %s [-mono] [-device devicename] modulename\n\n",argv[0]);
    printf("    Use -mono to force mono output (default is stereo)\n");
    printf("    Use -device to specify an alternative output device\n");
    printf("    (default is /dev/dsp, OSS drivers)\n\n");
    return -1;
  }
  if ((filehandle=open(argv[argc-1],O_RDONLY))==-1)
  {
    printf("Could not open file:%s\n",argv[argc-1]);
    quit();
    return -1;
  }
  fstat(filehandle,&filestat);
  if ((memptr=new BYTE[filestat.st_size])==NULL)
  {
    printf("Unable to allocate memory for module\n");
    quit();
    return -1;
  }
  if ((read(filehandle,memptr,filestat.st_size))!=filestat.st_size)
  {
    printf("Error reading file\n");
    quit();
    return -1;
  }
  if ((themodule=new Module(memptr))==NULL)
  {
    printf("Failed to allocate memory for Module object\n");
    quit();
    return -1;
  }
  if ((theplayer=new Player(themodule,device,stereo))==NULL)
  {
    printf("Error allocating memory for Player object\n");
    quit();
    return -1;
  }
  theplayer->Start();
  themodule->Init();

  printf("Module name: %s - Output to: %s\n\n",themodule->GetSongName(),
                                               device);

  for (short i=1; i<16; i++)
  {
    char   *buf;
    WORD   size, repeat,replen, volume;
    if (themodule->GetSampledInstrument(i,buf,size,repeat,replen,volume))
      printf("%02d: %s\n",i,buf);
    else
      printf("%02d: Synthetic instrument\n",i);
  }
  
  while(true)
  {
    fprintf(stdout,"\rPlaying step %d/%d ",theplayer->GetStep(),
            themodule->GetNumSteps());
    fflush(stdout);
    theplayer->Run();
    theplayer->Mix();
  }
  return 0;
}
