/**
 * TrueReality - parser.c
 * Copyright (C) 1998 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 "romimage_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' in structure 'rominfo' from   *
* 'romimage.c'.                                                                *
*                                                                              *
*******************************************************************************/

int parse_args(int argc, char *argv[])
{
        char *options[]=
        {
                "help",
                "shm", "noshm",
                "nodisp", "localdisp", "localdispproc", "remotedisp",
                "controller",
                NULL
        };
        int     i, j, n;
        int     number;
        int     retValue;



        prefs.plugged_controller = CONTR_1;

        prefs.display            = NO_DISPLAY;

#ifdef SHM
        prefs.UseSHM  = 1;
#else
        prefs.UseSHM  = 0;
#endif
   


        retValue = 0;

   

        for(i=1, n=0; (i < argc) && (retValue >= 0); i++)
        {
                if(*argv[i] != '-')
                {
                        n++;
                        switch(n)
                        {
                            case 1:
                                rominfo.real_name = argv[i];
                                break;
               
                            default:
                                fprintf(stderr, "ROM '%s' is used instead of '%s'\n", rominfo.real_name, argv[i]);

                        } /* switch(n) */
         
                        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;

                    case 1:
#ifdef SHM
                        prefs.UseSHM = 1;
#else
                        fputs("parse_args: shared memory stuff was not compiled in - ignored\n", stderr);
#endif
                        break;

                    case 2:
                        prefs.UseSHM = 0;
                        break;

                    case 3:
                        prefs.display = NO_DISPLAY;
                        break;

                    case 4:
#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY)
                        prefs.display = LOCAL_DISPLAY;
#else
                        fprintf(stderr, "parse_args: local display stuff not supported by display '%s' - ignored\n", DISPLAY);
#endif
                        break;

                    case 5:
#if (DISPLAY_SUPPORT & SUPPORT_LOCAL_DISPLAY_PROCESS)
                        prefs.display = LOCAL_DISPLAY_PROCESS;
#else
                        fprintf(stderr, "parse_args: local process display stuff not supported by display '%s' - ignored\n", DISPLAY);
#endif
                        break;

                    case 6:
#if (DISPLAY_SUPPORT & SUPPORT_REMOTE_DISPLAY)
                        prefs.display = REMOTE_DISPLAY;
#else
                        fprintf(stderr, "parse_args: remote display stuff not supported by display '%s' - ignored\n", DISPLAY);
#endif
                        break;
         
                    case 7:
                        if(argc >= i+2)
                                if(sscanf(argv[i+1], "%x", &number) == 1)
                                {
                                        prefs.plugged_controller = number;
                                        i++;
                                        break;
                                }
                        
                        fputs("ParseArgs: Could not read hex integer after '-controller'\n"
                              "           '-controller 1' will be used as default!\n", stderr);
                        break;
                        

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

                } /* switch(j) */

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



        if(n == 0 || retValue < 0)
        {
                print_help_message(argv[0]);
                retValue = -1;
        }


   
        return(retValue);

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










static void print_help_message(char *myname)
{
        fprintf(stderr,
                "\n\n"
                "USAGE: %s [options] romname\n\n"
                "options:\n"
                " -shm (default if compiled in)\n"
                " -noshm\n"
                " -controller n  [just n=1 (default) or n=0 implemented yet]\n"
                " -nodisp (default)\n"
                " -localdisp\n"
                " -localdispproc [do not use this yet]\n"
                " -remotedisp    [do not use this yet]\n"
                "\n", myname);

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





