/**
 * TrueReality - parser.c
 * Copyright (C) 1998, 1999 Niki W. Waibel
 *
 * This program is free software; you can redistribute it and/
 * or modify it under the terms of the GNU General Public Li-
 * cence as published by the Free Software Foundation; either
 * version 2 of the Licence, or any later version.
 *
 * This program is distributed in the hope that it will be use-
 * ful, but WITHOUT ANY WARRANTY; without even the implied war-
 * ranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public Licence for more details.
 *
 * You should have received a copy of the GNU General Public
 * Licence along with this program; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
 * USA.
 *
 * Information about me (the author):
 *   Niki W. Waibel, Reichenau 20, 6890 Lustenau, Austria - EUROPE
 *   niki.waibel@gmx.net
**/





/* include section */

#include <stdio.h>

#include "config.h"

#include "file_loader_extern.h"
#include "parser.h"
#include "parser_extern.h"


#ifdef DEBUG
#       include "debug.h"
#endif





/* export section */

struct tr_prefs prefs;   /* filled by 'int parse_args(int argc, char *argv[])' */





/* static section */

static void     print_help_message(char *myname);










/*******************************************************************************
*                                                                              *
* Parses the command line                                                      *
*                                                                              *
*                                                                              *
* 'display' and 'UseSHM' of structure 'prefs' are set currently.               *
* This routine also sets the pointer 'real_name' or                            *
* the pointers 'dmem_name' and 'imem_name' in structure 'rominfo' from         *
* 'file_loader.c'.                                                             *
*                                                                              *
*******************************************************************************/

int parse_args(int argc, char *argv[])
{
        char *options[]=
        {
                "help",
                "shm", "noshm",
                "nodisp", "localdisp", "localdispproc", "remotedisp",
                "controller",
                "depth",
                "joy", "nojoy",
                "d", "i",
                "nosound", "sound", "filesound",
                "sym",
                NULL
        };
        int     i, j;
        int     number;
        int     retValue = 0;





        puts("Parse the command line ..."); fflush(stdout);




        /* default values */

        prefs.load_what = N64_NOTHING;

        prefs.plugged_controller        = CONTR_1;

        prefs.symbols                   = NO_SYMBOLS;

#ifdef DISPLAY
        prefs.display                   = LOCAL_DISPLAY;
#endif

#ifdef JOYSTICK
        prefs.joystick                  = USE_JOYSTICK;
#endif

#ifdef AUDIO
        prefs.sound                     = NO_SOUND;
#endif

#ifdef COMMAND_DEPTH
        prefs.depth                     = 8;
#endif

#ifdef SHM
        prefs.UseSHM                    = 1;
#endif



        for(i=1; (i < argc) && (retValue >= 0); i++)
        {
                if(*argv[i] != '-')
                {
                        switch(prefs.load_what)
                        {
                            case N64_NOTHING:
                                rominfo.real_name = argv[i];
                                prefs.load_what = N64_IMAGE;
                                break;

                            case N64_DMEM:
                            case N64_IMEM:
                            case N64_DMEM | N64_IMEM:
                                fprintf(stderr, "   Will not load image %s, because you used '-d' or '-i' before\n", argv[i]);
                                break;

                            case N64_IMAGE:
                                fprintf(stderr, "   ROM '%s' is used instead of '%s'\n", rominfo.real_name, argv[i]);
                                break;

                        } /* switch(prefs.load_what) */

                        continue;

                } /* if(*argv[i] != '-') */



                for(j=0; options[j]; j++)
                        if(!strcmp(argv[i]+1, options[j]))
                                break;





                switch(j)
                {
                    case 0:
                        retValue = -1;
                        break;



#ifdef SHM
                    case 1:
                        prefs.UseSHM = 1;
                        break;

                    case 2:
                        prefs.UseSHM = 0;
                        break;
#endif /* #ifdef SHM */



                    case 3:
                        prefs.display = NO_DISPLAY;
                        break;




#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY)
                    case 4:
                        prefs.display = LOCAL_DISPLAY;
                        break;
#endif

#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY_PROCESS)
                    case 5:
                        prefs.display = LOCAL_DISPLAY_PROCESS;
                        break;
#endif

#if (DISPLAY_SUPPORT & SUPPORT_REMOTE_DISPLAY)
                    case 6:
                        prefs.display = REMOTE_DISPLAY;
                        break;
#endif



                    case 7:
                        if(argc >= i+2)
                                if(sscanf(argv[i+1], "%x", &number) == 1)
                                {
                                        prefs.plugged_controller = number;
                                        i++;
                                        break;
                                }
                        
                        fputs("   Could not read hex integer after '-controller'\n"
                              "   '-controller 1' will be used as default!\n", stderr);
                        break;



#ifdef COMMAND_DEPTH
                    case 8:
                        if(argc >= i+2)
                                if(sscanf(argv[i+1], "%d", &number) == 1)
                                {
                                        prefs.depth = number;
                                        i++;
                                        break;
                                }
                        
                        fputs("   Could not read integer after '-depth'\n"
                              "   '-depth 8' will be used as default!\n", stderr);
#endif /* #ifdef COMMAND_DEPTH */



#ifdef JOYSTICK
                    case 9:
                        prefs.joystick = USE_JOYSTICK;
                        break;

                    case 10:
                        prefs.joystick = NO_JOYSTICK;
                        break;
#endif /* #ifdef JOYSTICK */



                    case 11:
                        if(argc >= i+2)
                        {
                                i++;
                                rominfo.dmem_name = argv[i];
                                prefs.load_what |= N64_DMEM;
                        }
                        else
                                retValue = -1;
                        break;

                    case 12:
                        if(argc >= i+2)
                        {                        
                                i++;
                                rominfo.imem_name = argv[i];
                                prefs.load_what |= N64_IMEM;
                        }
                        else
                                retValue = -1;
                        break;



#ifdef AUDIO
                    case 13:
                        prefs.sound = NO_SOUND;
                        break;

                    case 14:
                        prefs.sound = NORMAL_SOUND;
                        break;

                    case 15:
                        if(argc >= i+2)
                        {
                                if(argv[i+1][0] != '-')
                                {
                                        i++;
                                        prefs.sound = FILE_SOUND;
                                        prefs.sound_file = argv[i];
                                }
                                else
                                {
                                        fprintf(stderr,
                                                "   WARNING: assume that the file you specified (%s)\n"
                                                "            after the '-filesound' option\n"
                                                "            is an option (because of the '-') and not a file!\n"
                                                "            So sound is turned off!!!\n",
                                                argv[i+1]);
                                        prefs.sound = NO_SOUND;
                                }
                        }
                        else
                        {
                                prefs.sound = NO_SOUND;
                                retValue = -1;
                        }
                        break;
#endif /* #ifdef AUDIO */



                    case 16:
                        if(argc >= i+2)
                        {
                                if(argv[i+1][0] != '-')
                                {
                                        i++;
                                        prefs.symbols = USE_SYMBOLS;
                                        prefs.symbol_file = argv[i];
                                }
                                else
                                {
                                        fprintf(stderr,
                                                "   WARNING: assume that the file you specified (%s)\n"
                                                "            after the '-sym' option\n"
                                                "            is an option (because of the '-') and not a file!\n"
                                                "            Symbols are not activated!!!\n",
                                                argv[i+1]);
                                        prefs.symbols = NO_SYMBOLS;
                                }
                        }
                        else
                        {
                                prefs.symbols = NO_SYMBOLS;
                                retValue = -1;
                        }
                        break;


                    default:
                        fprintf(stderr, "   %s: Wrong option '%s'\n", argv[0], argv[i]);

                } /* switch(j) */

        } /* for(i=1; i<argc; i++) */



        if(prefs.load_what == N64_NOTHING || retValue < 0)
        {
                print_help_message(argv[0]);
                retValue = -1;
        }



        puts("... READY\n"
             "-------------------------------------------------------------------------------"); fflush(stdout);

        return(retValue);

} /* int ParseArgs(int argc, char *argv[]) */










