/*--------------------------------------------------------------------------*
                     __       __           $RCSfile: atd.c,v $
                   _/ /_     / /           $Revision: 1.3 $
           ______ /  __/____/ /            $Date: 93/03/30 04:28:43 $
          / __  / / /  / __  /             $Author: tf $
         / __  / / /_ / /_/ /              $Locker:  $
        /_/ /_/ /___//_____/               $State: Exp $

            (c) Copyright 1993 Tobias Ferber, All Rights Reserved.

 *--------------------------------------------------------------------------*/

#include "atdt.h"
#include <stdio.h>

static char rcs_id[]= "$VER: $Id: atd.c,v 1.3 93/03/30 04:28:43 tf Exp $";
#define BANNER &rcs_id[6]

static UBYTE dialcodes[16]= { "123A456B789C*0#D" };

static struct IOAudio *a0= NULL,
                      *a1= NULL;

_abort()
{ puts("^C\n\n***BREAK");
  if(a0) free_dtone(a0);
  if(a1) free_dtone(a1);
  exit(5);
}

void myatdt(char *numstr, int delay)
{
  int i=0;
  onbreak(_abort);

  a0= alloc_dtone(),
  a1= alloc_dtone();

  printf("atdt"); fflush(stdout);

  if(a0 && a1)
  { while(numstr && *numstr)
    { printf("%c",*numstr);  fflush(stdout);
      for(i=0;i<16;i++)
      { if(toupper(*numstr)==dialcodes[i])
        { play_dtone(a0,a1,i);
          Delay(delay);
        }
      }
      ++numstr;
    }
    i=42;
  }
  printf("... "); fflush(stdout);

  if(a0) { free_dtone(a0); a0=NULL; }
  if(a1) { free_dtone(a1); a1=NULL; }
  puts((i==0)?"error.":"ok.");
}

main(int argc, char *argv[])
{
  BOOL badopt = FALSE;
  BOOL quiet  = FALSE;

  int delay= 2;

  if(argc>1)
  { while(--argc>0 && !badopt)
    { char *arg= *++argv;
      if(*arg=='-')
      { switch(*++arg)
        { case 'q': case 'Q':
            quiet= TRUE;
            break;
          case 'd': case 'D':
            quiet= FALSE;
            if(arg[1]) ++arg;
            else arg= (--argc > 0) ? *(++argv) : (char *)0L;
            if(arg && *arg)
	    { delay= atoi(arg);
              if(delay<0 || delay>127)
              { printf("Illegal delay value %d -- should be in [0..127]\n",delay);
                badopt=TRUE;
              }
            }
            else
            { puts("Error: Missing delay value after keyword");
              badopt=TRUE;
            }
            break;
          case 'f': case 'F':
            { char *fname= NULL;
              if(arg[1]) ++arg;
              else arg= (--argc > 0) ? *(++argv) : (char *)0L;
              if(arg && *arg)
              { FILE *fp= fopen(fname=arg,"r");
                if(fp)
                { char num[256];
                  while(fp && !feof(fp))
                  { num[0]='\0';
                    fscanf(fp,"%s",num);
                    if(quiet) atdt(num);
                    else myatdt(num,delay);
                  }
                  fclose(fp);
                }
                else printf("%s: No such file or directory\n",fname);
              }
              else
              { puts("Error: Missing file name after keyword");
                badopt=TRUE;
              }
            }
            break;
          case 'w': case 'W':
            (void)wbmain();
            break;
          default:
            printf("Bad option: -%c.",*arg);
            badopt=TRUE;
            break;
        }
      }
      else if(*arg=='?')
      { char num[256];
        printf("NUMBER/A: "); fflush(stdout);
        scanf("%s",num);
        if(quiet) atdt(num);
        else myatdt(num,delay);
      }
      else
      { if(quiet) atdt(arg);
        else myatdt(arg,delay);
      }
    }
  }
  else /* no args */
  { puts(BANNER);
    puts("(c)Copyright 1993 by Tobias Ferber, All Rights Reserved");
    puts("ATD [-q] | [-d <delay>] [-f <file>] [number]");
  }
  exit(0);
}

extern void ToneDial(); /* see tonedial.c */
wbmain() { ToneDial(); exit(0);}
