
/*
* This file is part of the GMOD package.
*
* parse_args by Andrew J. Robinson
*
* Parse command-line arguments.
*/


#include <unistd.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>

#include "defines.h"
#include "structs.h"
#include "globals.h"

int
parse_args (int argc, char *argv[], struct options_info *options)
{
  extern char *optarg;
  extern int optind;
  int option, num_err = 0;

  while ((option = getopt (argc, argv, "bcehlm:pqrRsv:xz")) != -1)
    switch (option)
      {
      case 'h':
  printf ("GMOD version 1.3a\n");
  printf ("Original source (C) Hannu Savolainen, 1993\n");
  printf ("Modifications by Andrew J. Robinson and Robert Sanders\n\n");
	printf ("Usage: %s [options] modfile . . .\n\n", argv[0]);
	printf ("Options:\n");
	printf ("     -b     DISABLE BPM tempos\n");
	printf ("     -c     DISABLE compression of modules in memory\n");
	printf ("     -e     Show empty samples (for messages)\n");
	printf ("     -h     Help\n");
	printf ("     -l     Break infinite loops\n");
	printf ("     -m x   Use mixer number x (0 - 15)\n");
	printf ("     -p     Use PAL sample timing\n");
        printf ("     -q     Quiet mode\n");
	printf ("     -r     Infinitely repeat module\n");
        printf ("     -R     Randomize module play order\n"); 
	printf ("     -s     Ignore speed 0 commands\n");
	printf ("     -v x   Set volume to x (0 - 255)\n");
	printf ("     -x     Extend octaves\n");
	printf ("     -z     Background mode\n\n");
	printf ("One or more MultiTracker, UltraTracker, MOD (4/6/8 channel), S3M,\n");
	printf ("or 669 files should be listed on the command line.\n");
	exit (ERR_NOERROR);
      case 'b':
	options->bpm_tempos = 0;
	break;
      case 'c':
	options->compress = 0;
	break;
      case 'e':
	options->show_empty_samples = 1;
	break;
      case 'l':
	options->loop_breaker = 1;
	/* override repeat */
	options->repeat = 0;
	break;
      case 'm':
	option = atoi (optarg);
	if ((option > 15) || (option < 0))
	  {
	    printf ("%s: mixer number must be between 0 and 15.\n", argv[0]);
	    num_err = 1;
	  }
	else
	  options->mixer = option;
	break;
      case 'p':
	options->pal = 1;
	break;
	case 'q':
	 fclose(stdout);
	 fclose(stderr);
	  break;
      case 'r':
	options->repeat = 1;
	/* override loop breaker */
	options->loop_breaker = 0;
	break;
	case 'R':
	options->randomize = 1;
        break;
      case 's':
	options->tolerant = 1;
	break;
      case 'v':
	option = atoi (optarg);
	if ((option > 255) || (option < 0))
	  {
	    printf ("%s: volume must be between 0 and 255.\n", argv[0]);
	    num_err = 1;
	  }
	else
	  options->main_volume = option;
	break;
      case 'x':
	options->extend_oct = 1;
	break;
      case 'z':
	background = 1;
	break;
      case '?':
	num_err = 1;
	break;
      case ':':
	num_err = 1;
	break;
      }

  if (num_err)
    {
      printf ("\nUsage: %s [options] modfile . . .\n", argv[0]);
      printf ("Use %s -h for help.\n\n", argv[0]);
      exit (ERR_BADARGS);
    }

  return optind;
}