static void print_help_message(char *myname)
{
        fprintf(stderr,
                "\n\n"
                "USAGE: %s [options] rom_name\n"
                "USAGE: %s [options] -i di_name\n"
                "USAGE: %s [options] -d di_name\n"
                "USAGE: %s [options] -d dmem_name -i imem_name\n\n"
                "options:\n"
                " -sym f [f is the symbol file]\n"
                " -controller n  [just n=1 (default) or n=0 implemented yet]\n"
#ifdef AUDIO
                " -nosound (default)\n"
                " -sound\n"
                " -filesound f [f is the file to store the audio data]\n"
#endif
#ifdef JOYSTICK
                " -joy (default)\n"
                " -nojoy\n"
#endif
#ifdef DISPLAY_SUPPORT
                " -nodisp\n"
#endif
#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY)
                " -localdisp (default)\n"
#endif
#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY_PROCESS)
                " -localdispproc [do not use this yet]\n"
#endif
#if (DISPLAY_SUPPORT & SUPPORT_REMOTE_DISPLAY)
                " -remotedisp    [do not use this yet]\n"
#endif
#ifdef SHM
                " -shm (default)\n"
                " -noshm\n"
#endif
#ifdef COMMAND_DEPTH
                " -depth n  [n=8 (default) or n=15,16,24,32]\n"
#endif
                "\n"
                "rom_name  : Filename of N64 ROM image\n"
                "di_name   : Name of DMEM/IMEM binaries ('.dmem' / '.imem' are added!)\n"
                "dmem_name : Filename of DMEM binary\n"
                "imem_name : Filename of IMEM binary\n"
                "\n", myname, myname, myname, myname);

} /* static void print_help_message(char *myname) */





