#include <stdio.h>
#include <strings.h>
#include <sys/wait.h>
#include "c_config.h"
#include "c_defs.h"
#include "c_structs.h"
#include "c_externs.h"

/*
 * cbzone_parseopts.c
 *  -- Todd W Mummert, December 1990, CMU
 *
 * Parse the options, read the MOTD, etc...
 */
  
extern char *optarg;
extern int optind, opterr;

int pager(file)
     char *file;
{
  char buf[100];
  FILE *f;

  sprintf(buf,"%s/%s",HELPDIRECTORY,file);
  if ((f=fopen(buf,"r")) != NULL) {
    fclose(f);
    switch (fork()) {
    case 0:
      execl(PAGER,PAGER,buf,0);
      printf("Unable to  read file %s\n", buf);
      fflush(stdout);
      exit(1);
    case -1:
      printf("Unable to fork process\n");
      fflush(stdout);
      return(1);
    default:
      wait(0);
      break;
    }
  }
  else {
    printf("File %s not found or unreadable.\n", buf);
    return(1);
  }
  return(0);
}

void parseopt(argc, argv, opt)
     int argc;
     char *argv[];
     Optionp opt;
{
  int c, tmp;
  int errflg = 0;
  logical salvo_changed = FALSE;
  logical early_exit = FALSE;
  logical original = FALSE;
  extern int getopt();

  opt->mblocks = MBLOCKS;
  opt->copters = FALSE;
  opt->delay = 5;
  opt->mlanders = MLANDERS;
  opt->mmissiles = MMISSILES;
  opt->loud = TRUE;
  opt->mtanks = MTANKS;
  opt->practice = FALSE;

  pager("cbzone.motd");
  
  while ((c = getopt(argc, argv, "b:cd:hl:m:OqSs:t:V")) != -1)
    switch (c) {
    case 'b':                           /* blocks */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp != opt->mblocks)
        opt->practice = TRUE;
      if (tmp < 0)
        opt->mblocks = 0;
      else
        opt->mblocks = tmp;
      break;
    case 'c':                           /* copter practice */
      opt->copters = TRUE;
      opt->practice = TRUE;
      break;
    case 'd':                           /* delay value */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp > opt->delay)
        opt->practice = TRUE;
      if (tmp <  0)
        opt->delay = 0;
      else if (tmp > 10)
        opt->delay = 10;
      else
        opt->delay = tmp;
      break;
    case 'h':                           /* help */
      if (pager("cbzone.help"))
        printf("Sorry help information not available.\n");
      errflg++;
      break;
    case 'l':                           /* landers */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp != opt->mlanders)
        opt->practice = TRUE;
      if (tmp < 0)
        opt->mlanders = 0;
      else
        opt->mlanders = tmp;
      break;
    case 'm':                           /* missiles */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp != opt->mmissiles)
        opt->practice = TRUE;
      if (tmp < 0)
        opt->mmissiles = 0;
      else
        opt->mmissiles = tmp;
      break;
    case 'O':                           /* Original version */
      original = TRUE;
      break;
    case 'q':                           /* quiet mode */
      opt->loud = FALSE;
      break;
    case 'S':                           /* print scores */
      scores(-1, TRUE);
      early_exit = TRUE;
      break;
    case 's':                           /* salvos */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp != opt->msalvos)
        opt->practice = TRUE;
      if (tmp < 1)
        opt->msalvos = 1;
      else
        opt->msalvos = tmp;
      salvo_changed = TRUE;
      break;
    case 't':                           /* tanks */
      if (sscanf(optarg,"%d",&tmp) != 1)
        errflg++;
      if (tmp != opt->mtanks)
        opt->practice = TRUE;
      if (tmp < 0)
        opt->mtanks = 0;
      else
        opt->mtanks = tmp;
      break;
    case 'V':                           /* version info */
      printf("Version \"%s\"\n", VERSION);
      early_exit = TRUE;
      break;
    default:
      errflg++;
      break;
    }

  if (errflg) {
    printf("usage:  cbzone [-b n] [-d n] [-l n] [-m n] [-s n] [-t n] [-OSVchq]\n");
    printf("\tFor more information on options, use:  cbzone -h\n");
    exit(1);
  }

  if (early_exit)
    exit(0);

  if (opt->copters)
    opt->mtanks = 0;

  if (original) {
    opt->mblocks = 8;
    opt->copters = FALSE;
    opt->mlanders = 1;
    opt->mmissiles = 1;
    opt->mtanks = 1;
    opt->practice = TRUE;
    salvo_changed = FALSE;
  }

  opt->menemies = (opt->mtanks > opt->mmissiles ?
                   opt->mtanks : opt->mmissiles);
  if (!opt->menemies) {
    printf("Must have at least one missile or tank.\n");
    exit(1);
  }

  if (!salvo_changed)
    opt->msalvos = opt->menemies;
  opt->mobjects = opt->mblocks + opt->mlanders + 2*opt->menemies +
    opt->msalvos + 1;
  opt->estart = 1;
  opt->lstart = opt->estart + opt->menemies;
  opt->sstart = opt->lstart + opt->mlanders;
  opt->bstart = opt->sstart + opt->menemies + opt->msalvos;
}
