/*
 * Copyright (c) 2004, Anthony Roberts
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following
 * disclaimer in the documentation and/or other materials provided
 * with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */



#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include "arger.h"


extern char *myname;


int option_printversion (int argc, char **argv, int index, void *state) {
  printf("shuffle 0.1 by Anthony Roberts");
  printf(" <acrobert@ucalgary.ca>\n");
  
  exit(0);
}

int option_printhelp (int argc, char **argv, int index, void *state) {
  printf("usage: shuffle [inputfile]\n");
  printf("\n");
  printf("\n-v --version\tDisplay version information.\n");
  printf("\n-h --help\tDisplay help information\n");
  printf("(you're looking at this)\n");
  printf("\n\n");
  
  exit(0);
}

int option_setval (int argc, char **argv, int index, void *state) {
  ((program_state_t *) state)->incom = argv[index];

  return ++index;
}

int getoption (int argc, char **argv, int startat, void *state) {
  option_def_t opt_list[] = {
    (option_def_t) { "-v", 3, option_printversion },
    (option_def_t) { "--version", 10, option_printversion },
    (option_def_t) { "-h", 3, option_printhelp },
    (option_def_t) { "--help", 7, option_printhelp },
    (option_def_t) { "", 0, option_setval },
    (option_def_t) { NULL, 0, NULL }
  };
  int i, j;
  
  for (i = startat; i < argc; ) {
    j = 0;
    while (opt_list[j].namelen != 0) {
      if (strncmp(opt_list[j].name, argv[i], opt_list[j].namelen) == 0) {
	i = opt_list[j].action(argc, argv, i, state);
	break;
      }

      j++;
    }

    if (opt_list[j].namelen == 0) {
      i = opt_list[j].action(argc, argv, i, state);
      return 0;
    }
  }

  if (! ((program_state_t *) state)->set) {
      fprintf(stderr, "%s: not enough arguments\n", myname);
      exit(1);
  }

  return 1;
}

