/*
  File autogenerated by gengetopt version 2.10
  generated with the following command:
  gengetopt --conf-parser -i ivtv-tune.ggo 

  The developers of gengetopt consider the fixed text that goes in all
  gengetopt output files to be in the public domain:
  we make no copyright claims on it.
*/


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

/* If we use autoconf.  */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "getopt.h"

#include "cmdline.h"

void
cmdline_parser_print_version (void)
{
  printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}

void
cmdline_parser_print_help (void)
{
  cmdline_parser_print_version ();
  printf("\n"
  "Purpose:\n"
  "  Channel/Frequency changer for IVTV\n"
  "\n"
  "Usage: %s [OPTIONS]...\n", CMDLINE_PARSER_PACKAGE);
  printf("   -h         --help              Print help and exit\n");
  printf("   -V         --version           Print version and exit\n");
  printf("   -cSTRING   --channel=STRING    set new channel\n");
  printf("   -dSTRING   --device=STRING     set video device node\n");
  printf("   -fFLOAT    --frequency=FLOAT   set new frequency (MHz)\n");
  printf("   -l         --list-channels     list all channels and their frequencies (default=off)\n");
  printf("   -L         --list-freqtable    list all available frequency mappings (default=off)\n");
  printf("   -tSTRING   --freqtable=STRING  set frequency map to use\n");
}


static char *gengetopt_strdup (const char *s);

/* gengetopt_strdup() */
/* strdup.c replacement of strdup, which is not standard */
char *
gengetopt_strdup (const char *s)
{
  char *result = (char*)malloc(strlen(s) + 1);
  if (result == (char*)0)
    return (char*)0;
  strcpy(result, s);
  return result;
}

