/*******************************************************************
*
* $VER: UseCli.c 1.1 (8.6.97) Tak Tang (tst92@ecs.soton.ac.uk)
*
* The UseCLI() module handles the case of being run from the shell.
* Arguments are parsed with rdargs, and then calls PlaySample()
* for each arg.
*
* This file is in the Public Domain
*
* Most of this code came from xPlay by Christian Buchner
*
********************************************************************
*
*/

/**** Header files ****/

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/execbase.h>

#include <dos/dos.h>

#include <clib/alib_protos.h>
#include <clib/dos_protos.h>
#include <clib/exec_protos.h>

#include <pragmas/dos_pragmas.h>
#include <pragmas/exec_pragmas.h>

#include <string.h>

#include "xPlay.h"
#include "PlaySample.h"
#include "UseCli.h"


/* Argument Array */

struct  ArgArray
{
    char **aa_Samples;
    ULONG aa_All;
    ULONG *aa_Volume;
    ULONG *aa_Frequency;
    ULONG *aa_Period;
    ULONG *aa_Priority;
    ULONG aa_Double;
    ULONG aa_Mono;
    ULONG aa_Quiet;
    ULONG aa_Filter;
    ULONG aa_NoFilter;
    ULONG aa_PC;
};


UBYTE *Template = "SAMPLES/M/A,ALL/S,V=VOLUME/N,F=FREQUENCY/N,P=PERIOD/N,PRI=PRIORITY/N,D=DOUBLE/S,M=MONO/S,Q=QUIET/S,FL=FILTER/S,NFL=NOFILTER/S,PC/S";


/****** UseCli() ***************************************************
*
*   NAME
*       UseCli() -- Handles the case of being run from the shell
*
*   SYNOPSIS
*       error UseCli( GlobalData )
*
*       ULONG UseCli( struct GlobalData * );
*
*   FUNCTION
*       Parses the command line with rdargs.  Each argument is
*       pattern matched, and the resulting files are passed to
*       PlaySample().
*
*       The template is SAMPLES/M/A,ALL/S,V=VOLUME/N,F=FREQUENCY/N,
*       P=PERIOD/N,PRI=PRIORITY/N,D=DOUBLE/S,M=MONO/S,Q=QUIET/S,
*       FL=FILTER/S,NFL=NOFILTER/S,PC/S
*
*   INPUTS
*       GlobalData     - pointer to a structure containing global
*                        data, such as library bases
*
*   RESULT
*       RETURN_OK - All files played OK
*       (others)
*
*   EXAMPLE
*
*   NOTES
*       (say something descriptive about pattern matching, links ...)
*
*   BUGS
*
*   SEE ALSO
*
********************************************************************
*
* 1.1 (9.6.97) tst
*     First version
*
*/

ULONG UseCli(struct GlobalData *gd)
{
  struct RDArgs *RDArgs;
  struct ArgArray AA  = { NULL,FALSE,NULL,NULL,NULL,NULL,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE };
  char **Files;
  struct AnchorPath *AnchorPath;
  UWORD Period=0;
  ULONG ReturnCode=RETURN_FAIL;
  char ProgName[32];

  if (!GetProgramName(ProgName,32L)) strcpy(ProgName,"xPack");

  if (!(RDArgs=ReadArgs(Template,(LONG *)&AA,0)))
  {
      PrintFault(IoErr(),ProgName);
  }
  else
  {
      if (AA.aa_Period) Period = *AA.aa_Period;
      if (AA.aa_Frequency) Period = (*(struct ExecBase**)(4))->ex_EClockFrequency*5/(*AA.aa_Frequency);

      if (!(Files=AA.aa_Samples))
      {
          ReturnCode=RETURN_OK;
      }
      else
      {
          if (!(AnchorPath=(struct AnchorPath *)AllocVec(sizeof(struct AnchorPath)+256L,MEMF_PUBLIC|MEMF_CLEAR)))
          {
              PrintFault(ERROR_NO_FREE_STORE,ProgName);
          }
          else
          {
              AnchorPath->ap_BreakBits=SIGBREAKF_CTRL_C;
              AnchorPath->ap_Strlen=256;

              while (*Files)
              {
                  LONG RetVal;

                  RetVal=MatchFirst(*Files,AnchorPath);
                  while(!RetVal)
                  {
                      if (AnchorPath->ap_Info.fib_DirEntryType>0L)
                      {
                          if (((AnchorPath->ap_Flags&APF_DIDDIR)==NULL)&&AA.aa_All)
                          {
                              AnchorPath->ap_Flags|=APF_DODIR;
                          }
                          AnchorPath->ap_Flags&=~APF_DIDDIR;
                      }
                      else
                      {
                          ReturnCode=PlaySample(
                              gd,
                              AnchorPath->ap_Buf,
                              Period ? Period : 0,
                              AA.aa_Volume   ? *AA.aa_Volume   :0,
                              AA.aa_Priority ? *AA.aa_Priority :0,
                              AA.aa_Double,
                              AA.aa_Mono,
                              AA.aa_Quiet,
                              AA.aa_Filter,
                              AA.aa_NoFilter,
                              AA.aa_PC);

                          if ( (ReturnCode) && (TRUE==gd->UserStop) )
                          {
                              MatchEnd(AnchorPath);
                              goto FreeAnchor;
                          }
                      }
                      RetVal=MatchNext(AnchorPath);
                  }
                  MatchEnd(AnchorPath);

                  if (RetVal!=ERROR_NO_MORE_ENTRIES)
                  {
                      Printf("%s: %s - ",ProgName,*Files);
                      PrintFault(RetVal,NULL);
                      goto FreeAnchor;
                  }
                  else Files++;
              }
              ReturnCode=RETURN_OK;

FreeAnchor:             FreeVec((APTR)AnchorPath);
          }
      }
      FreeArgs(RDArgs);
  }

  return ReturnCode;
} /* UseCli */

/**** End of file ****/

