/*****************************************************************************

   This file is part of x2600, the Atari 2600 Emulator
   ===================================================
   
   Copyright 1996 Alex Hornby. For contributions see the file CREDITS.

   This software is distributed under the terms of the GNU General Public
   License. This is free software with ABSOLUTELY NO WARRANTY.
   
   See the file COPYING for details.
   
   $Id: options.c,v 1.7 1996/11/24 16:55:40 ahornby Exp $

   Tweaked by Matthew Stroup for Amiga v2600, January 27, 1997

******************************************************************************/

/* Command Line Option Parser */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#if HAVE_GETOPT_H
#include <getopt.h>
#endif /* HAVE_GETOPT_H */

#include "dbg_mess.h"


#include "version.h"
/* *INDENT-OFF* */
/* Options common to all ports of x2600 */
struct BaseOptions {
  int rr;
  int tvtype;
  int lcon;
  int rcon;
  int bank;
  int magstep;
  char filename[80];
  int sound;
  int swap;
  int realjoy;
  int limit;
  int mousey;
  int mitshm;
  int dbg_level;
} base_opts={1,0,0,0,0,1,"",0,0,1,0,0,1,0};

static void
copyright(void)
{
    printf("\nVirtual 2600 Version %s.\nCopyright 1995/6 Alex Hornby.\n\nDistributed under the terms of the GNU General public license.\n",VERSION);
    printf("Virtual 2600 comes with ABSOLUTELY NO WARRANTY; for details see file COPYING.\nThis is free software, and you are welcome to redistribute it under\ncertain conditions.\n\n");
}

