/*
  This file is part of CardWords.
  (C) 1999 Tobias Peters
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_textclient.cc

#include <iostream>
#include <stdlib.h> // for getenv(3)
#include "cardwords_options.hh"
#include "cardwords_exceptions.hh"
#include "cardwords_textclient.hh"
#include <unistd.h>

int
main(int argc, const char **argv)
{
  // Refuse to run as root:
  if (getuid() == 0 || geteuid() == 0) {
    cerr << "This program is part of a game. Do not run this program as root."
         << endl;
    return 1;
  }

  setlocale (LC_ALL, "");
  bindtextdomain ("cardwords", LOCALEDIR_STR);
  textdomain ("cardwords");

  string home_directory = getenv("HOME");

  ostrstream options_filename;
  options_filename << home_directory
                   << "/.cardwords/options"
                   << ends;
  options_filename.freeze(true);

  CardWords_Options options(options_filename.str());

  cout << gettext ("Welcome to the cardwords textmode client.\n"\
                   "Please answer the questions below to start a game.\n"
                   "Please note that it is not possible to run this program"\
                   " in a dumb terminal\n"\
                   "like inside emacs.\n")
       << endl
       << gettext ("This program is part of cardwords, "\
                   "Copyright (C) 1998 1999 Tobias Peters\n"\
                   "cardwords is free software; you can redistribute it and/or modify\n"
                   "it under the terms of the GNU General Public License as published by\n"
                   "the Free Software Foundation; either version 2 of the License, or\n"
                   "(at your option) any later version.\n"
                   "\n"
                   "This program is distributed in the hope that it will be useful,\n"
                   "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
                   "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
                   "GNU General Public License for more details.\n"
                   "\n"
                   "You should have received a copy of the GNU General Public License\n"
                   "along with this program; if not, write to the Free Software\n"
                   "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
                   );

  string           old_string_val;
  bool             old_bool_val, new_bool_val;
  unsigned         old_unsigned_val, new_unsigned_val;
  string           line;
  
  if (options.get_type("name") == UNKNOWN_TYPE) {
    options.set("name", getenv("LOGNAME"));
  }
  if (options.get_type("name") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "name"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("name", &old_string_val);
  cout << gettext("Your name [")
       << old_string_val
       << gettext("]: ")
       << flush;
  getline(cin, line, '\n');
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (line != "") {
    options.set("name", line);
  }

  if (options.get_type("alt_name") == UNKNOWN_TYPE) {
    options.set("alt_name", "Zockerking");
  }
  if (options.get_type("alt_name") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "alt_name"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("alt_name", &old_string_val);
  cout << gettext("An alternative name [")
       << old_string_val
       << gettext("]: ")
       << flush;
  getline(cin, line, '\n');
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (line != "") {
    options.set("alt_name", line);
  }


  if (options.get_type("log_in_as_player") == UNKNOWN_TYPE) {
    options.set("log_in_as_player", true);
  }
  if (options.get_type("log_in_as_player") != BOOL_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "log_in_as_player"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("log_in_as_player", &old_bool_val);
  cout << gettext("Log in as Player [")
       << (old_bool_val ? gettext("Yes") : gettext("No"))
       << gettext("]: ")
       << flush;
  getline(cin, line);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (line != "") {
    new_bool_val = (line == gettext("Yes")
                    || line == gettext("yes")
                    || line == gettext("YES")
                    || line == gettext("Y")
                    || line == gettext("y"));
    if (new_bool_val != old_bool_val) {
      options.set("log_in_as_player", new_bool_val);
    }
  }

  if (options.get_type("start_local_server") == UNKNOWN_TYPE) {
    options.set("start_local_server", true);
  }
  if (options.get_type("start_local_server") != BOOL_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "start_local_server"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("start_local_server", &old_bool_val);
  cout << gettext("Start a local server [")
       << (old_bool_val ? gettext("Yes") : gettext("No"))
       << gettext("]: ")
       << flush;
  getline(cin, line);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (line != "") {
    new_bool_val = (line == gettext("Yes")
                    || line == gettext("yes")
                    || line == gettext("YES")
                    || line == gettext("Y")
                    || line == gettext("y"));
    if (new_bool_val != old_bool_val) {
      options.set("start_local_server", new_bool_val);
    }
  }


  if (options.get_type("server_program") == UNKNOWN_TYPE) {
    options.set("server_program", BINDIR_STR "/cardwords_server");
  }
  if (options.get_type("server_program") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "server_program"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("server_program", &old_string_val);
  cout << gettext("Server program [")
       << old_string_val
       << gettext("]: ")
       << flush;
  getline(cin, line);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (line != "") {
    options.set("server_program", line);
  }


  if (options.get_type("charset_file") == UNKNOWN_TYPE) {
    options.set("charset_file", LIBDIR_STR "/default.charset");
  }
  if (options.get_type("charset_file") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "charset_file"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("charset_file", &old_string_val);
  cout << gettext("Charset file [")
       << old_string_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0' && strcmp(old_string_val, buffer) != 0) {
    options.set("charset_file", buffer);
  }


  if (options.get_type("cardset_file") == UNKNOWN_TYPE) {
    options.set("cardset_file", LIBDIR_STR "/default.cardset");
  }
  if (options.get_type("cardset_file") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "cardset_file"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("cardset_file", &old_string_val);
  cout << gettext("Cardset file [")
       << old_string_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0' && strcmp(old_string_val, buffer) != 0) {
    options.set("cardset_file", buffer);
  }


  if (options.get_type("cardtable_file") == UNKNOWN_TYPE) {
    options.set("cardtable_file", LIBDIR_STR "/default.cardtable");
  }
  if (options.get_type("cardtable_file") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "cardtable_file"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("cardtable_file", &old_string_val);
  cout << gettext("Card-table file [")
       << old_string_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0' && strcmp(old_string_val, buffer) != 0) {
    options.set("cardtable_file", buffer);
  }


  if (options.get_type("bonus_file") == UNKNOWN_TYPE) {
    options.set("bonus_file", LIBDIR_STR "/default.bonus");
  }
  if (options.get_type("bonus_file") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "bonus_file"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("bonus_file", &old_string_val);
  cout << gettext("Bonus file [")
       << old_string_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0' && strcmp(old_string_val, buffer) != 0) {
    options.set("bonus_file", buffer);
  }


  if (options.get_type("number_of_human_players") == UNKNOWN_TYPE) {
    options.set("number_of_human_players", 1U);
  }
  if (options.get_type("number_of_human_players") != UNSIGNED_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "number_of_human_players"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("number_of_human_players", &old_unsigned_val);
  cout << gettext("Number of human players [")
       << old_unsigned_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0') {
    new_unsigned_val = old_unsigned_val;
    istrstream istr(buffer);
    istr >> new_unsigned_val;
    if (new_unsigned_val != old_unsigned_val) {
      options.set("number_of_human_players", new_unsigned_val);
    }
  }


  if (options.get_type("number_of_robot_players") == UNKNOWN_TYPE) {
    options.set("number_of_robot_players", 1U);
  }
  if (options.get_type("number_of_robot_players") != UNSIGNED_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "number_of_robot_players"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("number_of_robot_players", &old_unsigned_val);
  cout << gettext("Number of robot players [")
       << old_unsigned_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0') {
    new_unsigned_val = old_unsigned_val;
    istrstream istr(buffer);
    istr >> new_unsigned_val;
    if (new_unsigned_val != old_unsigned_val) {
      options.set("number_of_robot_players", new_unsigned_val);
    }
  }


  if (options.get_type("number_of_watchers") == UNKNOWN_TYPE) {
    options.set("number_of_watchers", LIBDIR_STR "/default.cardtable");
  }
  if (options.get_type("number_of_watchers") != UNSIGNED_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "number_of_watchers"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("number_of_watchers", &old_unsigned_val);
  cout << gettext("Number of watchers [")
       << old_unsigned_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0') {
    new_unsigned_val = old_unsigned_val;
    istrstream istr(buffer);
    istr >> new_unsigned_val;
    if (new_unsigned_val != old_unsigned_val) {
      options.set("number_of_watchers", new_unsigned_val);
    }
  }


  if (options.get_type("personal_dictionary_file") == UNKNOWN_TYPE) {
    ostrstream default_pers_dict;
    default_pers_dict << home_directory << "/.cardwords/words" << ends;
    default_pers_dict.freeze(true);
    options.set("personal_dictionary_file", default_pers_dict.str());
    default_pers_dict.freeze(false);
  }
  if (options.get_type("personal_dictionary_file") != STRING_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "personal_dictionary_file"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("personal_dictionary_file", &old_string_val);
  cout << gettext("Personal dictionary file [")
       << old_string_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0' && strcmp(old_string_val, buffer) != 0) {
    options.set("personal_dictionary_file", buffer);
  }


  if (options.get_type("port_number") == UNKNOWN_TYPE) {
    options.set("port_number", 27037U);
  }
  if (options.get_type("port_number") != UNSIGNED_TYPE) {
    cerr << gettext("ERROR: Option `")
         << "port_number"
         << gettext("' in options file `")
         << options_filename.str()
         << gettext("' has bad type.")
         << endl;
    return 1;
  }
  options.get("port_number", &old_unsigned_val);
  cout << gettext("Port number [")
       << old_unsigned_val
       << gettext("]: ")
       << flush;
  cin.getline(buffer, BUFFER_SIZE);
  if (cin.bad()) {
    cout << endl << gettext("OK, exiting.") << endl;
    return 1;
  }
  if (*buffer != '\0') {
    new_unsigned_val = old_unsigned_val;
    istrstream istr(buffer);
    istr >> new_unsigned_val;
    if (new_unsigned_val != old_unsigned_val) {
      options.set("Port number", new_unsigned_val);
    }
  }

  if (options.is_saved() == false) {
    cout << endl
         << "======================================================"
         << endl
         << gettext("Options have changed. Save them in `")
         << options_filename.str()
         << gettext("'? [")
         << gettext("Yes")
         << gettext("]: ")
         << flush;
    cin.getline(buffer, BUFFER_SIZE);
    if (cin.bad()) {
      cout << endl << gettext("OK, exiting.") << endl;
      return 1;
    }

    new_bool_val = (*buffer == '\0'
                    || strcmp(gettext("Yes"), buffer) == 0
                    || strcmp(gettext("yes"), buffer) == 0
                    || strcmp(gettext("YES"), buffer) == 0
                    || strcmp(gettext("Y"), buffer) == 0
                    || strcmp(gettext("y"), buffer) == 0);
    if (new_bool_val) {
      options.save(options_filename.str());
    }
  }
    
    
  options_filename.freeze(false);


  /*   CardWords_TextClient client;

  client.set_name("Trav",
                  "Trab");
  client.set_log_mode(CardWords_CommClient::lm_owner_player);
  
  //  char c;
  if (client.connect() < 0) {
    cerr << "Unable to connect to remote host" << endl;
    return 1;
  }

  client.register_reading();
  while (client.is_registered_for_reading()) {
    while (client.is_registered_for_writing()) {
      client.send_now();
    }
    client.read_now();
  }


  client.disconnect(); */
  return 0;
}