int
cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
{
  int c;	/* Character of the parsed option.  */
  int missing_required_options = 0;

  args_info->help_given = 0 ;
  args_info->version_given = 0 ;
  args_info->channel_given = 0 ;
  args_info->device_given = 0 ;
  args_info->frequency_given = 0 ;
  args_info->list_channels_given = 0 ;
  args_info->list_freqtable_given = 0 ;
  args_info->freqtable_given = 0 ;
#define clear_args() { \
  args_info->channel_arg = NULL; \
  args_info->device_arg = NULL; \
  args_info->list_channels_flag = 0;\
  args_info->list_freqtable_flag = 0;\
  args_info->freqtable_arg = NULL; \
}

  clear_args();

  optarg = 0;
  optind = 1;
  opterr = 1;
  optopt = '?';

  while (1)
    {
      int option_index = 0;
      char *stop_char;

      static struct option long_options[] = {
        { "help",	0, NULL, 'h' },
        { "version",	0, NULL, 'V' },
        { "channel",	1, NULL, 'c' },
        { "device",	1, NULL, 'd' },
        { "frequency",	1, NULL, 'f' },
        { "list-channels",	0, NULL, 'l' },
        { "list-freqtable",	0, NULL, 'L' },
        { "freqtable",	1, NULL, 't' },
        { NULL,	0, NULL, 0 }
      };

      stop_char = 0;
      c = getopt_long (argc, argv, "hVc:d:f:lLt:", long_options, &option_index);

      if (c == -1) break;	/* Exit from `while (1)' loop.  */

      switch (c)
        {
        case 'h':	/* Print help and exit.  */
          clear_args ();
          cmdline_parser_print_help ();
          exit (EXIT_SUCCESS);

        case 'V':	/* Print version and exit.  */
          clear_args ();
          cmdline_parser_print_version ();
          exit (EXIT_SUCCESS);

        case 'c':	/* set new channel.  */
          if (args_info->channel_given)
            {
              fprintf (stderr, "%s: `--channel' (`-c') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->channel_given = 1;
          args_info->channel_arg = gengetopt_strdup (optarg);
          break;

        case 'd':	/* set video device node.  */
          if (args_info->device_given)
            {
              fprintf (stderr, "%s: `--device' (`-d') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->device_given = 1;
          args_info->device_arg = gengetopt_strdup (optarg);
          break;

        case 'f':	/* set new frequency (MHz).  */
          if (args_info->frequency_given)
            {
              fprintf (stderr, "%s: `--frequency' (`-f') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->frequency_given = 1;
          args_info->frequency_arg = (float)strtod (optarg, NULL);
          break;

        case 'l':	/* list all channels and their frequencies.  */
          if (args_info->list_channels_given)
            {
              fprintf (stderr, "%s: `--list-channels' (`-l') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->list_channels_given = 1;
          args_info->list_channels_flag = !(args_info->list_channels_flag);
          break;

        case 'L':	/* list all available frequency mappings.  */
          if (args_info->list_freqtable_given)
            {
              fprintf (stderr, "%s: `--list-freqtable' (`-L') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->list_freqtable_given = 1;
          args_info->list_freqtable_flag = !(args_info->list_freqtable_flag);
          break;

        case 't':	/* set frequency map to use.  */
          if (args_info->freqtable_given)
            {
              fprintf (stderr, "%s: `--freqtable' (`-t') option given more than once\n", CMDLINE_PARSER_PACKAGE);
              clear_args ();
              exit (EXIT_FAILURE);
            }
          args_info->freqtable_given = 1;
          args_info->freqtable_arg = gengetopt_strdup (optarg);
          break;


        case 0:	/* Long option with no short option */

        case '?':	/* Invalid option.  */
          /* `getopt_long' already printed an error message.  */
          exit (EXIT_FAILURE);

        default:	/* bug: option not considered.  */
          fprintf (stderr, "%s: option unknown: %c\n", CMDLINE_PARSER_PACKAGE, c);
          abort ();
        } /* switch */
    } /* while */


  if ( missing_required_options )
    exit (EXIT_FAILURE);

  return 0;
}

#define CONFIGPARSERBUFSIZE 1024

int
cmdline_parser_configfile (char * const filename, struct gengetopt_args_info *args_info, int override)
{
  FILE* file;
  char linebuf[CONFIGPARSERBUFSIZE];
  int line_num = 0;
  int len;
  int fnum;
  char fopt[CONFIGPARSERBUFSIZE], farg[CONFIGPARSERBUFSIZE];
  char *stop_char;
  char *str_index, *str_index2;
  int next_token;

  if ((file = fopen(filename, "r")) == NULL)
    {
      fprintf (stderr, "%s: Error opening configuration file '%s'\n",
               CMDLINE_PARSER_PACKAGE, filename);
      exit (EXIT_FAILURE);
    }

  while ((fgets(linebuf, CONFIGPARSERBUFSIZE, file)) != NULL)
    {
      ++line_num;
      len = strlen(linebuf);
      if (len == CONFIGPARSERBUFSIZE-1)
        {
          fprintf (stderr, "%s: Line longer than %d characters found in configuration file '%s'\n",
                   CMDLINE_PARSER_PACKAGE, CONFIGPARSERBUFSIZE, filename);
          exit (EXIT_FAILURE);
        }

      if (linebuf[0] == '#')
        continue; /* Line was a comment */

      /* read the option */
      fnum = sscanf (linebuf, "%s", fopt);

      if (fnum < 0)
        continue; /* blank line */

      next_token = strlen (fopt);
      str_index = strchr (&linebuf[next_token], '\"');
      if (str_index)
        {
          str_index2 = strchr (str_index + 1, '\"');
          if (! str_index2)
            {
              fprintf 
                (stderr, 
                 "%s: unterminated string in configuration file '%s'\n",
                 CMDLINE_PARSER_PACKAGE, filename);
              exit (EXIT_FAILURE);
            }

          strncpy (farg, str_index + 1, str_index2 - str_index - 1);
          farg[str_index2 - str_index]='\0';
          ++fnum;
        }
      else
        if (sscanf (&linebuf[next_token], "%s", farg) > 0)
          ++fnum;

      if (fnum > 0)
        {
          if (!strcmp(fopt, "help"))
            {
              if (override || !args_info->help_given)
                {
                  args_info->help_given = 1;
                  
                }
              continue;
            }
          if (!strcmp(fopt, "version"))
            {
              if (override || !args_info->version_given)
                {
                  args_info->version_given = 1;
                  
                }
              continue;
            }
          if (!strcmp(fopt, "channel"))
            {
              if (override || !args_info->channel_given)
                {
                  args_info->channel_given = 1;
                  if (fnum == 2)
                    args_info->channel_arg = gengetopt_strdup (farg);
                  else
                    {
                      fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
                               filename, line_num);
                      exit (EXIT_FAILURE);
                    }
                }
              continue;
            }
          if (!strcmp(fopt, "device"))
            {
              if (override || !args_info->device_given)
                {
                  args_info->device_given = 1;
                  if (fnum == 2)
                    args_info->device_arg = gengetopt_strdup (farg);
                  else
                    {
                      fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
                               filename, line_num);
                      exit (EXIT_FAILURE);
                    }
                }
              continue;
            }
          if (!strcmp(fopt, "frequency"))
            {
              if (override || !args_info->frequency_given)
                {
                  args_info->frequency_given = 1;
                  if (fnum == 2)
                    args_info->frequency_arg = (float)strtod (farg, NULL);
                  else
                    {
                      fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
                               filename, line_num);
                      exit (EXIT_FAILURE);
                    }
                }
              continue;
            }
          if (!strcmp(fopt, "list-channels"))
            {
              if (override || !args_info->list_channels_given)
                {
                  args_info->list_channels_given = 1;
                  args_info->list_channels_flag = !(args_info->list_channels_flag);
                }
              continue;
            }
          if (!strcmp(fopt, "list-freqtable"))
            {
              if (override || !args_info->list_freqtable_given)
                {
                  args_info->list_freqtable_given = 1;
                  args_info->list_freqtable_flag = !(args_info->list_freqtable_flag);
                }
              continue;
            }
          if (!strcmp(fopt, "freqtable"))
            {
              if (override || !args_info->freqtable_given)
                {
                  args_info->freqtable_given = 1;
                  if (fnum == 2)
                    args_info->freqtable_arg = gengetopt_strdup (farg);
                  else
                    {
                      fprintf (stderr, "%s:%d: required <option_name> <option_val>\n",
                               filename, line_num);
                      exit (EXIT_FAILURE);
                    }
                }
              continue;
            }
          

          /* Tried all known options. This one is unknown! */
          fprintf (stderr, "%s: Unknown option '%s' found in %s\n",
                   CMDLINE_PARSER_PACKAGE, fopt, filename);
          exit (EXIT_FAILURE);
        }
    } /* while */
  fclose(file); /* No error checking on close */

  return 0;
}