static void
base_usage(void)
{
#if HAVE_GETOPT_LONG
printf("usage: v2600 [options] filename
Both short and long options are accepted
where options include:
        -h --help           This information
        -v --version        Show the current version/copyright info
        -f --framerate int  Set the refresh rate (1)
        -n --ntsc           Emulate an NTSC 2600 (default)
        -p --pal            Emulate a PAL 2600
        -l --lcon  <type>   Left controller, 0=JOY, 1=PADDLE, 2=KEYPAD.
        -r --rcon <type>    Right controller, 0=JOY, 1=PADDLE, 2=KEYPAD.
        -b --bank <type>    Bank switching scheme, 0=NONE,1=Atari 8k,
                            2=Atari 16k, 3=Parker 8k, 4=CBS, 5=Atari Super Chip
        -m --magstep <int>  The magnification to use (X11 only)        
        -s --sound          Turn on sound. (default)
        -S --nosound        Turn off sound.
        -w --swap           Swap the left and right control methods.
        -j --realjoy        Use real joysticks.
        -t --limit          Turn on speed limiting
        -y --mousey         Use mouse y for paddle (e.g. for video olypmics).
        -M --nomitshm       turn OFF MIT Shared memory for X11.
        -V --dbg_level      Set the debugging verbosity level, 0=NONE, 1=USER
                            2-4 programmer debug.
");
#else
printf("usage: v2600 [options] filename\nwhere options include:\n        -h           This information\n        -v           Show the current version/copyright info\n        -f int       Set the refresh rate (1=default)\n");
printf("        -n           Emulate an NTSC 2600 (default)\n        -p           Emulate a PAL 2600\n        -l <type>    Left controller, 0=JOY (default), 1=PADDLE.\n");
printf("        -r <type>    Right controller, 0=JOY (default), 1=PADDLE.\n        -b <type>    Bank switching scheme, 0=NONE (default),1=Atari 8k,\n                     2=Atari 16k, 3=Parker 8k, 4=CBS, 5=Atari Super Chip\n");
printf("        -s           Turn on sound.\n        -S           Turn off sound. (default)\n        -w           Swap the left and right control methods.\n        -j           Use real joysticks. (default)\n");
printf("        -k           Use keyboard for joystick.\n        -y           Use mouse for paddle (e.g. video olympics).\n");
printf("        -V           Set the debugging verbosity level, 0=NONE (default),\n                     1=USER, 2-4 programmer debug.\n\n");
#endif
}

// I don't understand why this exists...
/*
static void 
usage(void)
{
    base_usage();
}
*/
int
parse_options(int argc, char **argv)
{
#if HAVE_GETOPT_H
    int c;

    while (1)
    {
	int option_index = 0;
#if HAVE_GETOPT_LONG
	static struct option long_options[] =
	{
            {"framerate", 1, 0, 'f'},
            {"ntsc", 0, 0, 'n'},
            {"pal", 0, 0, 'p'},
            {"help", 0, 0, 'h'},
            {"version", 0, 0, 'v'},
            {"lcon", 1, 0, 'l'},
            {"rcon", 1, 0, 'r'},
	    {"bank", 1, 0, 'b'},
	    {"magstep", 1, 0, 'm'},
	    {"sound", 1, 0, 's'},
	    {"nosound", 1, 0, 'S'},
	    {"swap", 1, 0, 'w'},
	    {"realjoy", 1, 0, 'j'},
	    {"limit", 1, 0, 't'},
	    {"mousey", 1, 0, 'y'},
	    {"nomitshm", 1, 0, 'M'},
	    {"dbg_level", 1, 0, 'V'}
	};
	
	c = getopt_long (argc, argv, "hvf:npl:r:b:m:sSwjtyMV:",
			 long_options, &option_index);	
#else
	c = getopt(argc, argv, "hvf:npl:r:b:m:sSwjtyMV:");
#endif  /* GETOPT_LONG */
	if (c == -1)
            break;

	switch (c)
	{
	case 'h':
	    copyright();
	    usage();
	    exit(0);
	    break;

	case 'v':
	    copyright();
	    exit(0);
	    break;

	case 'f':
	    printf ("Setting frame rate to `%s'\n", optarg);
	    base_opts.rr=atoi(optarg);
	    break;
	    
	case 'n':
	    base_opts.tvtype=0;
	    break;

	case 'p':
	    base_opts.tvtype=1;
	    break;

	case 'l':
	    printf ("option l with value `%s'\n", optarg);
	    base_opts.lcon=atoi(optarg);
	    break;
	
	case 'r':
	    printf ("option r with value `%s'\n", optarg);
	    base_opts.rcon=atoi(optarg);
	    break;

	case 'b':
	    printf ("option b with value `%s'\n", optarg);
	    base_opts.bank=atoi(optarg);
	    break;

	case 'm':
	    printf ("option m with value `%s'\n", optarg);
	    base_opts.magstep=atoi(optarg);
	    break;
	    
	case 's':
	    base_opts.sound=1;
	    break;

	case 'S':
	    base_opts.sound=0;
	    break;

	case 'w':
	    base_opts.swap=1;
	    break;

	case 'j':
	    base_opts.realjoy=1;
	    break;

	case 'k':
         base_opts.realjoy=0;
         break;

	case 'y':
	    base_opts.mousey=1;
	    break;

	case 'M':
	    base_opts.mitshm=0;
	    break;

	case 'V':
	    base_opts.dbg_level=atoi(optarg);
	    break;

	default:
	    fprintf (stderr,"\ngetopt() returned character code 0%o\n", c);
	}
    }
    if (optind < argc)
    {
	dbg_message(DBG_NORMAL, "non-option ARGV-elements: ");
	while (optind < argc) {
	    dbg_message(DBG_NORMAL, "%s\n",argv[optind]);
	    if(argv[optind]!=NULL)
		strcpy(base_opts.filename, argv[optind]);
	    optind++;
	}
    }
#else
// Here's a generic command parser...
int n=1;
	if (argc==1)
	{
		copyright();
		base_usage();
		exit(0);
	}
	while(argv[n][0]=='-')
	{
		switch (argv[n][1])
		{
			case 'h':
				copyright();
				base_usage();
				exit(0);
				break;

			case 'v':
				copyright();
				exit(0);
				break;

			case 'f':
				printf ("Setting frame rate to `%s'\n", argv[n+1]);
				base_opts.rr=atoi(argv[n+1]);
				n++;
				break;
	    
			case 'n':
				base_opts.tvtype=0;
				break;

			case 'p':
				base_opts.tvtype=1;
				break;

			case 'l':
				printf ("Left controller set to type `%s'\n", argv[n+1]);
				base_opts.lcon=atoi(argv[n+1]);
				n++;
				break;
	
			case 'r':
				printf ("Right controller set to type `%s'\n", argv[n+1]);
				base_opts.rcon=atoi(argv[n+1]);
				n++;
				break;

			case 'b':
				printf ("Banking set to type `%s'\n", argv[n+1]);
				base_opts.bank=atoi(argv[n+1]);
				n++;
				break;

			case 's':
				base_opts.sound=1;
				break;

			case 'S':
				base_opts.sound=0;
				break;

			case 'w':
				base_opts.swap=1;
				break;

			case 'j':
				base_opts.realjoy=1;
				break;

			case 'k':
				base_opts.realjoy=0;
				break;

			case 't':
				base_opts.limit=1;
				break;

			case 'y':
				base_opts.mousey=1;
				break;

			case 'V':
				printf ("Debug level set to '%s'\n", argv[n+1]);
				base_opts.dbg_level=atoi(argv[n+1]);
				n++;
				break;

			default:
				fprintf (stderr,"\n'%s' is not valid or is not a supported option.\n", argv[n]);
		}
		n++;
	}
	if (n<argc) strcpy(base_opts.filename, argv[n]);
	else
	{
		printf("No filename was specified.\n");
		exit(0);
	}

#endif

    return 0;
}
