#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "decl.h"
#include "data.h"
#include "data.c"

int Setup(void);
void ShutDown(void);
void Warn(void);
int ChooseSource(void);
char ChooseCharOrRandomChar(void);

int main() {
  int source;
  char charStart = '*';
  srand((unsigned) time(&t));
  if (Setup()) {
    while (1) {
      source = ChooseSource();
      if (source) {
	charStart = ChooseCharOrRandomChar();
      }
      PresentQuestions(source, charStart);
      if (UserWantsToQuit("\nDo you want to shut down now?")) break;
    }
    ShutDown();
  } else {
    Warn();
  }
  return (0);
}

int Setup(void)
{
  int i, j;
  char passwd[MaxWordSize];
  printf("Welcome to Gskc Vocabz %c ver: 1.2!! (03-Nov-1997)\n", 169);
  printf("This program is the sole property of\n");
  printf("   Gaurav S. Kc\n   GPO Box 5770\n");
  printf("   Kathmandu\n   Nepal.\n");
  printf("   Contact: gskc@cyberdude.com\n");
  printf("   Phone: (977)-1-526-920 & (977)-1-537-363\n\n\n");
  for (i = 1; i <= 3; i++) {
    j = 0;
    printf("Please enter the access code: ");
    while ((passwd[j] = getchar()) != '\n') {
      j++;
    }
    passwd[j] = '\0';
    if (strlen(passwd) == 4) {
      if (passwd[0] == 103 && passwd[1] == 115 &&
	  passwd[2] == 107 && passwd[3] == 99) {
	InitDataBase();
	return (1);
      }
    }
    printf("\n\nAccess denied!!\n");
  }
  return (0);
}

void ShutDown(void)
{
  printf("\nThank-you for using Gskc Vocabz ...\n");
  printf("I hope you enjoyed your session, \n");
  printf("and also managed to learn new words.\n");
  printf("Goodbye.\n");
  ShutDataBase();
  return;
}

void Warn(void) {
  printf("\nIn case you have misplaced your access code,\n");
  printf("you may obtain another one by placing a request to:\n");
  printf("Gaurav S. Kc. However, if your copy of GskcVocabz is\n");
  printf("not yet registered, you may be liable to pay a fine for\n");
  printf("infringement of copyright material.\n");
  printf("\nContact the publishers for further details.\n");
  while ((XtraChar = getchar()) != '\n');
  return;
}

int ChooseSource(void)
{
  while (1) {
    printf("\nDo you want to practise only with \n");
    printf("words beginning with a specific letter?\n");
    printf("If so, please enter 1, else 0: ");
    userEntry = getchar();
    if (userEntry=='1' || userEntry=='0') {
      while ((XtraChar = getchar()) != '\n');
      return (userEntry-'0');
    } else if (userEntry != '\n') {
      while ((XtraChar = getchar()) != '\n');
    }
  }
}

char ChooseCharOrRandomChar(void)
{
  // printf("ChooseCharOrRandomChar entered ...\n");
  while (1) {
    printf("Enter the letter you want; press '*' ");
    printf("if you have no particular choice: ");
    if ((userEntry = getchar()) == '*'){
      userEntry = IthAlphabet((int) (1+RandomCharFactor*rand()));
      break;
    } else if (userEntry>='a' && userEntry<='z') {
      break;
    } else if (userEntry != '\n') {
      while ((XtraChar = getchar()) != '\n');
    }
    printf("Please enter a lowercase letter or '*'!!\n");
  }
  while ((XtraChar = getchar()) != '\n');
  // printf("ChooseCharOrRandomChar exited ...\n");
  return ((char) userEntry);
}

