 /* 
  * UAE - The Un*x Amiga Emulator
  * 
  * Main program 
  * 
  * (c) 1995 Bernd Schmidt, Ed Hanway
  */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"
#include "amiga.h"
#include "options.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "disk.h"
#include "debug.h"
#include "xwin.h"
#include "os.h"
#include "filesys.h"
#include "keybuf.h"
#include "gui.h"

int framerate = DEFAULT_FRAMERATE;
bool dont_want_aspect = DEFAULT_NO_ASPECT;
bool use_fast_draw = true;
bool use_debugger = false;
bool use_slow_mem = DEFAULT_WANT_SLOW_MEM;
bool use_fast_mem = DEFAULT_WANT_FAST_MEM;
bool automount_uaedev = true;
bool produce_sound = true;
KbdLang keyboard_lang = DEFAULT_KBD_LANG;
#ifdef __DOS__
bool use_dos_hicolor = false;
#endif

static void usage(void)
{
    printf("UAE - The Un*x Amiga emulator\n");
    printf("Summary of command-line options:\n");
    printf("  -h                       : Print help\n");
    printf("  -m VOLNAME:mount_point   : mount file system at <mount point> as AmigaDOS\n"
	   "                             volume VOLNAME:\n");
    printf("  -M VOLNAME:mount_point   : like -m, but mount read-only\n");
    printf("  -S                       : Turn off sound support (if it is configured)\n");
    printf("  -s                       : Emulate 1MB slow memory at 0xC00000\n");
    printf("  -F                       : Emulate 2MB fast memory at 0x200000\n");
    printf("  -d                       : Draw the screen with the right aspect\n");
    printf("  -f n                     : Set the frame rate to 1/n\n");
    printf("  -x                       : Use slower, but more portable drawing method\n");
    printf("  -D                       : Start up the built-in debugger\n");
    printf("  -l lang                  : Set keyboard language to lang, where lang is\n"
	   "                             either DE or US\n");
#ifdef __DOS__
    printf("  -H                       : Use Hicolor mode for better colors\n");
#endif
}

#ifdef __unix

static bool mount_seen = false;

static void parse_cmdline(int argc, char **argv)
{
    int c;
    extern char *optarg;

    while(((c = getopt(argc, argv, "l:Df:dhxFasSm:M:H")) != EOF)) switch(c) {
     case 'h':
	usage();
	exit(0);
	
     case 'm':
     case 'M':
	{
	    /* mount file system (repeatable)
	     * syntax: [-m | -M] VOLNAME:/mount_point
	     * example: -M CDROM:/cdrom -m UNIXFS:./disk
	     */
	    static char buf[256];
	    char *s2;
	    bool readonly = (c == 'M');
	    
	    if (mount_seen)
		fprintf (stderr, "Multiple mounts not supported right now, sorry.\n");
	    else {
		mount_seen = true;
		strcpy(buf, optarg);
		s2 = strchr(buf, ':');
		if(s2) {
		    *s2++ = '\0';
		    add_filesys_unit(buf, s2, readonly);
		} else {
		    fprintf(stderr, "Usage: [-m | -M] VOLNAME:/mount_point\n");
		}
	    }
	}
	break;
	
     case 'S':
	produce_sound = false;
	break;

     case 'f':
	framerate = atoi(optarg);
	break;
	
     case 'd':
	dont_want_aspect = false;
	break;
	
     case 'x':
	use_fast_draw = false;
	break;

     case 'D':
	use_debugger = true;
	break;

     case 'l':
	if (0 == strcasecmp(optarg, "de"))
	    keyboard_lang = KBD_LANG_DE;
	else if (0 == strcasecmp(optarg, "us"))
	    keyboard_lang = KBD_LANG_US;
	break;
	
     case 'a':
	automount_uaedev = false;
	break;
	
     case 's':
	use_slow_mem = true;
	break;
	
     case 'F':
	use_fast_mem = true;
	break;
#ifdef __DOS__
     case 'H':
	use_dos_hicolor = true;
	break;     
#endif
    }
}
#endif

int main(int argc, char **argv)
{
    parse_cmdline(argc, argv);
    if (produce_sound && !init_sound()) {       
	fprintf(stderr, "Sound driver unavailable: Sound output disabled\n");
	produce_sound = false;
    }

#ifdef __DOS__
    freopen("NUL", "w", stderr);
#endif
    
    if (gui_init() < 0) {
	fprintf(stderr, "Failed to initialize the GUI\n");
	/* abort()? Maybe. */
    }
    if (graphics_init()) {
	init_joystick();
	keybuf_init ();    
	memory_init();
	custom_init();
	DISK_init();
	init_m68k();
	MC68000_reset();

	debug();
    
	graphics_leave();
	close_joystick();
    }
#if 0
    {
	extern void dump_counts();
	dump_counts();
    }
#endif
    gui_exit();
    return 0;
}
